from datetime import date, timedelta from django.contrib.auth.decorators import login_required from django.core.mail import send_mail from django.db.models import DateTimeField, Q, Value from django.http import HttpResponseRedirect from django.template.loader import render_to_string from django.urls import reverse, reverse_lazy from django.utils import timezone from django.utils.decorators import method_decorator from django.views.generic.detail import DetailView from django.views.generic.edit import FormView, UpdateView from django.views.generic.list import ListView from fiches.forms import ( AddressFormSet, MailFormSet, PhoneFormSet, ProfileForm, SearchForm, SocialFormSet, ) from fiches.models import Department, Profile from fiches.utils import get_ldap_infos @method_decorator(login_required, name="dispatch") class FicheView(DetailView): model = Profile template_name = "fiches/fiche.html" slug_field = "user__username" @method_decorator(login_required, name="dispatch") class EditView(UpdateView): template_name = "fiches/fiches_modif.html" model = Profile form_class = ProfileForm def get(self, request, *args, **kwargs): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) phone_form = PhoneFormSet(instance=self.object) social_form = SocialFormSet(instance=self.object) mail_form = MailFormSet(instance=self.object) address_form = AddressFormSet(instance=self.object) return self.render_to_response( self.get_context_data( form=form, phone_form=phone_form, social_form=social_form, mail_form=mail_form, address_form=address_form, ) ) def post(self, request, *args, **kwargs): self.object = self.get_object() form_class = self.get_form_class() form = self.get_form(form_class) phone_form = PhoneFormSet(self.request.POST, instance=self.object) social_form = SocialFormSet(self.request.POST, instance=self.object) mail_form = MailFormSet(self.request.POST, instance=self.object) address_form = AddressFormSet(self.request.POST, instance=self.object) if ( form.is_valid() and phone_form.is_valid() and social_form.is_valid() and mail_form.is_valid() and address_form.is_valid() ): return self.form_valid( form, phone_form, social_form, mail_form, address_form ) else: return self.form_invalid( form, phone_form, social_form, mail_form, address_form ) def form_valid(self, form, phone_form, social_form, mail_form, address_form): self.object = form.save() phone_form.save() social_form.save() mail_form.save() address_form.save() send_mail( "Fiche annuaire modifée", render_to_string( "fiches/mail/mail_modif.txt", {"profile": self.get_object()} ), "klub-dev@ens.psl.eu", ["{}@clipper.ens.psl.eu".format(self.get_object().user.username)], fail_silently=False, ) return HttpResponseRedirect(self.get_success_url()) def form_invalid(self, form, phone_form, social_form, mail_form, address_form): return self.render_to_response( self.get_context_data( form=form, phone_form=phone_form, social_form=social_form, mail_form=mail_form, address_form=address_form, ) ) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) today = date.today() context["default_birth_date"] = today.replace( year=(today.year - 20) ).isoformat() return context def get_object(self): return self.request.user.profile def get_success_url(self): return reverse("fiche", args=(self.get_object().user,)) @method_decorator(login_required, name="dispatch") class HomeView(FormView): model = Profile template_name = "fiches/home.html" form_class = SearchForm def form_valid(self, form): name = form.cleaned_data["name"] promotion = form.cleaned_data["year"] depts = form.cleaned_data["department"] result = Profile.objects.filter( Q(full_name__icontains=name) | Q(nickname__icontains=name) | Q(user__username__icontains=name) ) if depts: result = result.filter(department__in=depts) if promotion: result = result.filter(promotion=promotion) return self.render_to_response(self.get_context_data(result=result)) @method_decorator(login_required, name="dispatch") class BirthdayView(ListView): model = Profile template_name = "fiches/birthday.html" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) today = timezone.now() context["result"] = list( Profile.objects.filter( birth_date__day=today.day, birth_date__month=today.month ).annotate(day=Value(today, output_field=DateTimeField())) ) for _ in range(1, 7): today = today + timedelta(days=1) context["result"] += list( Profile.objects.filter( birth_date__day=today.day, birth_date__month=today.month ).annotate(day=Value(today, output_field=DateTimeField())) ) return context @method_decorator(login_required, name="dispatch") class ResetView(UpdateView): model = Profile template_name = "fiches/reset.html" fields = [] success_url = reverse_lazy("fiche_modif") def get_object(self): return self.request.user.profile def post(self, request, *args, **kwargs): if "reset" in request.POST: # On réinitialise le profil profile = self.get_object() base_infos = get_ldap_infos(profile.user.cas_account.cas_login) # On supprime les trucs inutiles profile.phone_set.all().delete() profile.social_set.all().delete() profile.mail_set.all().delete() profile.address_set.all().delete() profile.nickname = "" profile.pronoun = "" profile.birth_date = None profile.past_studies = "" profile.experiences = "" profile.thurne = "" profile.text_field = "" profile.picture.delete() # On réinitialise avec les infos du LDAP if base_infos is not None: profile.full_name = base_infos["name"] profile.department.clear() profile.department.add(Department.objects.get(name=base_infos["dept"])) profile.promotion = base_infos["promo"] profile.save() return HttpResponseRedirect(self.success_url)