Commit be8e2a40ee46fea0d6bde11dd064e2906f754ca6

Authored by Zambom
1 parent dd6e8430

Changing user forms

amadeus/roles.py
@@ -2,25 +2,14 @@ from rolepermissions.roles import AbstractUserRole @@ -2,25 +2,14 @@ from rolepermissions.roles import AbstractUserRole
2 2
3 class Student(AbstractUserRole): 3 class Student(AbstractUserRole):
4 available_permissions = { 4 available_permissions = {
5 - 'view_courses': True,  
6 - 'view_modules': True,  
7 - 'view_categories': True,  
8 - 'subscribe_course': True,  
9 } 5 }
10 6
11 class Professor(AbstractUserRole): 7 class Professor(AbstractUserRole):
12 available_permissions = { 8 available_permissions = {
13 - 'create_courses_record': True,  
14 - 'edit_courses_record': True,  
15 - 'delete_courses': True,  
16 - 'view_modules': True,  
17 - 'create_modules': True,  
18 - 'edit_modules': True,  
19 - 'delete_modules': True,  
20 - 'view_categories': True,  
21 - 'create_categories': True,  
22 - 'edit_categories': True,  
23 - 'delete_categories': True, 9 + }
  10 +
  11 +class Coordinator(AbstractUserRole):
  12 + available_permissions = {
24 } 13 }
25 14
26 class SystemAdmin(AbstractUserRole): 15 class SystemAdmin(AbstractUserRole):
amadeus/settings.py
@@ -53,6 +53,7 @@ INSTALLED_APPS = [ @@ -53,6 +53,7 @@ INSTALLED_APPS = [
53 53
54 'users', 54 'users',
55 'localization', 55 'localization',
  56 + 'log',
56 57
57 ] 58 ]
58 59
users/admin.py
1 from django.contrib import admin 1 from django.contrib import admin
2 from .models import User 2 from .models import User
3 -from .forms import AdminUserForm 3 +from .forms import UserForm
4 4
5 class UserAdmin(admin.ModelAdmin): 5 class UserAdmin(admin.ModelAdmin):
6 list_display = ['username', 'name', 'email', 'is_staff', 'is_active'] 6 list_display = ['username', 'name', 'email', 'is_staff', 'is_active']
7 search_fields = ['username', 'name', 'email'] 7 search_fields = ['username', 'name', 'email']
8 - form = AdminUserForm 8 + form = UserForm
9 9
10 admin.site.register(User, UserAdmin) 10 admin.site.register(User, UserAdmin)
users/forms.py
@@ -9,7 +9,68 @@ from rolepermissions.shortcuts import assign_role @@ -9,7 +9,68 @@ from rolepermissions.shortcuts import assign_role
9 from django.contrib.auth.forms import UserCreationForm 9 from django.contrib.auth.forms import UserCreationForm
10 from .models import User 10 from .models import User
11 11
12 -class AdminUserForm(forms.ModelForm): 12 +class Validation(forms.ModelForm):
  13 + def clean_email(self):
  14 + email = self.cleaned_data['email']
  15 +
  16 + if User.objects.filter(email = email).exists():
  17 + raise forms.ValidationError(_('There is already a registered User with this e-mail'))
  18 +
  19 + return email
  20 +
  21 + def validate_cpf(self, cpf):
  22 + cpf = ''.join(re.findall('\d', str(cpf)))
  23 +
  24 + if not cpf == '':
  25 + if cpfcnpj.validate(cpf):
  26 + return True
  27 +
  28 + return False
  29 +
  30 + return True
  31 +
  32 + def clean_cpf(self):
  33 + cpf = self.cleaned_data['cpf']
  34 +
  35 + if not self.validate_cpf(cpf):
  36 + raise forms.ValidationError(_('Please enter a valid CPF'))
  37 +
  38 + return cpf
  39 +
  40 + def clean_birth_date(self):
  41 + birth_date = self.cleaned_data['birth_date']
  42 +
  43 + if not birth_date is None:
  44 + if birth_date >= date.today():
  45 + raise forms.ValidationError(_('Please enter a valid date'))
  46 +
  47 + return birth_date
  48 +
  49 +class ValidationRegister(Validation):
  50 + def clean_password(self):
  51 + password = self.cleaned_data.get('password')
  52 +
  53 + # At least MIN_LENGTH long
  54 + if len(password) < self.MIN_LENGTH:
  55 + raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_LENGTH))
  56 +
  57 + # At least one letter and one non-letter
  58 + first_isalpha = password[0].isalpha()
  59 + if all(c.isalpha() == first_isalpha for c in password):
  60 + raise forms.ValidationError(_('The password must contain at least one letter and at least one digit or a punctuation character.'))
  61 +
  62 + return password
  63 +
  64 + def clean_password2(self):
  65 + password = self.cleaned_data.get("password")
  66 + password2 = self.cleaned_data.get("password2")
  67 +
  68 + if password and password2 and password != password2:
  69 + raise forms.ValidationError(_('The confirmation password is incorrect.'))
  70 +
  71 + return password2
  72 +
  73 +class UserForm(Validation):
13 def save(self, commit=True): 74 def save(self, commit=True):
14 super(AdminUserForm, self).save(commit=False) 75 super(AdminUserForm, self).save(commit=False)
15 76
@@ -22,6 +83,8 @@ class AdminUserForm(forms.ModelForm): @@ -22,6 +83,8 @@ class AdminUserForm(forms.ModelForm):
22 assign_role(self.instance, 'student') 83 assign_role(self.instance, 'student')
23 elif self.instance.type_profile == 1: 84 elif self.instance.type_profile == 1:
24 assign_role(self.instance, 'professor') 85 assign_role(self.instance, 'professor')
  86 + elif self.instance.type_profile == 3:
  87 + assign_role(self.instance, 'coordinator')
25 88
26 self.instance.save() 89 self.instance.save()
27 90
@@ -36,125 +99,26 @@ class AdminUserForm(forms.ModelForm): @@ -36,125 +99,26 @@ class AdminUserForm(forms.ModelForm):
36 'password':forms.PasswordInput 99 'password':forms.PasswordInput
37 } 100 }
38 101
39 -class RegisterUserForm(forms.ModelForm):  
40 - 102 +class RegisterUserForm(ValidationRegister):
41 password = forms.CharField(label=_('Password'), widget=forms.PasswordInput) 103 password = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
42 password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) 104 password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput)
43 - # birth_date = forms.DateField(widget=forms.SelectDateWidget())  
44 - MIN_LENGTH = 8  
45 -  
46 - def validate_cpf(self, cpf):  
47 - cpf = ''.join(re.findall('\d', str(cpf)))  
48 -  
49 - if cpfcnpj.validate(cpf):  
50 - return True  
51 - return False  
52 -  
53 - def clean_email(self):  
54 - email = self.cleaned_data['email']  
55 - if User.objects.filter(email = email).exists():  
56 - raise forms.ValidationError(_('There is already a registered User with this e-mail'))  
57 - return email  
58 -  
59 - def clean_birth_date(self):  
60 - birth_date = self.cleaned_data['birth_date']  
61 - if birth_date >= date.today():  
62 - raise forms.ValidationError(_('Please enter a valid date'))  
63 - return birth_date  
64 -  
65 -  
66 - def clean_cpf(self):  
67 - cpf = self.cleaned_data['cpf']  
68 - if (cpf == ""):  
69 - return cpf  
70 - if User.objects.filter(cpf = cpf).exists():  
71 - raise forms.ValidationError(_('There is already a registered User with this CPF'))  
72 - if not self.validate_cpf(cpf):  
73 - raise forms.ValidationError(_('Please enter a valid CPF'))  
74 - return cpf  
75 105
76 - def clean_password(self):  
77 - password = self.cleaned_data.get('password')  
78 -  
79 - # At least MIN_LENGTH long  
80 - if len(password) < self.MIN_LENGTH:  
81 - raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_LENGTH))  
82 -  
83 - # At least one letter and one non-letter  
84 - first_isalpha = password[0].isalpha()  
85 - if all(c.isalpha() == first_isalpha for c in password):  
86 - raise forms.ValidationError(_('The password must contain at least one letter and at least one digit or '\  
87 - "a punctuation character."))  
88 -  
89 - return password  
90 -  
91 - def clean_password2(self):  
92 - password = self.cleaned_data.get("password")  
93 - password2 = self.cleaned_data.get("password2")  
94 -  
95 - if password and password2 and password != password2:  
96 - raise forms.ValidationError(_('The confirmation password is incorrect.'))  
97 - return password2 106 + MIN_LENGTH = 8
98 107
99 def save(self, commit=True): 108 def save(self, commit=True):
100 super(RegisterUserForm, self).save(commit=False) 109 super(RegisterUserForm, self).save(commit=False)
  110 +
101 self.instance.set_password(self.cleaned_data['password']) 111 self.instance.set_password(self.cleaned_data['password'])
102 112
103 self.instance.save() 113 self.instance.save()
  114 +
104 return self.instance 115 return self.instance
105 116
106 class Meta: 117 class Meta:
107 model = User 118 model = User
108 - # exclude = ['is_staff', 'is_active']  
109 - fields = ['username', 'name', 'email', 'city', 'state', 'gender', 'cpf', 'birth_date', 'phone', 'image', 'titration',  
110 - 'year_titration', 'institution', 'curriculum',]  
111 -  
112 -class UserForm(RegisterUserForm):  
113 -  
114 - class Meta:  
115 - model = User  
116 - fields = ['username', 'name', 'email', 'birth_date', 'city',  
117 - 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'titration',  
118 - 'year_titration', 'institution', 'curriculum', 'is_staff', 'is_active']  
119 -  
120 -class UpdateUserForm(forms.ModelForm):  
121 -  
122 - def validate_cpf(self, cpf):  
123 - cpf = ''.join(re.findall('\d', str(cpf)))  
124 -  
125 - if cpfcnpj.validate(cpf):  
126 - return True  
127 - return False  
128 -  
129 - def clean_cpf(self):  
130 - cpf = self.cleaned_data['cpf']  
131 - if not self.validate_cpf(cpf):  
132 - raise forms.ValidationError(_('Please enter a valid CPF'))  
133 - return cpf  
134 -  
135 - def clean_birth_date(self):  
136 - birth_date = self.cleaned_data['birth_date']  
137 - if birth_date >= date.today():  
138 - raise forms.ValidationError(_('Please enter a valid date'))  
139 - return birth_date  
140 -  
141 - class Meta:  
142 - model = User  
143 - fields = ['username', 'name', 'email', 'birth_date', 'city',  
144 - 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'titration',  
145 - 'year_titration', 'institution', 'curriculum', 'is_staff', 'is_active']  
146 -  
147 -  
148 -  
149 -class UpdateProfileFormAdmin(UpdateUserForm):  
150 -  
151 - class Meta:  
152 - model = User  
153 - fields = ['username', 'name', 'email', 'birth_date', 'city',  
154 - 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'titration',  
155 - 'year_titration', 'institution', 'curriculum', 'is_staff', 'is_active'] 119 + fields = ['username', 'name', 'email',]
156 120
157 -class UpdateProfileForm(UpdateUserForm): 121 +class UpdateProfileForm(Validation):
158 122
159 class Meta: 123 class Meta:
160 model = User 124 model = User
users/models.py
@@ -26,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin): @@ -26,7 +26,7 @@ class User(AbstractBaseUser, PermissionsMixin):
26 birth_date = models.DateField(_('Birth Date'), null=True, blank=True) 26 birth_date = models.DateField(_('Birth Date'), null=True, blank=True)
27 phone = models.CharField(_('Phone'), max_length = 30, blank = True) 27 phone = models.CharField(_('Phone'), max_length = 30, blank = True)
28 cpf = models.CharField(_('CPF'), max_length = 15, blank=True, null=True) 28 cpf = models.CharField(_('CPF'), max_length = 15, blank=True, null=True)
29 - type_profile = models.IntegerField(_('Type'), null = True, blank = True, choices = ((1, _('Professor')), (2, _('Student')))) 29 + type_profile = models.IntegerField(_('Type'), null = True, blank = True, choices = ((1, _('Professor')), (2, _('Student')), (3, _('Coordinator'))))
30 titration = models.CharField(_('Titration'), max_length = 50, blank = True, null = True) 30 titration = models.CharField(_('Titration'), max_length = 50, blank = True, null = True)
31 year_titration = models.CharField(_('Year of titration'), max_length = 4, blank = True, null = True) 31 year_titration = models.CharField(_('Year of titration'), max_length = 4, blank = True, null = True)
32 institution = models.CharField(_('Institution'), max_length = 50, blank=True, null=True) 32 institution = models.CharField(_('Institution'), max_length = 50, blank=True, null=True)
@@ -36,7 +36,7 @@ class User(AbstractBaseUser, PermissionsMixin): @@ -36,7 +36,7 @@ class User(AbstractBaseUser, PermissionsMixin):
36 is_active = models.BooleanField(_('Active'), default = True) 36 is_active = models.BooleanField(_('Active'), default = True)
37 37
38 USERNAME_FIELD = 'username' 38 USERNAME_FIELD = 'username'
39 - REQUIRED_FIELDS = ['email', 'cpf'] 39 + REQUIRED_FIELDS = ['email']
40 40
41 objects = UserManager() 41 objects = UserManager()
42 42
@@ -47,11 +47,11 @@ class User(AbstractBaseUser, PermissionsMixin): @@ -47,11 +47,11 @@ class User(AbstractBaseUser, PermissionsMixin):
47 def __str__(self): 47 def __str__(self):
48 return self.name or self.username 48 return self.name or self.username
49 49
50 - def get_full_name(self):  
51 - return str(self) 50 + def save(self, *args, **kwargs):
  51 + if not self.is_staff and self.type_profile is None:
  52 + self.type_profile = 2
52 53
53 - def get_short_name(self):  
54 - return str(self).split(" ")[0] 54 + super(User, self).save(*args, **kwargs)
55 55
56 @property 56 @property
57 def image_url(self): 57 def image_url(self):
users/views.py
@@ -12,7 +12,7 @@ from itertools import chain @@ -12,7 +12,7 @@ from itertools import chain
12 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 12 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
13 13
14 from .models import User 14 from .models import User
15 -from .forms import UserForm, UpdateProfileForm, UpdateUserForm, UpdateProfileFormAdmin 15 +from .forms import UserForm, UpdateProfileForm
16 16
17 17
18 #API IMPORTS 18 #API IMPORTS
@@ -85,7 +85,7 @@ class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView): @@ -85,7 +85,7 @@ class Update(HasRoleMixin, LoginRequiredMixin, generic.UpdateView):
85 slug_url_kwarg = 'username' 85 slug_url_kwarg = 'username'
86 context_object_name = 'acc' 86 context_object_name = 'acc'
87 model = User 87 model = User
88 - form_class = UpdateUserForm 88 + form_class = UserForm
89 success_url = reverse_lazy('users:manage') 89 success_url = reverse_lazy('users:manage')
90 90
91 def form_valid(self, form): 91 def form_valid(self, form):
@@ -165,10 +165,9 @@ class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView): @@ -165,10 +165,9 @@ class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView):
165 def get_context_data(self, **kwargs): 165 def get_context_data(self, **kwargs):
166 context = super(UpdateProfile, self).get_context_data(**kwargs) 166 context = super(UpdateProfile, self).get_context_data(**kwargs)
167 context['title'] = 'Update Profile' 167 context['title'] = 'Update Profile'
168 - if has_role(self.request.user, 'system_admin'):  
169 - context['form'] = UpdateProfileFormAdmin(instance = self.object)  
170 - else:  
171 - context['form'] = UpdateProfileForm(instance = self.object) 168 +
  169 + context['form'] = UpdateProfileForm(instance = self.object)
  170 +
172 return context 171 return context
173 172
174 def form_valid(self, form): 173 def form_valid(self, form):