Commit f93a3aa6b03951ef8266193bff81c906f059b199
Exists in
master
and in
5 other branches
Merge branch 'dev' of https://github.com/amadeusproject/amadeuslms into dev
Showing
8 changed files
with
95 additions
and
116 deletions
Show diff stats
core/static/js/base/amadeus.js
... | ... | @@ -16,7 +16,7 @@ function campoNumerico(campo, evento){ |
16 | 16 | evento.returnValue = false; |
17 | 17 | return false; |
18 | 18 | } |
19 | -} | |
19 | +}; | |
20 | 20 | |
21 | 21 | function formatarCpf(campo, evento){ |
22 | 22 | var codTecla; |
... | ... | @@ -47,85 +47,37 @@ function formatarCpf(campo, evento){ |
47 | 47 | return false; |
48 | 48 | } |
49 | 49 | return false; |
50 | -} | |
50 | +}; | |
51 | 51 | |
52 | -function validarCpfSemAlert(campo,nome,idElementoMensagemErro){ | |
53 | - //alert("teste"); | |
54 | - cpf = campo.value; | |
55 | - | |
56 | - cpf = cpf.replace(".",""); | |
57 | - cpf = cpf.replace("-",""); | |
58 | - cpf = cpf.replace(".",""); | |
59 | - retorno = true; | |
52 | +function formatarTelefone(campo, evento){ | |
53 | + var codTecla; | |
54 | + var tamanho; | |
55 | + if( document.all ) { // Internet Explorer | |
56 | + codTecla = evento.keyCode; | |
57 | + } else if( document.layers ) { // Nestcape | |
58 | + codTecla = evento.which; | |
59 | + } else if( evento ) { // Firefox | |
60 | + codTecla = evento.which; | |
61 | + } | |
62 | + tamanho = campo.value.length; | |
60 | 63 | |
61 | - if(trim(cpf).length > 0){ | |
62 | - //alert("teste2"); | |
63 | - cpfstr= ''; | |
64 | - temp = cpf + ''; | |
65 | - | |
66 | - cpfstr = temp.substring(0,3); | |
67 | - cpfstr = cpfstr + temp.substring(3,6); | |
68 | - cpfstr = cpfstr + temp.substring(6,9); | |
69 | - cpfstr = cpfstr + temp.substring(9,11); | |
70 | - | |
71 | - | |
72 | - | |
73 | - retorno = false; | |
74 | - if(cpf != null){ | |
75 | - //alert("teste3"); | |
76 | - soma = 0; | |
77 | - digito1 = 0; | |
78 | - digito2 = 0; | |
79 | - for(i = 0; i < 9; i = i + 1) { | |
80 | - soma = soma + ((parseInt(cpf.substring(i,i+1)))*(11-(i+1))); | |
81 | - } | |
82 | - soma = soma % 11; | |
83 | - if (soma == 0 || soma == 1) { | |
84 | - digito1 = 0; | |
85 | - } else { | |
86 | - digito1 = 11 - soma; | |
87 | - } | |
88 | - soma = 0; | |
89 | - | |
90 | - for(i = 0; i < 9; i = i + 1) { | |
91 | - soma = soma + ((parseInt(cpf.substring(i,i+1)))*(12-(i+1))); | |
92 | - } | |
93 | - soma = soma + (digito1*2); | |
94 | - soma = soma % 11; | |
95 | - if (soma == 0 || soma == 1) { | |
96 | - digito2 = 0; | |
97 | - } | |
98 | - else{ | |
99 | - digito2 = 11 - soma; | |
100 | - } | |
101 | - digito = digito1 +''+ digito2; | |
102 | - | |
103 | - | |
104 | - //alert(cpfstr.substring(9,11)); | |
105 | - if(digito == (cpfstr.substring(9,11))){ | |
106 | - retorno = true; | |
107 | - } else{ | |
108 | - //alert("teste4"); | |
109 | - retorno = false; | |
110 | - | |
111 | - } | |
112 | - } else { | |
113 | - retorno = false; | |
64 | + if((codTecla > 47 && codTecla < 58) && tamanho < 14){ | |
65 | + | |
66 | + if(tamanho == 0){ | |
67 | + campo.value = "(" + campo.value; | |
68 | + }else if( tamanho == 3 ){ | |
69 | + campo.value = campo.value + ")"; | |
70 | + }else if(tamanho == 9){ | |
71 | + campo.value = campo.value + "-"; | |
114 | 72 | } |
115 | - }else{ | |
116 | - retorno = false; | |
117 | - } | |
118 | - //alert(retorno); | |
119 | - if(retorno == false){ | |
120 | - //alert('E-mail informado invalido! Por favor, especifique um E-mail válido para o campo \"' + nome + '\".'); | |
121 | - document.getElementById(idElementoMensagemErro).style.display = ''; | |
122 | - campo.focus(); | |
123 | - return false; | |
124 | - }else{ | |
125 | - document.getElementById(idElementoMensagemErro).style.display = 'none'; | |
126 | 73 | return true; |
74 | + } else if(codTecla == 0 || codTecla == 8){ | |
75 | + return true; | |
76 | + } else { | |
77 | + evento.returnValue = false; | |
78 | + return false; | |
127 | 79 | } |
128 | - return retorno; | |
80 | + return false; | |
129 | 81 | } |
130 | 82 | |
131 | 83 | /* | ... | ... |
core/tests.py
... | ... | @@ -165,21 +165,21 @@ class UpdateUserTestCase(TestCase): |
165 | 165 | data = {'username': 'test', 'password': 'testing1'} |
166 | 166 | response = self.client.post(reverse('core:home'), data) |
167 | 167 | self.assertRedirects(response, reverse('app:index')) |
168 | - | |
169 | 168 | |
170 | 169 | data={ |
171 | - 'username': 'testeamadeus', | |
172 | - 'email': 'teste@amadeus.com', | |
170 | + 'username': 'test', | |
171 | + 'birth_date': '12/12/2000', | |
172 | + 'email': 'testing@amadeus.com', | |
173 | + 'cpf': '705.089.884-89', | |
173 | 174 | 'name': 'Teste Amadeus', |
174 | 175 | 'city': 'Praia', |
175 | 176 | 'state': 'PE', |
176 | 177 | 'gender': 'F', |
177 | 178 | } |
178 | - # self.assertRedirects(response1, reverse('app:index')) | |
179 | 179 | response = self.client.get(self.url) |
180 | 180 | self.assertEqual(response.status_code, 200) |
181 | 181 | response = self.client.post(self.url, data) |
182 | - self.assertEqual(response.status_code, 302) | |
182 | + self.assertRedirects(response, reverse('users:profile')) | |
183 | 183 | |
184 | 184 | def test_update_error(self): |
185 | 185 | |
... | ... | @@ -195,12 +195,13 @@ class UpdateUserTestCase(TestCase): |
195 | 195 | 'username': '', |
196 | 196 | 'email': 'teste@amadeus.com', |
197 | 197 | 'name': 'Teste Amadeus', |
198 | + 'birth_date': '12/12/2000', | |
198 | 199 | 'city': 'Praia', |
199 | 200 | 'state': 'PE', |
200 | 201 | 'gender': 'F', |
201 | 202 | } |
202 | 203 | response = self.client.post(self.url, data) |
203 | - self.assertFormError(response, 'form', 'username', 'This field is required.') | |
204 | + self.assertFormError(response, 'form', 'username', _('This field is required.')) | |
204 | 205 | |
205 | 206 | |
206 | 207 | class DeleteUserTestCase(TestCase): | ... | ... |
users/forms.py
1 | 1 | # coding=utf-8 |
2 | -import os | |
2 | +import os, re | |
3 | +from datetime import date | |
4 | +from pycpfcnpj import cpfcnpj | |
3 | 5 | from django.conf import settings |
4 | 6 | from django import forms |
5 | 7 | from django.utils.translation import ugettext_lazy as _ |
... | ... | @@ -8,24 +10,6 @@ from django.contrib.auth.forms import UserCreationForm |
8 | 10 | from core.forms import RegisterUserForm |
9 | 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 | 13 | class AdminUserForm(forms.ModelForm): |
30 | 14 | def save(self, commit=True): |
31 | 15 | super(AdminUserForm, self).save(commit=False) |
... | ... | @@ -57,16 +41,33 @@ class UserForm(RegisterUserForm): |
57 | 41 | model = User |
58 | 42 | fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active'] |
59 | 43 | |
60 | -class EditUserForm(forms.ModelForm): | |
44 | +class UpdateUserForm(forms.ModelForm): | |
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_cpf(self): | |
54 | + cpf = self.cleaned_data['cpf'] | |
55 | + if not self.validate_cpf(cpf): | |
56 | + raise forms.ValidationError(_('Please enter a valid CPF')) | |
57 | + return cpf | |
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 | |
61 | 64 | |
62 | 65 | class Meta: |
63 | 66 | model = User |
64 | - fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'cpf', 'phone', 'image'] | |
67 | + fields = ['username', 'name', 'email', 'city', 'state', 'birth_date', 'gender', 'type_profile', 'cpf', 'phone', 'image', 'is_staff', 'is_active'] | |
65 | 68 | |
66 | -# Ailson | |
67 | -class UpdateUserForm(forms.ModelForm): | |
68 | - company_logo = forms.ImageField(label=_('Company Logo'),required=False, error_messages = {'invalid':_("Image files only")}) | |
69 | +class UpdateProfileForm(UpdateUserForm): | |
69 | 70 | |
70 | 71 | class Meta: |
71 | 72 | model = User |
72 | - fields = ['username', 'name', 'email', 'city', 'state', 'birth_date', 'gender', 'cpf', 'phone', 'image'] | |
73 | 73 | \ No newline at end of file |
74 | + fields = ['username', 'name', 'email', 'birth_date', 'city', 'state', 'gender', 'cpf', 'phone', 'image'] | ... | ... |
users/templates/users/edit_profile.html
... | ... | @@ -31,7 +31,7 @@ |
31 | 31 | <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> |
32 | 32 | <label for="{{ field.auto_id }}">{{ field.label }}</label> |
33 | 33 | {% if field.auto_id == 'id_birth_date' %} |
34 | - <input type="date" class="form-control"name="{{field.name}}" value="{% if field.value.year %}{{field.value|date:'Y-m-d'}}{% else %}{{field.value}}{% endif %}" min="{{now|date:'Y-m-d'}}" id="{{ field.auto_id }}"> | |
34 | + <input type="text" class="form-control date-picker"name="{{field.name}}" value="{% if field.value.year %}{{field.value|date:'Y-m-d'}}{% else %}{{field.value}}{% endif %}" min="{{now|date:'Y-m-d'}}" id="{{ field.auto_id }}"> | |
35 | 35 | <span id="helpBlock" class="help-block">{{ field.help_text }}</span> |
36 | 36 | {% elif field.auto_id == 'id_image' %} |
37 | 37 | {% render_field field class='form-control input-sm' %} |
... | ... | @@ -49,11 +49,16 @@ |
49 | 49 | {% render_field field type='checkbox' %} |
50 | 50 | </label> |
51 | 51 | </div> |
52 | + {% elif field.auto_id == 'id_cpf' %} | |
53 | + {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarCpf(this,event);' %} | |
54 | + | |
55 | + {% elif field.auto_id == 'id_phone' %} | |
56 | + {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarTelefone(this,event);' %} | |
52 | 57 | {% else %} |
53 | 58 | {% render_field field class='form-control' %} |
54 | 59 | <span id="helpBlock" class="help-block">{{ field.help_text }}</span> |
55 | 60 | {% endif %} |
56 | - {% if field.errors.length > 0 %} | |
61 | + {% if field.errors %} | |
57 | 62 | <div class="alert alert-danger alert-dismissible" role="alert"> |
58 | 63 | <button type="button" class="close" data-dismiss="alert" aria-label="Close"> |
59 | 64 | <span aria-hidden="true">×</span> |
... | ... | @@ -64,7 +69,6 @@ |
64 | 69 | {% endfor %} |
65 | 70 | </ul> |
66 | 71 | </div> |
67 | - </div> | |
68 | 72 | {% endif %} |
69 | 73 | </div> |
70 | 74 | {% endfor %} | ... | ... |
users/templates/users/profile.html
... | ... | @@ -27,6 +27,17 @@ |
27 | 27 | {% endblock %} |
28 | 28 | |
29 | 29 | {% block content %} |
30 | + {% if messages %} | |
31 | + {% for message in messages %} | |
32 | + <div class="alert alert-success alert-dismissible" role="alert"> | |
33 | + <button type="button" class="close" data-dismiss="alert" aria-label="Close"> | |
34 | + <span aria-hidden="true">×</span> | |
35 | + </button> | |
36 | + <p>{{ message }}</p> | |
37 | + </div> | |
38 | + {% endfor %} | |
39 | + {% endif %} | |
40 | + | |
30 | 41 | <div class="row"> |
31 | 42 | <div class="col-lg-offset-4 col-lg-2"> |
32 | 43 | <img src="" class="img-responsive center-block " alt="logo amadeus"> | ... | ... |
users/templates/users/update.html
... | ... | @@ -34,7 +34,7 @@ |
34 | 34 | <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput"> |
35 | 35 | <label for="{{ field.auto_id }}">{{ field.label }}</label> |
36 | 36 | {% if field.auto_id == 'id_birth_date' %} |
37 | - <input type="date" class="form-control"name="{{field.name}}" value="{% if field.value.year %}{{field.value|date:'Y-m-d'}}{% else %}{{field.value}}{% endif %}" min="{{now|date:'Y-m-d'}}" id="{{ field.auto_id }}"> | |
37 | + <input type="text" class="form-control date-picker"name="{{field.name}}" value="{% if field.value.year %}{{field.value|date:'Y-m-d'}}{% else %}{{field.value}}{% endif %}" min="{{now|date:'Y-m-d'}}" id="{{ field.auto_id }}"> | |
38 | 38 | <span id="helpBlock" class="help-block">{{ field.help_text }}</span> |
39 | 39 | {% elif field.auto_id == 'id_image' %} |
40 | 40 | {% render_field field class='form-control input-sm' %} |
... | ... | @@ -48,6 +48,16 @@ |
48 | 48 | </div> |
49 | 49 | {% elif field.auto_id == 'id_cpf' %} |
50 | 50 | {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarCpf(this,event);' %} |
51 | + | |
52 | + {% elif field.auto_id == 'id_phone' %} | |
53 | + {% render_field field class='form-control' onkeypress='campoNumerico(this,event); formatarTelefone(this,event);' %} | |
54 | + | |
55 | + {% elif field.auto_id == 'id_is_staff' or field.auto_id == 'id_is_active' %} | |
56 | + <div class="checkbox"> | |
57 | + <label for="{{ field.auto_id }}"> | |
58 | + {% render_field field %}<span class="checkbox-material"><span class="check"></span></span> {{field.label}} | |
59 | + </label> | |
60 | + </div> | |
51 | 61 | {% else %} |
52 | 62 | {% render_field field class='form-control' %} |
53 | 63 | <span id="helpBlock" class="help-block">{{ field.help_text }}</span> | ... | ... |
users/urls.py
... | ... | @@ -11,6 +11,6 @@ urlpatterns = [ |
11 | 11 | url(r'^profile/$', views.Profile.as_view(), name='profile'), |
12 | 12 | url(r'^profile/editar/(?P<username>[\w_-]+)/$', views.EditProfile.as_view(), name='edit_profile'), |
13 | 13 | # |
14 | - url(r'^profile/update/$', views.UpdateUser.as_view(), name='update_profile'), | |
14 | + url(r'^profile/update/$', views.UpdateProfile.as_view(), name='update_profile'), | |
15 | 15 | url(r'^profile/delete/$', views.DeleteUser.as_view(), name='delete_profile'), |
16 | 16 | ] | ... | ... |
users/views.py
... | ... | @@ -8,7 +8,7 @@ from django.core.urlresolvers import reverse_lazy |
8 | 8 | from django.utils.translation import ugettext_lazy as _ |
9 | 9 | from rolepermissions.shortcuts import assign_role |
10 | 10 | from .models import User |
11 | -from .forms import UserForm, ProfileForm, UpdateUserForm | |
11 | +from .forms import UserForm, UpdateProfileForm, UpdateUserForm | |
12 | 12 | |
13 | 13 | # ================ ADMIN ======================= |
14 | 14 | class UsersListView(HasRoleMixin, LoginRequiredMixin, generic.ListView): |
... | ... | @@ -103,13 +103,13 @@ def delete(request,username): |
103 | 103 | |
104 | 104 | |
105 | 105 | |
106 | -class UpdateUser(LoginRequiredMixin, generic.edit.UpdateView): | |
106 | +class UpdateProfile(LoginRequiredMixin, generic.edit.UpdateView): | |
107 | 107 | |
108 | 108 | allowed_roles = ['student'] |
109 | 109 | login_url = reverse_lazy("core:home") |
110 | 110 | template_name = 'users/edit_profile.html' |
111 | - form_class = UpdateUserForm | |
112 | - success_url = reverse_lazy('users:update_profile') | |
111 | + form_class = UpdateProfileForm | |
112 | + success_url = reverse_lazy('users:profile') | |
113 | 113 | |
114 | 114 | def get_object(self): |
115 | 115 | user = get_object_or_404(User, username = self.request.user.username) |
... | ... | @@ -119,7 +119,7 @@ class UpdateUser(LoginRequiredMixin, generic.edit.UpdateView): |
119 | 119 | form.save() |
120 | 120 | messages.success(self.request, _('Profile edited successfully!')) |
121 | 121 | |
122 | - return super(UpdateUser, self).form_valid(form) | |
122 | + return super(UpdateProfile, self).form_valid(form) | |
123 | 123 | |
124 | 124 | class DeleteUser(LoginRequiredMixin, generic.edit.DeleteView): |
125 | 125 | allowed_roles = ['student'] | ... | ... |