Use mixin in UserUpdateView
This commit is contained in:
parent
b6626093e5
commit
f33416b712
2 changed files with 36 additions and 35 deletions
|
@ -4,21 +4,23 @@
|
|||
|
||||
{% block content %}
|
||||
|
||||
{% for error in user_form.non_field_errors %}
|
||||
<p class="error">{{ error }}</p>
|
||||
{% endfor %}
|
||||
{% for error in profile_form.non_field_errors %}
|
||||
<p class="error">{{ error }}</p>
|
||||
{% for form in forms.values %}
|
||||
{% for error in form.non_field_errors %}
|
||||
<div class="notification is-danger">
|
||||
{{ error }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
|
||||
<h1 class="title">{% trans "Modification de l'utilisateur " %}{{user_form.instance.username}}</h1>
|
||||
<h1 class="title">{% trans "Modification du profil " %}{{ view.user.username }}</h1>
|
||||
|
||||
<div class="container">
|
||||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{% include "bds/forms/form.html" with form=user_form errors=False %}
|
||||
{% include "bds/forms/form.html" with form=profile_form errors=False %}
|
||||
{% for form in forms.values %}
|
||||
{% include "bds/forms/form.html" with form=form errors=False %}
|
||||
{% endfor %}
|
||||
|
||||
<div class="field">
|
||||
<p class="control">
|
||||
|
|
55
bds/views.py
55
bds/views.py
|
@ -1,12 +1,13 @@
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from bds.autocomplete import bds_search
|
||||
from bds.forms import ProfileForm, UserForm
|
||||
from bds.mixins import StaffRequiredMixin
|
||||
from bds.mixins import MultipleFormView, StaffRequiredMixin
|
||||
from shared.views import AutocompleteView
|
||||
|
||||
User = get_user_model()
|
||||
|
@ -21,41 +22,39 @@ class Home(StaffRequiredMixin, TemplateView):
|
|||
template_name = "bds/home.html"
|
||||
|
||||
|
||||
class UserUpdateView(StaffRequiredMixin, TemplateView):
|
||||
class UserUpdateView(StaffRequiredMixin, MultipleFormView):
|
||||
template_name = "bds/user_update.html"
|
||||
|
||||
def get_user(self):
|
||||
return get_object_or_404(User, pk=self.kwargs["pk"])
|
||||
|
||||
def get_user_form(self, data=None):
|
||||
return UserForm(prefix="u", instance=self.user, data=data)
|
||||
|
||||
def get_profile_form(self, data=None):
|
||||
profile = getattr(self.user, "bds", None)
|
||||
return ProfileForm(prefix="p", instance=profile, data=data)
|
||||
|
||||
def get_context_data(self, user_form=None, profile_form=None, **kwargs):
|
||||
return {
|
||||
"user_form": user_form or self.get_user_form(),
|
||||
"profile_form": profile_form or self.get_profile_form(),
|
||||
form_classes = {
|
||||
"user": UserForm,
|
||||
"profile": ProfileForm,
|
||||
}
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
self.user = self.get_user()
|
||||
return super().get(*args, **kwargs)
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self.user = get_object_or_404(User, pk=self.kwargs["pk"])
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self.user = self.get_user()
|
||||
user_form = self.get_user_form(data=request.POST)
|
||||
profile_form = self.get_profile_form(data=request.POST)
|
||||
def get_user_instance(self):
|
||||
return self.user
|
||||
|
||||
if user_form.is_valid() and profile_form.is_valid():
|
||||
self.user = user_form.save()
|
||||
profile = profile_form.save(commit=False)
|
||||
profile.user = self.user
|
||||
def get_profile_instance(self):
|
||||
return getattr(self.user, "bds", None)
|
||||
|
||||
def get_success_url(self):
|
||||
return reverse("bds:user.update", args=(self.user.pk,))
|
||||
|
||||
def form_valid(self, forms):
|
||||
user = forms["user"].save()
|
||||
profile = forms["profile"].save(commit=False)
|
||||
profile.user = user
|
||||
profile.save()
|
||||
messages.success(self.request, _("Profil mis à jour avec succès"))
|
||||
messages.success(self.request, _("Profil mis à jour avec succès !"))
|
||||
|
||||
return super().form_valid(forms)
|
||||
|
||||
def form_invalid(self, forms):
|
||||
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
|
||||
return super().form_invalid(forms)
|
||||
else:
|
||||
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
|
||||
|
||||
|
|
Loading…
Reference in a new issue