annuaire-eleves/fiches/views.py

108 lines
3.6 KiB
Python
Raw Normal View History

2019-02-11 22:52:48 +01:00
from django.shortcuts import render
2020-01-27 19:58:25 +01:00
from django.shortcuts import get_object_or_404, redirect
2020-01-15 20:58:10 +01:00
from django.contrib.auth.decorators import login_required
from fiches.models import Profile
2020-09-16 15:38:38 +02:00
from fiches.forms import ProfileForm, SearchForm, PhoneForm
2020-01-15 20:58:10 +01:00
from django.urls import reverse
2020-02-08 11:15:43 +01:00
from django.db.models import Q
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
2020-02-08 14:10:14 +01:00
from datetime import timedelta
2020-02-13 00:11:28 +01:00
from django.core.mail import send_mail
from django.template.loader import render_to_string
2020-09-16 15:38:38 +02:00
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import FormView, UpdateView
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-01-15 20:58:10 +01:00
@login_required
def fiche_modif(request):
2020-01-27 19:58:25 +01:00
profile = request.user.profile
2020-02-13 00:11:28 +01:00
if request.method == "POST":
form = ProfileForm(request.POST, request.FILES, instance=profile)
2020-09-16 15:38:38 +02:00
#form_phone = PhoneForm(request.POST, instance=)
2020-01-27 19:58:25 +01:00
if form.is_valid():
form.save()
2020-02-13 00:11:28 +01:00
send_mail(
"Fiche annuaire modifée",
render_to_string("fiches/mail/mail_modif.txt", {"profile": profile}),
"klub-dev@ens.psl.eu",
["{}@clipper.ens.psl.eu".format(request.user.username)],
fail_silently=False,
)
2020-09-15 13:32:11 +02:00
return redirect(reverse("fiche", args=(profile.user,)))
2020-02-13 00:11:28 +01:00
2020-01-27 19:58:25 +01:00
else:
form = ProfileForm(instance=profile)
2020-02-13 00:11:28 +01:00
return render(request, "fiches/fiches_modif.html", {"form": form})
2020-01-15 21:27:37 +01:00
2020-02-08 11:02:39 +01:00
2020-09-16 15:38:38 +02:00
@method_decorator(login_required, name="dispatch")
class EditView(UpdateView):
model = Profile
template_name = "fiches/fiches_modif.html"
form_class = ProfileForm
def get_object(self):
return self.request.user.profile
def get_success_url(self):
return reverse("fiche", args=(self.get_object().user,))
def form_valid(self, form):
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 super().form_valid(form)
@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(
2020-02-13 00:11:28 +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()
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)
context['result'] += list(
Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month
)
)
return context