Repository navigation
Expand file tree
/
Copy pathforms.py
More file actions
53 lines (42 loc) · 1.5 KB
/
Copy pathforms.py
File metadata and controls
53 lines (42 loc) · 1.5 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
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')
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."),
)
def save(self, commit=True):
user = super().save(commit=False)
user.set_unusable_password()
if commit:
user.save()
return user