Commit 4d782f00b0354d02795b453b582d43fd8e003bcc

Authored by ailsoncgt
1 parent baf2c430

import date to validate date register and update profile #7

Showing 1 changed file with 26 additions and 25 deletions   Show diff stats
users/forms.py
1 # coding=utf-8 1 # coding=utf-8
2 -import os 2 +import os, re
  3 +from datetime import date
  4 +from pycpfcnpj import cpfcnpj
3 from django.conf import settings 5 from django.conf import settings
4 from django import forms 6 from django import forms
5 from django.utils.translation import ugettext_lazy as _ 7 from django.utils.translation import ugettext_lazy as _
@@ -8,40 +10,39 @@ from django.contrib.auth.forms import UserCreationForm @@ -8,40 +10,39 @@ from django.contrib.auth.forms import UserCreationForm
8 from core.forms import RegisterUserForm 10 from core.forms import RegisterUserForm
9 from .models import User 11 from .models import User
10 12
11 -  
12 -class ProfileForm(forms.ModelForm):  
13 -  
14 - def save(self, commit=True):  
15 - super(ProfileForm, self).save(commit=False)  
16 -  
17 - self.instance.set_password(self.cleaned_data['password'])  
18 - self.instance.save()  
19 -  
20 - return self.instance  
21 -  
22 - class Meta:  
23 - model = User  
24 - fields = ['username', 'name', 'email', 'password', 'birth_date', 'city', 'state', 'gender', 'cpf', 'phone', 'image']  
25 - widgets = {  
26 - 'password':forms.PasswordInput  
27 - }  
28 -  
29 class UserForm(RegisterUserForm): 13 class UserForm(RegisterUserForm):
30 14
31 class Meta: 15 class Meta:
32 model = User 16 model = User
33 fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active'] 17 fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active']
34 18
35 -class EditUserForm(forms.ModelForm): 19 +class UpdateUserForm(forms.ModelForm):
  20 +
  21 + def validate_cpf(self, cpf):
  22 + cpf = ''.join(re.findall('\d', str(cpf)))
  23 +
  24 + if cpfcnpj.validate(cpf):
  25 + return True
  26 + return False
  27 +
  28 + def clean_cpf(self):
  29 + cpf = self.cleaned_data['cpf']
  30 + if not self.validate_cpf(cpf):
  31 + raise forms.ValidationError(_('Please enter a valid CPF'))
  32 + return cpf
  33 +
  34 + def clean_birth_date(self):
  35 + birth_date = self.cleaned_data['birth_date']
  36 + if birth_date >= date.today():
  37 + raise forms.ValidationError(_('Please enter a valid date'))
  38 + return birth_date
36 39
37 class Meta: 40 class Meta:
38 model = User 41 model = User
39 - fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'cpf', 'phone', 'image'] 42 + fields = ['username', 'name', 'email', 'city', 'state', 'birth_date', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active']
40 43
41 -# Ailson  
42 -class UpdateUserForm(forms.ModelForm):  
43 - company_logo = forms.ImageField(label=_('Company Logo'),required=False, error_messages = {'invalid':_("Image files only")}) 44 +class UpdateProfileForm(UpdateUserForm):
44 45
45 class Meta: 46 class Meta:
46 model = User 47 model = User
47 - fields = ['username', 'name', 'email', 'city', 'state', 'birth_date', 'gender', 'cpf', 'phone', 'image']  
48 \ No newline at end of file 48 \ No newline at end of file
  49 + fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'cpf', 'phone', 'image']