diff --git a/amadeus/templates/base.html b/amadeus/templates/base.html index 3380838..92e4730 100644 --- a/amadeus/templates/base.html +++ b/amadeus/templates/base.html @@ -118,7 +118,7 @@ diff --git a/users/forms.py b/users/forms.py index 2fa24ed..75600af 100644 --- a/users/forms.py +++ b/users/forms.py @@ -2,6 +2,7 @@ from django import forms from django.utils.translation import ugettext_lazy as _ from rolepermissions.shortcuts import assign_role +from django.contrib.auth import update_session_auth_hash from .models import User class Validation(forms.ModelForm): @@ -14,49 +15,57 @@ class Validation(forms.ModelForm): if image: if hasattr(image, '_size'): if image._size > self.MAX_UPLOAD_SIZE: - raise forms.ValidationError(_("The image is too large. It should have less than 2MB.")) + self._errors['image'] = [_("The image is too large. It should have less than 2MB.")] - return image + return ValueError - def clean_password(self): - password = self.cleaned_data.get('password') + return image + def clean_new_password(self): + password = self.cleaned_data.get('new_password') + print(self.cleaned_data) if self.is_edit and len(password) == 0: return password # At least MIN_LENGTH long if len(password) < self.MIN_PASS_LENGTH: - raise forms.ValidationError(_("The password must contain at least % d characters." % self.MIN_PASS_LENGTH)) + self._errors['new_password'] = [_("The new password must contain at least % d characters." % self.MIN_PASS_LENGTH)] + + return ValueError # 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.')) + self._errors['new_password'] = [_('The password must contain at least one letter and at least one digit or a punctuation character.')] + + return ValueError return password def clean_password2(self): - password = self.cleaned_data.get("password") + password = self.cleaned_data.get("new_password") password2 = self.cleaned_data.get("password2") if self.is_edit and len(password) == 0: return password2 if password and password2 and password != password2: - raise forms.ValidationError(_('The confirmation password is incorrect.')) + self._errors['password2'] = [_('The confirmation password is incorrect.')] + + return ValueError return password2 class RegisterUserForm(Validation): - password = forms.CharField(label=_('Password'), widget = forms.PasswordInput) - password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput) + new_password = forms.CharField(label=_('Password'), widget = forms.PasswordInput(render_value=True)) + password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput(render_value=True)) is_edit = False def save(self, commit=True): super(RegisterUserForm, self).save(commit=False) - self.instance.set_password(self.cleaned_data['password']) + self.instance.set_password(self.cleaned_data['new_password']) self.instance.save() @@ -94,19 +103,19 @@ class UserForm(Validation): self.is_edit = is_update if self.is_edit: - del self.fields['password'] + del self.fields['new_password'] del self.fields['password2'] if not is_edit: - password = forms.CharField(label=_('Password'), widget = forms.PasswordInput, required = False) - password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput, required = False) + new_password = forms.CharField(label=_('Password'), widget = forms.PasswordInput(render_value=True), required = False) + password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput(render_value=True), required = False) def save(self, commit=True): super(UserForm, self).save(commit=False) if not self.is_edit: - self.instance.set_password(self.cleaned_data['password']) + self.instance.set_password(self.cleaned_data['new_password']) self.instance.save() @@ -117,4 +126,46 @@ class UserForm(Validation): fields = ['email', 'username', 'last_name', 'social_name', 'description', 'show_email', 'image', 'is_staff', 'is_active'] widgets = { 'description': forms.Textarea, - } \ No newline at end of file + } + +class ChangePassForm(Validation): + def __init__(self, *args, **kwargs): + self.user = kwargs.pop('user', None) + self.request = kwargs.pop('request', None) + super(ChangePassForm, self).__init__(*args, **kwargs) + + is_edit = False + + new_password = forms.CharField(label=_('New Password'), widget = forms.PasswordInput(render_value=True), required = True) + password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput(render_value=True), required = True) + + def clean_password(self): + password = self.cleaned_data.get('password', None) + + if not self.user.check_password(password): + self._errors['password'] = [_('The value inputed does not match with your actual password.')] + + return ValueError + + return password + + def save(self, commit=True): + super(ChangePassForm, self).save(commit=False) + + self.instance.set_password(self.cleaned_data['new_password']) + + update_session_auth_hash(self.request, self.instance) + + self.instance.save() + + return self.instance + + class Meta: + model = User + fields = ['password'] + labels = { + 'password': _('Actual Password') + } + widgets = { + 'password': forms.PasswordInput + } \ No newline at end of file diff --git a/users/templates/users/_form.html b/users/templates/users/_form.html index 6b1aad4..41865e8 100644 --- a/users/templates/users/_form.html +++ b/users/templates/users/_form.html @@ -7,7 +7,7 @@
{% if field.auto_id == 'id_image' %} {% if field.field.required %} - + {% else %} {% endif %} @@ -22,7 +22,7 @@
{% elif field.auto_id == 'id_description' %} {% if field.field.required %} - + {% else %} {% endif %} @@ -35,7 +35,7 @@ {% else %} {% if field.field.required %} - + {% else %} {% endif %} @@ -56,7 +56,12 @@ {% endif %} {% endfor %} -
- +
+
+ +
+
\ No newline at end of file diff --git a/users/templates/users/change_password.html b/users/templates/users/change_password.html index b0c0edd..ada604d 100644 --- a/users/templates/users/change_password.html +++ b/users/templates/users/change_password.html @@ -5,53 +5,16 @@ {% load django_bootstrap_breadcrumbs %} {% block breadcrumbs %} - - {{ block.super }} - {% breadcrumb 'Edit' 'users:update_profile' %} - + {{ block.super }} + {% breadcrumb 'Change Password' 'users:change_pass' %} {% endblock %} {% block content %} - {% if messages %} - {% for message in messages %} - - {% endfor %} - {% endif %} -
-
-
-
- {% csrf_token %} -
- - -
-
- - -
-
- - -
-
-
- +
+
+
+ {% include "users/_form.html" with back_url="users:profile" %}
- -
- -
-
+
- - -
{% endblock %} diff --git a/users/templates/users/create.html b/users/templates/users/create.html index 4d40956..92dae0c 100644 --- a/users/templates/users/create.html +++ b/users/templates/users/create.html @@ -11,7 +11,7 @@
- {% include 'users/_form.html' %} + {% include 'users/_form.html' with back_url="users:manage" %}
diff --git a/users/templates/users/edit_profile.html b/users/templates/users/edit_profile.html index 9a58bee..2f1dffb 100644 --- a/users/templates/users/edit_profile.html +++ b/users/templates/users/edit_profile.html @@ -12,62 +12,13 @@ {% block content %} - {% if messages %} - {% for message in messages %} - - {% endfor %} - {% endif %} -
-
-
-
- {% csrf_token %} - {% for field in form %} -
- - {% if field.auto_id == 'id_image' %} - {% render_field field class='form-control input-sm' %} -
- - - - -
- {% elif field.auto_id == 'id_description' %} - {% render_field field class='form-control text_wysiwyg' %} - {% else %} - {% render_field field class='form-control' %} - {{ field.help_text }} - {% endif %} - {% if field.errors %} - - {% endif %} -
- {% endfor %} -
- -
- -
-
-
-
+
+
+
+ {% include "users/_form.html" with back_url="users:profile" %} +
+
+
+
+
{% endblock %} diff --git a/users/templates/users/update.html b/users/templates/users/update.html index 13ef811..11deb91 100644 --- a/users/templates/users/update.html +++ b/users/templates/users/update.html @@ -14,21 +14,10 @@ {% block content %} - {% if messages %} - {% for message in messages %} - - {% endfor %} - {% endif %} -
- {% include 'users/_form.html' %} + {% include 'users/_form.html' with back_url="users:manage" %}
diff --git a/users/urls.py b/users/urls.py index 4806990..1e8e3e6 100644 --- a/users/urls.py +++ b/users/urls.py @@ -12,4 +12,5 @@ urlpatterns = [ url(r'^create/$', views.CreateView.as_view(), name = 'create'), url(r'^profile/$', views.Profile.as_view(), name = 'profile'), url(r'^edit_profile/$', views.UpdateProfile.as_view(), name = 'edit_profile'), + url(r'^change_pass/$', views.ChangePassView.as_view(), name='change_pass'), ] diff --git a/users/views.py b/users/views.py index aff575c..3288bc8 100644 --- a/users/views.py +++ b/users/views.py @@ -14,7 +14,7 @@ from itertools import chain from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from .models import User -from .forms import RegisterUserForm, ProfileForm, UserForm +from .forms import RegisterUserForm, ProfileForm, UserForm, ChangePassForm #API IMPORTS from rest_framework import viewsets @@ -179,6 +179,44 @@ class UpdateView(LoginRequiredMixin, generic.UpdateView): return context +class ChangePassView(LoginRequiredMixin, generic.UpdateView): + login_url = reverse_lazy("users:login") + redirect_field_name = 'next' + + template_name = 'users/change_password.html' + slug_field = 'email' + slug_url_kwarg = 'email' + context_object_name = 'acc' + model = User + form_class = ChangePassForm + success_url = reverse_lazy('users:profile') + + def get_form_kwargs(self): + kwargs = super(ChangePassView, self).get_form_kwargs() + + kwargs.update({'user': self.request.user}) + kwargs.update({'request': self.request}) + + return kwargs + + def get_object(self): + user = get_object_or_404(User, email = self.request.user.email) + + return user + + def form_valid(self, form): + form.save() + + messages.success(self.request, _('Password changed successfully!')) + + return super(ChangePassView, self).form_valid(form) + + def get_context_data (self, **kwargs): + context = super(ChangePassView, self).get_context_data(**kwargs) + context['title'] = _("Change Password") + + return context + class Profile(LoginRequiredMixin, generic.DetailView): login_url = reverse_lazy("users:login") redirect_field_name = 'next' @@ -187,7 +225,7 @@ class Profile(LoginRequiredMixin, generic.DetailView): template_name = 'users/profile.html' def get_object(self): - user = get_object_or_404(User, username = self.request.user.username) + user = get_object_or_404(User, email = self.request.user.email) return user @@ -254,12 +292,12 @@ def login(request): user = authenticate(username=username, password=password) if user is not None: login_user(request, user) - return redirect(reverse("users:login")) + return redirect(reverse("home")) else: messages.add_message(request, messages.ERROR, _('E-mail or password are incorrect.')) context["username"] = username elif request.user.is_authenticated: - return redirect('home') + return redirect(reverse('home')) return render(request,"users/login.html",context) -- libgit2 0.21.2