annuaire-eleves/fiches/views.py
2020-09-16 15:38:38 +02:00

108 lines
No EOL
3.6 KiB
Python

from django.shortcuts import render
from django.shortcuts import get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from fiches.models import Profile
from fiches.forms import ProfileForm, SearchForm, PhoneForm
from django.urls import reverse
from django.db.models import Q
from django.utils import timezone
from django.utils.decorators import method_decorator
from datetime import timedelta
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from django.views.generic.edit import FormView, UpdateView
@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"))
@login_required
def fiche_modif(request):
profile = request.user.profile
if request.method == "POST":
form = ProfileForm(request.POST, request.FILES, instance=profile)
#form_phone = PhoneForm(request.POST, instance=)
if form.is_valid():
form.save()
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,
)
return redirect(reverse("fiche", args=(profile.user,)))
else:
form = ProfileForm(instance=profile)
return render(request, "fiches/fiches_modif.html", {"form": form})
@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(
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