Commit 81206bb3 authored by Hamza Dely's avatar Hamza Dely
Browse files

[comptes/views] Factorisation du code de certaines vues

parent fac275e9
......@@ -11,6 +11,7 @@ from django.db import transaction
from django.utils import timezone
from django.core.exceptions import ValidationError
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import PasswordChangeForm
from note_kfet.environnement import ACCREDITATIONS_DEFAUT
......@@ -100,6 +101,13 @@ class AdherentInscriptionForm(UserCreationForm):
# TODO : Envoyer un mail de bienvenue à l'adhérent nouvellement inscrit
return adh
class AdherentPasswordChangeForm(PasswordChangeForm):
"""
Formulaire de changement de mot de passe pour un adhérent
"""
def __init__(self, instance=None, **kwargs):
super().__init__(instance, **kwargs)
class AdherentDroitForm(forms.Form):
"""
Formulaire pour l'attribution d'un nouveau droit.
......
{% extends "base.html" %}
{% from "forms.html" import display_grid %}
{% block app %}Gestion des comptes{% endblock %}
{% block fonction %}Changement du mot de passe du compte n°{{ adherent.id }}{% endblock %}
{% block fonction %}Changement du mot de passe du compte n°{{ object.id }}{% endblock %}
{% block contenu %}
<div class="row">
{{ display_grid(form, csrf_input, submit="Changer") }}
......
......@@ -17,7 +17,6 @@ from django.views.generic.detail import DetailView
from django.views.generic.edit import FormView, CreateView, UpdateView, DeleteView
from django.contrib import messages
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.contrib.auth.views import LoginView, LogoutView, PasswordResetDoneView, PasswordResetCompleteView
from django.contrib.auth.models import Permission
......@@ -42,7 +41,8 @@ from comptes.models import (
Section, Alias, Droit, Accreditation, Adhesion, Adherent,
)
from comptes.forms import (
AdherentInscriptionForm, AdherentDroitForm, AdherentSuppressionForm, AliasAjoutForm
AdherentInscriptionForm, AdherentPasswordChangeForm, AdherentDroitForm,
AdherentSuppressionForm, AliasAjoutForm,
)
from comptes.filters import AdherentRechercheFilter
from comptes.serializers import (
......@@ -198,9 +198,6 @@ class AdherentUpdateView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin,
del form.fields[key]
return form
def get_success_url(self, *args, **kwargs):
return reverse_lazy('comptes:detail', kwargs={'pk' : self.object.id})
class AdherentDroitChangeView(NoteMixin, LoginRequiredMixin, FormView):
"""
Une vue permettant de modifier les droits d'un adhérent.
......@@ -271,7 +268,7 @@ class AdherentDroitChangeView(NoteMixin, LoginRequiredMixin, FormView):
def get_success_url(self):
return reverse_lazy("comptes:detail", kwargs={'pk' : self.target_user.id})
class AdherentPasswordChangeView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin, FormView):
class AdherentPasswordChangeView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
"""
Une vue pour changer le mot de passe d'un utilisateur.
"""
......@@ -280,27 +277,17 @@ class AdherentPasswordChangeView(NoteMixin, LoginRequiredMixin, PermissionRequir
permission_required = D("comptes.adherent_change_pw", Acl.LIMITE)
template_name = "adherent_change_password.html"
form_class = PasswordChangeForm
def get_form(self, form_class=None):
self.adherent = get_object_or_404(Adherent, pk=self.kwargs['pk'])
return self.form_class(self.adherent, **self.get_form_kwargs())
form_class = AdherentPasswordChangeForm
model = Adherent
def form_valid(self, form):
user = self.request.user
if self.adherent == user:
form.save()
messages.success(self.request, "Mot de passe changé avec succès")
elif (self.adherent.type == Adherent.PERSONNE and
user.has_perm("comptes.adherent_change_pw", Acl.BASIQUE)):
form.save()
messages.success(self.request, "Mot de passe changé avec succès")
elif (self.adherent.type in [Adherent.CLUB, Adherent.SECTION] and
user.has_perm("comptes.adherent_change_pw", Acl.ETENDU)):
form.save()
messages.success(self.request, "Mot de passe changé avec succès")
elif user.has_perm("comptes.adherent_change_pw", Acl.TOTAL):
form.save()
if (self.object == user
or user.has_perm("comptes.adherent_change_pw", Acl.TOTAL)
or (self.object.type == Adherent.PERSONNE
and user.has_perm("comptes.adherent_change_pw", Acl.BASIQUE))
or (self.object.type in [Adherent.CLUB, Adherent.SECTION]
and user.has_perm("comptes.adherent_change_pw", Acl.ETENDU))):
messages.success(self.request, "Mot de passe changé avec succès")
else:
messages.error(
......@@ -309,14 +296,6 @@ class AdherentPasswordChangeView(NoteMixin, LoginRequiredMixin, PermissionRequir
return super().form_valid(form)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update({'adherent' : self.adherent})
return context
def get_success_url(self):
return reverse_lazy('comptes:detail', kwargs={'pk' : self.adherent.id})
class AdherentAvatarView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView):
"""
Vue pour la modification de l'avatar d'un adhérent.
......@@ -339,10 +318,6 @@ class AdherentAvatarView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin,
return super().form_valid(form)
def get_success_url(self):
adherent = get_object_or_404(Adherent, pk=self.kwargs['pk'])
return reverse_lazy('comptes:detail', kwargs={'pk' : adherent.id})
class AdherentDeleteView(NoteMixin, LoginRequiredMixin, PermissionRequiredMixin, DeleteView):
"""
Vue pour la suppression d'un adhérent.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment