Use mixin in UserUpdateView
This commit is contained in:
parent
b6626093e5
commit
f33416b712
|
@ -4,21 +4,23 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
|
|
||||||
{% for error in user_form.non_field_errors %}
|
{% for form in forms.values %}
|
||||||
<p class="error">{{ error }}</p>
|
{% for error in form.non_field_errors %}
|
||||||
|
<div class="notification is-danger">
|
||||||
|
{{ error }}
|
||||||
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% for error in profile_form.non_field_errors %}
|
|
||||||
<p class="error">{{ error }}</p>
|
|
||||||
{% 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">
|
<div class="container">
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
|
|
||||||
{% include "bds/forms/form.html" with form=user_form errors=False %}
|
{% for form in forms.values %}
|
||||||
{% include "bds/forms/form.html" with form=profile_form errors=False %}
|
{% include "bds/forms/form.html" with form=form errors=False %}
|
||||||
|
{% endfor %}
|
||||||
|
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<p class="control">
|
<p class="control">
|
||||||
|
|
55
bds/views.py
55
bds/views.py
|
@ -1,12 +1,13 @@
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.views.generic import TemplateView
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
from bds.autocomplete import bds_search
|
from bds.autocomplete import bds_search
|
||||||
from bds.forms import ProfileForm, UserForm
|
from bds.forms import ProfileForm, UserForm
|
||||||
from bds.mixins import StaffRequiredMixin
|
from bds.mixins import MultipleFormView, StaffRequiredMixin
|
||||||
from shared.views import AutocompleteView
|
from shared.views import AutocompleteView
|
||||||
|
|
||||||
User = get_user_model()
|
User = get_user_model()
|
||||||
|
@ -21,41 +22,39 @@ class Home(StaffRequiredMixin, TemplateView):
|
||||||
template_name = "bds/home.html"
|
template_name = "bds/home.html"
|
||||||
|
|
||||||
|
|
||||||
class UserUpdateView(StaffRequiredMixin, TemplateView):
|
class UserUpdateView(StaffRequiredMixin, MultipleFormView):
|
||||||
template_name = "bds/user_update.html"
|
template_name = "bds/user_update.html"
|
||||||
|
|
||||||
def get_user(self):
|
form_classes = {
|
||||||
return get_object_or_404(User, pk=self.kwargs["pk"])
|
"user": UserForm,
|
||||||
|
"profile": ProfileForm,
|
||||||
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(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def get(self, *args, **kwargs):
|
def dispatch(self, request, *args, **kwargs):
|
||||||
self.user = self.get_user()
|
self.user = get_object_or_404(User, pk=self.kwargs["pk"])
|
||||||
return super().get(*args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def get_user_instance(self):
|
||||||
self.user = self.get_user()
|
return self.user
|
||||||
user_form = self.get_user_form(data=request.POST)
|
|
||||||
profile_form = self.get_profile_form(data=request.POST)
|
|
||||||
|
|
||||||
if user_form.is_valid() and profile_form.is_valid():
|
def get_profile_instance(self):
|
||||||
self.user = user_form.save()
|
return getattr(self.user, "bds", None)
|
||||||
profile = profile_form.save(commit=False)
|
|
||||||
profile.user = self.user
|
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()
|
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:
|
else:
|
||||||
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
|
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue