1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-23 07:52:23 +00:00

add a profile_update view

This commit is contained in:
Pierre-antoine Comby 2020-02-03 19:25:05 +01:00
parent e80163ce4d
commit b9c3ab5ea8
4 changed files with 54 additions and 7 deletions

View File

@ -16,5 +16,6 @@ urlpatterns = [
path('club/<int:pk>/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/<int:pk>',views.UserDetailView.as_view(),name="user_detail")
path('user/<int:pk>',views.UserDetailView.as_view(),name="user_detail"),
path('user/<int:pk>/update',views.UserUpdateView.as_view(),name="user_update_profile"),
]

View File

@ -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 ...

View File

@ -22,10 +22,12 @@
<dt class="col-6 col-md-3">{% trans 'balance'|capfirst %}</dt>
<dd class="col-6 col-md-3">{{ object.user.note.balance | pretty_money }}</dd>
</dl>
<center>
<a class="btn btn-primary" href="{% url 'member:user_update_profile' object.pk %}">{% trans 'Update Profile' %}</a>
<a class="btn btn-primary" href="{% url 'password_change' %}">{% trans 'Change password' %}</a>
</center>
<a class="btn btn-primary" href="{% url 'password_change' %}">{% trans 'Change password' %}</a>
<div class="accordion" id="accordionExample">
<div class="accordion" id="accordionProfile">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
@ -35,7 +37,7 @@
</h5>
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionProfile">
<div class="card-body">
{% render_table club_list %}
</div>
@ -49,7 +51,7 @@
</button>
</h5>
</div>
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample">
<div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionProfile">
<div class="card-body">
{% render_table history_list %}
</div>

View File

@ -0,0 +1,17 @@
<!doctype html>
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load i18n static pretty_money django_tables2 %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ form|crispy }}
{{ profile_form|crispy }}
<button class="btn btn-link" type="submit">
{% trans "Save Changes" %}
</button>
</form>
{% endblock %}