forked from pythonprobr/pythonpro-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.py
More file actions
101 lines (80 loc) · 3.2 KB
/
forms.py
File metadata and controls
101 lines (80 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from collections import ChainMap
from captcha.fields import ReCaptchaField
from captcha.widgets import ReCaptchaV2Checkbox
from django import forms
from django.contrib.auth.forms import UserCreationForm, PasswordResetForm
from django.core.exceptions import ValidationError
from django.forms import CharField, ModelForm
from django.utils.translation import gettext_lazy as _
from pythonpro.core.models import User
class NormalizeEmailMixin:
def _normalize_email(self):
self.data = dict(self.data.items())
email = self.data.get('email')
if email is not None:
self.data['email'] = email.lower()
class UserEmailForm(ModelForm, NormalizeEmailMixin):
current_password = CharField(label=_("Password"), strip=False, required=True)
class Meta:
model = User
fields = ('email',)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self._normalize_email()
def clean(self):
cleaned_data = super().clean()
if not self.user.check_password(cleaned_data.get('current_password', '')):
self.add_error('current_password', ValidationError('Senha Inválida', 'invalid_password'))
return cleaned_data
class UserSignupForm(UserCreationForm, NormalizeEmailMixin):
class Meta:
model = User
fields = ('first_name', 'email', 'source')
def __init__(self, *args, **kwargs):
self.plain_password = User.objects.make_random_password(30)
data = kwargs.get('data', None)
if data is not None:
self._set_passwords(data)
kwargs['data'] = data
elif args:
query_dict = args[0]
dct = {}
self._set_passwords(dct)
args = (ChainMap(query_dict, dct), *args[1:])
super().__init__(*args, **kwargs)
self._normalize_email()
def _set_passwords(self, data):
if 'password1' not in data and 'password2' not in data:
data['password1'] = data['password2'] = self.plain_password
password1 = forms.CharField(
label=_("Password"),
strip=False,
required=False,
widget=forms.PasswordInput,
)
password2 = forms.CharField(
label=_("Password confirmation"),
widget=forms.PasswordInput,
strip=False,
required=False,
help_text=_("Enter the same password as before, for verification."),
)
class LeadForm(UserSignupForm):
class Meta:
model = User
fields = ('first_name', 'email')
first_name = forms.CharField(
label=str(), widget=forms.TextInput(attrs={'placeholder': 'Qual Seu Nome?'})
)
email = forms.EmailField(
label=str(), widget=forms.EmailInput(attrs={'placeholder': 'Qual seu MELHOR e-mail?'})
)
source = forms.CharField(widget=forms.HiddenInput())
password1 = forms.CharField(widget=forms.HiddenInput())
password2 = forms.CharField(widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['email'].widget.attrs.pop("autofocus", None)
class PythonProResetForm(PasswordResetForm):
captcha = ReCaptchaField(widget=ReCaptchaV2Checkbox)