Commit 34d6ebe7d1335370b1639be38090fab2018f9f01
1 parent
f676e02b
Exists in
master
and in
5 other branches
Validate cpf in forms.py #14
Showing
2 changed files
with
41 additions
and
3 deletions
Show diff stats
core/forms.py
1 | from django import forms | 1 | from django import forms |
2 | from django.utils.translation import ugettext_lazy as _ | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | from users.models import User | 3 | from users.models import User |
4 | +from pycpfcnpj import cpfcnpj | ||
5 | +import re | ||
6 | + | ||
7 | + | ||
4 | 8 | ||
5 | class RegisterUserForm(forms.ModelForm): | 9 | class RegisterUserForm(forms.ModelForm): |
6 | 10 | ||
@@ -9,6 +13,37 @@ class RegisterUserForm(forms.ModelForm): | @@ -9,6 +13,37 @@ class RegisterUserForm(forms.ModelForm): | ||
9 | # birth_date = forms.DateField(widget=forms.SelectDateWidget()) | 13 | # birth_date = forms.DateField(widget=forms.SelectDateWidget()) |
10 | MIN_LENGTH = 8 | 14 | MIN_LENGTH = 8 |
11 | 15 | ||
16 | + def validate_cpf(self, cpf): | ||
17 | + cpf = ''.join(re.findall('\d', str(cpf))) | ||
18 | + # print(cpf) | ||
19 | + | ||
20 | + # if (not cpf) or (len(cpf) < 11): | ||
21 | + # return False | ||
22 | + | ||
23 | + # #Get only the first 9 digits and generate other 2 | ||
24 | + # _int = map(int, cpf) | ||
25 | + # integer = list(map(int, cpf)) | ||
26 | + # new = integer[:9] | ||
27 | + | ||
28 | + # while len(new) < 11: | ||
29 | + # r = sum([(len(new) + 1 - i)* v for i, v in enumerate(new)]) % 11 | ||
30 | + | ||
31 | + # if r > 1: | ||
32 | + # f = 11 - r | ||
33 | + # else: | ||
34 | + # f = 0 | ||
35 | + # new.append(f) | ||
36 | + | ||
37 | + # #if generated number is the same(original) the cpf is valid | ||
38 | + # new2 = list(new) | ||
39 | + # if new2 == _int: | ||
40 | + # return cpf | ||
41 | + # else: | ||
42 | + # return False | ||
43 | + if cpfcnpj.validate(cpf): | ||
44 | + return True | ||
45 | + return False | ||
46 | + | ||
12 | def clean_email(self): | 47 | def clean_email(self): |
13 | email = self.cleaned_data['email'] | 48 | email = self.cleaned_data['email'] |
14 | if User.objects.filter(email = email).exists(): | 49 | if User.objects.filter(email = email).exists(): |
@@ -19,7 +54,8 @@ class RegisterUserForm(forms.ModelForm): | @@ -19,7 +54,8 @@ class RegisterUserForm(forms.ModelForm): | ||
19 | cpf = self.cleaned_data['cpf'] | 54 | cpf = self.cleaned_data['cpf'] |
20 | if User.objects.filter(cpf = cpf).exists(): | 55 | if User.objects.filter(cpf = cpf).exists(): |
21 | raise forms.ValidationError(_('There is already a registeres User with this CPF')) | 56 | raise forms.ValidationError(_('There is already a registeres User with this CPF')) |
22 | - print(cpf) | 57 | + if not self.validate_cpf(cpf): |
58 | + raise forms.ValidationError(_('Please enter a valid CPF')) | ||
23 | return cpf | 59 | return cpf |
24 | 60 | ||
25 | def clean_password(self): | 61 | def clean_password(self): |
requirements.txt
1 | click==6.6 | 1 | click==6.6 |
2 | +deps==0.1.0 | ||
2 | Django==1.10 | 3 | Django==1.10 |
3 | -django-autoslug==1.9.3 | ||
4 | django-bootstrap-breadcrumbs==0.8 | 4 | django-bootstrap-breadcrumbs==0.8 |
5 | django-discover-runner==1.0 | 5 | django-discover-runner==1.0 |
6 | django-role-permissions==1.2.1 | 6 | django-role-permissions==1.2.1 |
@@ -10,6 +10,8 @@ itsdangerous==0.24 | @@ -10,6 +10,8 @@ itsdangerous==0.24 | ||
10 | Jinja2==2.8 | 10 | Jinja2==2.8 |
11 | MarkupSafe==0.23 | 11 | MarkupSafe==0.23 |
12 | Pillow==3.3.1 | 12 | Pillow==3.3.1 |
13 | -psycopg2==2.6.2 | 13 | +pkg-resources==0.0.0 |
14 | +pycpfcnpj==1.0.2 | ||
14 | six==1.10.0 | 15 | six==1.10.0 |
16 | +slugify==0.0.1 | ||
15 | Werkzeug==0.11.11 | 17 | Werkzeug==0.11.11 |