160 lines
5.4 KiB
Python
160 lines
5.4 KiB
Python
from datetime import date
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.core.mail import send_mail
|
|
from django.db.models import Q
|
|
from django.forms import formset_factory
|
|
from django.forms.models import model_to_dict
|
|
from django.http import HttpResponseRedirect
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.template.loader import render_to_string
|
|
from django.urls import reverse
|
|
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 Address, Mail, Phone, Profile, Social
|
|
|
|
|
|
@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"))
|
|
|
|
|
|
@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):
|
|
result = Profile.objects.filter(
|
|
Q(full_name__icontains=form.cleaned_data["name"])
|
|
| Q(nickname__icontains=form.cleaned_data["name"])
|
|
)
|
|
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
|
|
)
|
|
)
|
|
for i 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
|
|
)
|
|
)
|
|
return context
|