forms.py
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
from django import forms
from django.utils.translation import ugettext_lazy as _
from users.models import User
from pycpfcnpj import cpfcnpj
import re
class RegisterUserForm(forms.ModelForm):
password = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
password2 = forms.CharField(label = _('Password confirmation'), widget = forms.PasswordInput)
# birth_date = forms.DateField(widget=forms.SelectDateWidget())
MIN_LENGTH = 8
def validate_cpf(self, cpf):
cpf = ''.join(re.findall('\d', str(cpf)))
# print(cpf)
# if (not cpf) or (len(cpf) < 11):
# return False
# #Get only the first 9 digits and generate other 2
# _int = map(int, cpf)
# integer = list(map(int, cpf))
# new = integer[:9]
# while len(new) < 11:
# r = sum([(len(new) + 1 - i)* v for i, v in enumerate(new)]) % 11
# if r > 1:
# f = 11 - r
# else:
# f = 0
# new.append(f)
# #if generated number is the same(original) the cpf is valid
# new2 = list(new)
# if new2 == _int:
# return cpf
# else:
# return False
if cpfcnpj.validate(cpf):
return True
return False
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(email = email).exists():
raise forms.ValidationError(_('There is already a registered User with this e-mail'))
return email
def clean_cpf(self):
cpf = self.cleaned_data['cpf']
if User.objects.filter(cpf = cpf).exists():
raise forms.ValidationError(_('There is already a registeres User with this CPF'))
if not self.validate_cpf(cpf):
raise forms.ValidationError(_('Please enter a valid CPF'))
return cpf
def clean_password(self):
password = self.cleaned_data.get('password')
# At least MIN_LENGTH long
if len(password) < self.MIN_LENGTH:
raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_LENGTH))
# At least one letter and one non-letter
first_isalpha = password[0].isalpha()
if all(c.isalpha() == first_isalpha for c in password):
raise forms.ValidationError(_('The password must contain at least one letter and at least one digit or '\
"a punctuation character."))
return password
def clean_password2(self):
password = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password and password2 and password != password2:
raise forms.ValidationError(_('The confirmation password is incorrect.'))
return password2
def save(self, commit=True):
super(RegisterUserForm, self).save(commit=False)
self.instance.set_password(self.cleaned_data['password'])
self.instance.save()
return self.instance
class Meta:
model = User
# exclude = ['is_staff', 'is_active']
fields = ['username', 'name', 'email', 'city', 'state', 'gender', 'cpf', 'birth_date', 'phone', 'image']