gestioCOF/bds/views.py
2020-07-26 22:10:09 +02:00

61 lines
1.9 KiB
Python

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 MultipleFormView, StaffRequiredMixin
from shared.views import AutocompleteView
User = get_user_model()
class BDSAutocompleteView(StaffRequiredMixin, AutocompleteView):
template_name = "bds/search_results.html"
search_composer = bds_search
class Home(StaffRequiredMixin, TemplateView):
template_name = "bds/home.html"
class UserUpdateView(StaffRequiredMixin, MultipleFormView):
template_name = "bds/user_update.html"
form_classes = {
"user": UserForm,
"profile": ProfileForm,
}
def dispatch(self, request, *args, **kwargs):
self.user = get_object_or_404(User, pk=self.kwargs["pk"])
return super().dispatch(request, *args, **kwargs)
def get_user_instance(self):
return 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 !"))
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"))
return self.render_to_response(self.get_context_data(user_form, profile_form))