From b9c3ab5ea8eceaf827d379e10d245961793d1956 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Mon, 3 Feb 2020 19:25:05 +0100 Subject: [PATCH] add a profile_update view --- apps/member/urls.py | 3 ++- apps/member/views.py | 29 +++++++++++++++++++++++++++- templates/member/profile_detail.html | 12 +++++++----- templates/member/profile_update.html | 17 ++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 templates/member/profile_update.html diff --git a/apps/member/urls.py b/apps/member/urls.py index 8d3aa40a..9bcc1095 100644 --- a/apps/member/urls.py +++ b/apps/member/urls.py @@ -16,5 +16,6 @@ urlpatterns = [ path('club//add_member/',views.ClubAddMemberView.as_view(),name="club_add_member"), path('club/create/',views.ClubCreateView.as_view(),name="club_create"), path('user/',views.UserListView.as_view(),name="user_list"), - path('user/',views.UserDetailView.as_view(),name="user_detail") + path('user/',views.UserDetailView.as_view(),name="user_detail"), + path('user//update',views.UserUpdateView.as_view(),name="user_update_profile"), ] diff --git a/apps/member/views.py b/apps/member/views.py index 04735c64..90ea5ec3 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -5,7 +5,7 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.utils.translation import gettext_lazy as _ -from django.views.generic import CreateView, ListView, DetailView +from django.views.generic import CreateView, ListView, DetailView, UpdateView from django.http import HttpResponseRedirect from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User @@ -49,6 +49,33 @@ class UserCreateView(CreateView): profile.save() return super().form_valid(form) +class UserUpdateView(LoginRequiredMixin,UpdateView): + model = User + fields = ['first_name','last_name','username','email'] + template_name = 'member/profile_update.html' + + second_form = ProfileForm + def get_context_data(self,**kwargs): + context = super().get_context_data(**kwargs) + context["profile_form"] = self.second_form(instance=context['user'].profile) + + return context + + def form_valid(self, form): + profile_form = ProfileForm(data=self.request.POST,instance=self.request.user.profile) + if form.is_valid() and profile_form.is_valid(): + user = form.save() + profile = profile_form.save(commit=False) + profile.user = user + profile.save() + return super().form_valid(form) + + def get_success_url(self, **kwargs): + if kwargs: + return reverse_lazy('member:user_detail', kwargs = {'pk': kwargs['id']}) + else: + return reverse_lazy('member:user_detail', args = (self.object.id,)) + class UserDetailView(LoginRequiredMixin,DetailView): """ Affiche les informations sur un utilisateur, sa note, ses clubs ... diff --git a/templates/member/profile_detail.html b/templates/member/profile_detail.html index fd5ebd7f..53a0b9a0 100644 --- a/templates/member/profile_detail.html +++ b/templates/member/profile_detail.html @@ -22,10 +22,12 @@
{% trans 'balance'|capfirst %}
{{ object.user.note.balance | pretty_money }}
+
+ {% trans 'Update Profile' %} + {% trans 'Change password' %} +
- {% trans 'Change password' %} - -
+
@@ -35,7 +37,7 @@
-
+
{% render_table club_list %}
@@ -49,7 +51,7 @@
-
+
{% render_table history_list %}
diff --git a/templates/member/profile_update.html b/templates/member/profile_update.html new file mode 100644 index 00000000..10936cf7 --- /dev/null +++ b/templates/member/profile_update.html @@ -0,0 +1,17 @@ + +{% extends "base.html" %} +{% load crispy_forms_tags %} +{% load i18n static pretty_money django_tables2 %} + +{% block content %} + +
+ {% csrf_token %} + {{ form|crispy }} + {{ profile_form|crispy }} + +
+ +{% endblock %}