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] 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})