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
65 lines (52 loc) · 1.99 KB
/
forms.py
File metadata and controls
65 lines (52 loc) · 1.99 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
from collections import ChainMap
from django import forms
from django.contrib.auth.forms import UserCreationForm
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 UserEmailForm(ModelForm):
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)
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):
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)
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."),
)