From b639c04549552768953246fac1329602260e589a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sat, 11 Feb 2017 00:21:11 +0100 Subject: [PATCH] Fix the registration forms - The former `RegistrationUserProfileForm` is splitted in two. - There is a new form: `RegistrationCofProfileForm` --- cof/forms.py | 50 +++++++++++++++------------- cof/templates/registration_form.html | 1 + cof/views.py | 46 +++++++++++++++++-------- gestion/forms.py | 5 +-- 4 files changed, 60 insertions(+), 42 deletions(-) diff --git a/cof/forms.py b/cof/forms.py index d73e66c7..cf6652c7 100644 --- a/cof/forms.py +++ b/cof/forms.py @@ -17,6 +17,8 @@ from .models import CofProfile, EventCommentValue, \ from .widgets import TriStateCheckbox from .shared import lock_table, unlock_table +from gestion.models import Profile + from bda.models import Spectacle @@ -216,30 +218,21 @@ class RegistrationPassUserForm(RegistrationUserForm): return user -class RegistrationProfileForm(forms.ModelForm): - def __init__(self, *args, **kw): - super(RegistrationProfileForm, self).__init__(*args, **kw) - self.fields['mailing_cof'].initial = True - self.fields['mailing_bda'].initial = True - self.fields['mailing_bda_revente'].initial = True +class RegistrationCofProfileForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super(RegistrationCofProfileForm, self).__init__(*args, **kwargs) self.fields['num'].widget.attrs['readonly'] = True - self.fields.keyOrder = [ - 'login_clipper', - 'phone', - 'occupation', - 'departement', - 'is_cof', - 'num', - 'type_cotiz', - 'mailing_cof', - 'mailing_bda', - 'mailing_bda_revente', - 'comments' - ] + class Meta: + model = CofProfile + fields = [ + "num", "type_cotiz", "is_cof", "is_buro", + "mailing", "mailing_bda", "mailing_bda_revente", + "petits_cours_accept", "petits_cours_remarques" + ] def save(self, *args, **kw): - instance = super(RegistrationProfileForm, self).save(*args, **kw) + instance = super(RegistrationCofProfileForm, self).save(*args, **kw) if instance.is_cof and not instance.num: # Generate new number try: @@ -253,11 +246,20 @@ class RegistrationProfileForm(forms.ModelForm): unlock_table(CofProfile) return instance + +class RegistrationProfileForm(forms.ModelForm): class Meta: - model = CofProfile - fields = ("login_clipper", "num", "phone", "occupation", - "departement", "is_cof", "type_cotiz", "mailing_cof", - "mailing_bda", "mailing_bda_revente", "comments") + model = Profile + fields = [ + "login_clipper", "phone", "occupation", "departement", "comments" + ] + + +class RegistrationUserForm(forms.ModelForm): + class Meta: + model = User + fields = ["first_name", "last_name"] + STATUS_CHOICES = (('no', 'Non'), ('wait', 'Oui mais attente paiement'), diff --git a/cof/templates/registration_form.html b/cof/templates/registration_form.html index 6a798cc1..97bc375c 100644 --- a/cof/templates/registration_form.html +++ b/cof/templates/registration_form.html @@ -15,6 +15,7 @@
+ {{ cofprofile_form | bootstrap }} {{ clubs_form | bootstrap }}
{{ event_formset.management_form }} diff --git a/cof/views.py b/cof/views.py index e66f6094..8c5398c2 100644 --- a/cof/views.py +++ b/cof/views.py @@ -27,13 +27,16 @@ from .models import EventCommentField, EventCommentValue, \ from .shared import send_custom_mail from .models import CofProfile, Clipper, Club from .decorators import buro_required, cof_required -from .forms import UserProfileForm, EventStatusFilterForm, \ - SurveyForm, SurveyStatusFilterForm, RegistrationUserForm, \ - RegistrationProfileForm, EventForm, CalendarForm, EventFormset, \ - RegistrationPassUserForm, ClubsForm +from .forms import ( + EventStatusFilterForm, SurveyForm, SurveyStatusFilterForm, + RegistrationUserForm, RegistrationProfileForm, RegistrationCofProfileForm, + EventForm, CalendarForm, EventFormset, RegistrationPassUserForm, ClubsForm +) from bda.models import Tirage, Spectacle +from gestion.models import Profile + @login_required def home(request): @@ -331,16 +334,20 @@ def registration_form2(request, login_clipper=None, username=None): # profile profile_form = RegistrationProfileForm(initial={ 'login_clipper': login_clipper}) + # COF - profile + cofprofile_form = RegistrationCofProfileForm() registration_set_ro_fields(user_form, profile_form) # events & clubs event_formset = EventFormset(events=events, prefix='events') clubs_form = ClubsForm() if username: member = get_object_or_404(User, username=username) - (profile, _) = CofProfile.objects.get_or_create(user=member) + (profile, _) = Profile.objects.get_or_create(user=member) + (cofprofile, _) = CofProfile.objects.get_or_create(profile=profile) # already existing, prefill user_form = RegistrationUserForm(instance=member) profile_form = RegistrationProfileForm(instance=profile) + cofprofile_form = RegistrationCofProfileForm(instance=cofprofile) registration_set_ro_fields(user_form, profile_form) # events current_registrations = [] @@ -360,12 +367,14 @@ def registration_form2(request, login_clipper=None, username=None): user_form = RegistrationPassUserForm() user_form.force_long_username() profile_form = RegistrationProfileForm() + cofprofile_form = RegistrationCofProfileForm() event_formset = EventFormset(events=events, prefix='events') clubs_form = ClubsForm() return render(request, "registration_form.html", {"member": member, "login_clipper": login_clipper, "user_form": user_form, "profile_form": profile_form, + "cofprofile_form": cofprofile_form, "event_formset": event_formset, "clubs_form": clubs_form}) @@ -414,17 +423,25 @@ def registration(request): if user_form.is_valid(): member = user_form.save() - profile, _ = CofProfile.objects.get_or_create(user=member) - was_cof = profile.is_cof - request_dict["num"] = profile.num + cofprofile, _ = CofProfile.objects.get_or_create(user=member) + was_cof = cofprofile.is_cof + request_dict["num"] = cofprofile.num # Maintenant on remplit le formulaire de profil - profile_form = RegistrationProfileForm(request_dict, - instance=profile) - if (profile_form.is_valid() and event_formset.is_valid() - and clubs_form.is_valid()): + cofprofile_form = RegistrationCofProfileForm( + request_dict, + instance=cofprofile + ) + forms_are_valid = all( + profile_form.is_valid(), + cofprofile_form.is_valid(), + event_formset.is_valid(), + clubs_form.is_valid() + ) + if forms_are_valid: # Enregistrement du profil - profile = profile_form.save() - if profile.is_cof and not was_cof: + profile_form.save() + cofprofile = cofprofile_form.save() + if cofprofile.is_cof and not was_cof: send_custom_mail(member, "bienvenue") # Enregistrement des inscriptions aux événements for form in event_formset: @@ -469,6 +486,7 @@ def registration(request): {"success": success, "user_form": user_form, "profile_form": profile_form, + "cofprofile_form": cofprofile_form, "member": member, "login_clipper": login_clipper, "event_formset": event_formset, diff --git a/gestion/forms.py b/gestion/forms.py index 7472f818..7584d29e 100644 --- a/gestion/forms.py +++ b/gestion/forms.py @@ -10,10 +10,7 @@ class UserForm(forms.ModelForm): fields = ["first_name", "last_name"] - class ProfileForm(forms.ModelForm): class Meta: - model = CofProfile + model = Profile fields = ["phone", "departement"] - -