From 5b0b60fadb7a15d642831540b9c9ab7e2232e0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 17 Aug 2016 15:34:01 +0200 Subject: [PATCH] =?UTF-8?q?Meilleure=20inscription=20des=20ext=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lors de la création d'un compte exté via la vue `/registration` (i.e. compte non associé à un clipper), deux champs sont ajoutés au formulaire pour la création d'un mot de passe. Il est toujours possible de changer ce mot de passe via l'admin s'il est perdu par l'utilisateur. --- gestioncof/forms.py | 29 ++++++++++++++++++++++++++++- gestioncof/views.py | 25 ++++++++++++------------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/gestioncof/forms.py b/gestioncof/forms.py index 7aac9e5f..5fb3d0b3 100644 --- a/gestioncof/forms.py +++ b/gestioncof/forms.py @@ -8,7 +8,7 @@ from django import forms from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User from django.forms.widgets import RadioSelect, CheckboxSelectMultiple -from django.forms.formsets import BaseFormSet +from django.forms.formsets import BaseFormSet, formset_factory from django.db.models import Max from gestioncof.models import CofProfile, EventCommentValue, \ @@ -215,6 +215,32 @@ class RegistrationUserForm(forms.ModelForm): fields = ("username", "first_name", "last_name", "email") +class RegistrationPassUserForm(RegistrationUserForm): + """ + Formulaire pour changer le mot de passe d'un utilisateur. + """ + password1 = forms.CharField(label=_('Mot de passe'), + widget=forms.PasswordInput) + password2 = forms.CharField(label=_('Confirmation du mot de passe'), + widget=forms.PasswordInput) + + def clean_password2(self): + pass1 = self.cleaned_data['password1'] + pass2 = self.cleaned_data['password2'] + if pass1 and pass2: + if pass1 != pass2: + raise forms.ValidationError(_('Mots de passe non identiques.')) + return pass2 + + def save(self, commit=True, *args, **kwargs): + user = super(RegistrationPassUserForm, self).save(commit, *args, + **kwargs) + user.set_password(self.cleaned_data['password2']) + if commit: + user.save() + return user + + class RegistrationProfileForm(forms.ModelForm): def __init__(self, *args, **kw): super(RegistrationProfileForm, self).__init__(*args, **kw) @@ -350,6 +376,7 @@ class BaseEventRegistrationFormset(BaseFormSet): kwargs['current_registration'] = self.current_registrations[index] return super(BaseEventRegistrationFormset, self)._construct_form( index, **kwargs) +EventFormset = formset_factory(AdminEventForm, BaseEventRegistrationFormset) class CalendarForm(forms.ModelForm): diff --git a/gestioncof/views.py b/gestioncof/views.py index 4ba41061..5a665860 100644 --- a/gestioncof/views.py +++ b/gestioncof/views.py @@ -14,7 +14,6 @@ from django.http import Http404, HttpResponse from django.contrib.auth.decorators import login_required from django.contrib.auth.views import login as django_login_view from django.contrib.auth.models import User -from django.forms.models import formset_factory from django.utils import timezone import django.utils.six as six @@ -29,8 +28,8 @@ from gestioncof.models import CofProfile, Clipper from gestioncof.decorators import buro_required, cof_required from gestioncof.forms import UserProfileForm, EventStatusFilterForm, \ SurveyForm, SurveyStatusFilterForm, RegistrationUserForm, \ - RegistrationProfileForm, AdminEventForm, EventForm, CalendarForm, \ - BaseEventRegistrationFormset + RegistrationProfileForm, EventForm, CalendarForm, EventFormset, \ + RegistrationPassUserForm from bda.models import Tirage, Spectacle @@ -320,8 +319,6 @@ def registration_set_ro_fields(user_form, profile_form): @buro_required def registration_form2(request, login_clipper=None, username=None): events = Event.objects.filter(old=False).all() - EventFormset = formset_factory(AdminEventForm, - BaseEventRegistrationFormset) member = None if login_clipper: clipper = get_object_or_404(Clipper, username=login_clipper) @@ -366,7 +363,7 @@ def registration_form2(request, login_clipper=None, username=None): current_registrations=current_registrations) elif not login_clipper: # new user - user_form = RegistrationUserForm() + user_form = RegistrationPassUserForm() profile_form = RegistrationProfileForm() event_formset = EventFormset(events=events, prefix='events') return render(request, "registration_form.html", @@ -379,21 +376,23 @@ def registration_form2(request, login_clipper=None, username=None): def registration(request): if request.POST: request_dict = request.POST.copy() - member = None - login_clipper = None - success = False - events = Event.objects.filter(old=False).all() # num ne peut pas être défini manuellement if "num" in request_dict: del request_dict["num"] + member = None + login_clipper = None + success = False # ----- # Remplissage des formulaires # ----- - user_form = RegistrationUserForm(request_dict) - EventFormset = formset_factory(AdminEventForm, - BaseEventRegistrationFormset) + if 'password1' in request_dict or 'password2' in request_dict: + user_form = RegistrationPassUserForm(request_dict) + else: + user_form = RegistrationUserForm + profile_form = RegistrationProfileForm(request_dict) + events = Event.objects.filter(old=False).all() event_formset = EventFormset(events=events, data=request_dict, prefix='events') if "user_exists" in request_dict and request_dict["user_exists"]: