2021-02-06 00:25:38 +01:00
|
|
|
from datetime import date
|
|
|
|
|
2020-01-15 20:58:10 +01:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2021-02-06 00:25:38 +01:00
|
|
|
from django.core.mail import send_mail
|
|
|
|
from django.db.models import Q
|
2020-09-17 15:46:16 +02:00
|
|
|
from django.forms import formset_factory
|
|
|
|
from django.forms.models import model_to_dict
|
2021-02-06 00:25:38 +01:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.template.loader import render_to_string
|
2020-01-15 20:58:10 +01:00
|
|
|
from django.urls import reverse
|
2020-02-08 14:10:14 +01:00
|
|
|
from django.utils import timezone
|
2020-09-16 15:38:38 +02:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.views.generic.detail import DetailView
|
|
|
|
from django.views.generic.edit import FormView, UpdateView
|
2021-02-06 00:25:38 +01:00
|
|
|
from django.views.generic.list import ListView
|
|
|
|
|
|
|
|
from fiches.forms import (
|
|
|
|
AddressFormSet,
|
|
|
|
MailFormSet,
|
|
|
|
PhoneFormSet,
|
|
|
|
ProfileForm,
|
|
|
|
SearchForm,
|
|
|
|
SocialFormSet,
|
|
|
|
)
|
|
|
|
from fiches.models import Address, Mail, Phone, Profile, Social
|
2019-02-11 22:52:48 +01:00
|
|
|
|
2020-01-27 19:58:25 +01:00
|
|
|
|
2020-09-16 15:38:38 +02:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class FicheView(DetailView):
|
|
|
|
model = Profile
|
|
|
|
template_name = "fiches/fiche.html"
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return get_object_or_404(Profile, user__username=self.kwargs.get("user"))
|
2019-04-08 21:47:50 +02:00
|
|
|
|
|
|
|
|
2020-09-16 15:38:38 +02:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class EditView(UpdateView):
|
|
|
|
template_name = "fiches/fiches_modif.html"
|
2020-09-17 15:46:16 +02:00
|
|
|
model = Profile
|
2020-09-16 15:38:38 +02:00
|
|
|
form_class = ProfileForm
|
|
|
|
|
2020-09-17 15:46:16 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
form = self.get_form(form_class)
|
2020-09-19 18:59:49 +02:00
|
|
|
phone_form = PhoneFormSet(instance=self.object)
|
|
|
|
social_form = SocialFormSet(instance=self.object)
|
|
|
|
mail_form = MailFormSet(instance=self.object)
|
|
|
|
address_form = AddressFormSet(instance=self.object)
|
2020-09-17 15:46:16 +02:00
|
|
|
return self.render_to_response(
|
2021-01-28 00:29:20 +01:00
|
|
|
self.get_context_data(
|
|
|
|
form=form,
|
|
|
|
phone_form=phone_form,
|
|
|
|
social_form=social_form,
|
|
|
|
mail_form=mail_form,
|
|
|
|
address_form=address_form,
|
|
|
|
)
|
|
|
|
)
|
2020-09-16 15:38:38 +02:00
|
|
|
|
2020-09-17 15:46:16 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = self.get_object()
|
|
|
|
form_class = self.get_form_class()
|
|
|
|
form = self.get_form(form_class)
|
2020-09-19 18:59:49 +02:00
|
|
|
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)
|
2021-01-28 00:29:20 +01:00
|
|
|
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
|
|
|
|
)
|
2020-09-17 15:46:16 +02:00
|
|
|
else:
|
2021-01-28 00:29:20 +01:00
|
|
|
return self.form_invalid(
|
|
|
|
form, phone_form, social_form, mail_form, address_form
|
|
|
|
)
|
|
|
|
|
2020-09-17 16:30:41 +02:00
|
|
|
def form_valid(self, form, phone_form, social_form, mail_form, address_form):
|
2020-09-17 15:46:16 +02:00
|
|
|
self.object = form.save()
|
|
|
|
phone_form.save()
|
|
|
|
social_form.save()
|
2020-09-17 16:30:41 +02:00
|
|
|
mail_form.save()
|
|
|
|
address_form.save()
|
2020-09-16 15:38:38 +02:00
|
|
|
send_mail(
|
2021-01-28 00:29:20 +01:00
|
|
|
"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,
|
|
|
|
)
|
2020-09-17 15:46:16 +02:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
2020-09-17 16:30:41 +02:00
|
|
|
def form_invalid(self, form, phone_form, social_form, mail_form, address_form):
|
2020-09-17 15:46:16 +02:00
|
|
|
return self.render_to_response(
|
2021-01-28 00:29:20 +01:00
|
|
|
self.get_context_data(
|
|
|
|
form=form,
|
|
|
|
phone_form=phone_form,
|
|
|
|
social_form=social_form,
|
|
|
|
mail_form=mail_form,
|
|
|
|
address_form=address_form,
|
|
|
|
)
|
|
|
|
)
|
2020-09-16 15:38:38 +02:00
|
|
|
|
2021-02-06 00:25:38 +01:00
|
|
|
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
|
|
|
|
|
2020-09-17 15:46:16 +02:00
|
|
|
def get_object(self):
|
|
|
|
return self.request.user.profile
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse("fiche", args=(self.get_object().user,))
|
2020-09-16 15:38:38 +02:00
|
|
|
|
|
|
|
|
2021-02-06 00:25:38 +01:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
2020-09-16 15:38:38 +02:00
|
|
|
class HomeView(FormView):
|
|
|
|
model = Profile
|
|
|
|
template_name = "fiches/home.html"
|
|
|
|
form_class = SearchForm
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
result = Profile.objects.filter(
|
2021-01-28 00:29:20 +01:00
|
|
|
Q(full_name__icontains=form.cleaned_data["name"])
|
|
|
|
| Q(nickname__icontains=form.cleaned_data["name"])
|
|
|
|
)
|
2020-09-16 15:38:38 +02:00
|
|
|
return self.render_to_response(self.get_context_data(result=result))
|
2020-02-08 11:02:39 +01:00
|
|
|
|
2020-02-08 14:10:14 +01:00
|
|
|
|
2020-09-16 15:38:38 +02:00
|
|
|
@method_decorator(login_required, name="dispatch")
|
|
|
|
class BirthdayView(ListView):
|
|
|
|
model = Profile
|
|
|
|
template_name = "fiches/birthday.html"
|
2020-02-08 14:10:14 +01:00
|
|
|
|
2020-09-16 15:38:38 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
today = timezone.now()
|
2021-01-28 00:29:20 +01:00
|
|
|
context["result"] = list(
|
|
|
|
Profile.objects.filter(
|
|
|
|
birth_date__day=today.day, birth_date__month=today.month
|
|
|
|
)
|
2020-02-13 00:11:28 +01:00
|
|
|
)
|
2020-09-16 15:38:38 +02:00
|
|
|
for i in range(1, 7):
|
|
|
|
today = today + timedelta(days=1)
|
2021-01-28 00:29:20 +01:00
|
|
|
context["result"] += list(
|
2020-09-16 15:38:38 +02:00
|
|
|
Profile.objects.filter(
|
|
|
|
birth_date__day=today.day, birth_date__month=today.month
|
|
|
|
)
|
|
|
|
)
|
2021-01-28 00:29:20 +01:00
|
|
|
return context
|