gestioCOF/bds/views.py

112 lines
3.4 KiB
Python
Raw Normal View History

2020-07-06 23:40:56 +02:00
from django.contrib import messages
from django.contrib.auth import get_user_model
from django.shortcuts import get_object_or_404
2020-07-21 14:49:10 +02:00
from django.urls import reverse
2020-07-06 23:40:56 +02:00
from django.utils.translation import gettext_lazy as _
2020-06-05 17:02:16 +02:00
from django.views.generic import TemplateView
from bds.autocomplete import bds_search
2020-07-21 14:49:37 +02:00
from bds.forms import ProfileForm, UserForm, UserFromClipperForm, UserFromScratchForm
2020-07-21 14:49:10 +02:00
from bds.mixins import MultipleFormView, StaffRequiredMixin
2020-07-01 22:29:07 +02:00
from shared.views import AutocompleteView
2020-06-05 17:02:16 +02:00
2020-07-06 23:40:56 +02:00
User = get_user_model()
2020-06-05 17:02:16 +02:00
2020-07-01 22:29:07 +02:00
class BDSAutocompleteView(StaffRequiredMixin, AutocompleteView):
2020-06-05 17:02:16 +02:00
template_name = "bds/search_results.html"
2020-07-01 22:29:07 +02:00
search_composer = bds_search
2020-06-07 20:58:33 +02:00
2020-06-07 21:01:54 +02:00
class Home(StaffRequiredMixin, TemplateView):
2020-06-07 20:58:33 +02:00
template_name = "bds/home.html"
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
class UserUpdateView(StaffRequiredMixin, MultipleFormView):
2020-07-06 23:40:56 +02:00
template_name = "bds/user_update.html"
2020-07-21 14:49:10 +02:00
form_classes = {
"user": UserForm,
"profile": ProfileForm,
}
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
def dispatch(self, request, *args, **kwargs):
self.user = get_object_or_404(User, pk=self.kwargs["pk"])
return super().dispatch(request, *args, **kwargs)
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
def get_user_instance(self):
return self.user
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
def get_profile_instance(self):
return getattr(self.user, "bds", None)
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
def get_success_url(self):
return reverse("bds:user.update", args=(self.user.pk,))
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
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 !"))
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
return super().form_valid(forms)
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:10 +02:00
def form_invalid(self, forms):
messages.error(self.request, _("Veuillez corriger les erreurs ci-dessous"))
return super().form_invalid(forms)
2020-07-21 14:49:37 +02:00
class UserCreateView(StaffRequiredMixin, MultipleFormView):
template_name = "bds/user_create.html"
def get_form_classes(self):
profile_class = ProfileForm
if "clipper" in self.kwargs:
user_class = UserFromClipperForm
else:
user_class = UserFromScratchForm
return {"user": user_class, "profile": profile_class}
def get_user_initial(self):
if "clipper" in self.kwargs:
clipper = self.kwargs["clipper"]
email = "{}@clipper.ens.fr".format(clipper)
fullname = self.request.GET.get("fullname", None)
if fullname:
# Heuristique : le premier mot est le prénom
first_name = fullname.split()[0]
last_name = " ".join(fullname.split()[1:])
else:
first_name = ""
last_name = ""
return dict(
username=clipper,
email=email,
first_name=first_name,
last_name=last_name,
)
2020-07-06 23:40:56 +02:00
else:
2020-07-21 14:49:37 +02:00
return {}
2020-07-06 23:40:56 +02:00
2020-07-21 14:49:37 +02:00
def get_success_url(self):
return reverse("bds:user.update", args=(self.user.pk,))
def form_valid(self, forms):
# On redéfinit self.user pour get_success_url
self.user = forms["user"].save()
profile = forms["profile"].save(commit=False)
profile.user = self.user
profile.save()
messages.success(self.request, _("Profil créé 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)