From 58d708b79152b50965a67a1b8dcf93af0083aecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Fri, 10 Feb 2017 23:12:12 +0100 Subject: [PATCH 1/2] Move profile editing to `gestion` --- cof/forms.py | 21 ------------------ cof/views.py | 13 ----------- gestioCOF/urls.py | 3 ++- gestion/forms.py | 19 ++++++++++++++++ .../templates/gestion}/profile.html | 0 gestion/urls.py | 10 +++++++++ gestion/views.py | 22 ++++++++++++++++++- 7 files changed, 52 insertions(+), 36 deletions(-) create mode 100644 gestion/forms.py rename {cof/templates => gestion/templates/gestion}/profile.html (100%) create mode 100644 gestion/urls.py diff --git a/cof/forms.py b/cof/forms.py index 9c340053..d73e66c7 100644 --- a/cof/forms.py +++ b/cof/forms.py @@ -177,27 +177,6 @@ class EventStatusFilterForm(forms.Form): yield ("has_paid", None, value) -class UserProfileForm(forms.ModelForm): - first_name = forms.CharField(label=_('Prénom'), max_length=30) - last_name = forms.CharField(label=_('Nom'), max_length=30) - - def __init__(self, *args, **kw): - super(UserProfileForm, self).__init__(*args, **kw) - self.fields['first_name'].initial = self.instance.user.first_name - self.fields['last_name'].initial = self.instance.user.last_name - - def save(self, *args, **kw): - super(UserProfileForm, self).save(*args, **kw) - self.instance.user.first_name = self.cleaned_data.get('first_name') - self.instance.user.last_name = self.cleaned_data.get('last_name') - self.instance.user.save() - - class Meta: - model = CofProfile - fields = ["first_name", "last_name", "phone", "mailing_cof", - "mailing_bda", "mailing_bda_revente"] - - class RegistrationUserForm(forms.ModelForm): def __init__(self, *args, **kw): super(RegistrationUserForm, self).__init__(*args, **kw) diff --git a/cof/views.py b/cof/views.py index 5958e732..e66f6094 100644 --- a/cof/views.py +++ b/cof/views.py @@ -302,19 +302,6 @@ def survey_status(request, survey_id): "form": form}) -@cof_required -def profile(request): - success = False - if request.method == "POST": - form = UserProfileForm(request.POST, instance=request.user.profile) - if form.is_valid(): - form.save() - success = True - else: - form = UserProfileForm(instance=request.user.profile) - return render(request, "profile.html", {"form": form, "success": success}) - - def registration_set_ro_fields(user_form, profile_form): user_form.fields['username'].widget.attrs['readonly'] = True profile_form.fields['login_clipper'].widget.attrs['readonly'] = True diff --git a/gestioCOF/urls.py b/gestioCOF/urls.py index 08418f1f..d786e497 100644 --- a/gestioCOF/urls.py +++ b/gestioCOF/urls.py @@ -30,6 +30,8 @@ admin.autodiscover() urlpatterns = [ # Page d'accueil url(r'^$', cof_views.home, name='home'), + # The common views + url(r"^$", include("gestion.urls")), # Le BdA url(r'^bda/', include('bda.urls')), # Les exports @@ -54,7 +56,6 @@ urlpatterns = [ url(r'^login$', cof_views.login, name="cof-login"), url(r'^logout$', cof_views.logout), # Infos persos - url(r'^profile$', cof_views.profile), url(r'^outsider/password-change$', django_views.password_change), url(r'^outsider/password-change-done$', django_views.password_change_done, diff --git a/gestion/forms.py b/gestion/forms.py new file mode 100644 index 00000000..7472f818 --- /dev/null +++ b/gestion/forms.py @@ -0,0 +1,19 @@ +from django import forms +from django.contrib.auth.models import User + +from .models import Profile + + +class UserForm(forms.ModelForm): + class Meta: + model = User + fields = ["first_name", "last_name"] + + + +class ProfileForm(forms.ModelForm): + class Meta: + model = CofProfile + fields = ["phone", "departement"] + + diff --git a/cof/templates/profile.html b/gestion/templates/gestion/profile.html similarity index 100% rename from cof/templates/profile.html rename to gestion/templates/gestion/profile.html diff --git a/gestion/urls.py b/gestion/urls.py new file mode 100644 index 00000000..a5075985 --- /dev/null +++ b/gestion/urls.py @@ -0,0 +1,10 @@ +from django.conf.urls import url + +from . import views + + +app_name = "gestion" + +urlpatterns = [ + url(r"^profile$", views.profile, name="profile"), +] diff --git a/gestion/views.py b/gestion/views.py index 91ea44a2..698d7886 100644 --- a/gestion/views.py +++ b/gestion/views.py @@ -1,3 +1,23 @@ from django.shortcuts import render +from django.contrib.auth.decorators import login_required -# Create your views here. +from .forms import ProfileForm, UserForm + + +@login_required +def profile(request): + success = False + user = request.user + if request.method == "POST": + user_form = UserForm(request.POST, instance=user) + profile_form = ProfileForm(request.POST, instance=user.profile) + if all(user_form.is_valid(), profile_form.is_valid()): + user_form.save() + profile_form.save() + success = True + else: + user_form = UserForm(instance=user) + profile_form = ProfileForm(instance=user.profile) + return render(request, "gestion/profile.html", + {"user_form": user_form, "profile_form": profile_form, + "success": success}) 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 2/2] 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"] - -