annuaire-eleves/fiches/views.py

72 lines
2.3 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-01-15 21:27:37 +01:00
from fiches.forms import ProfileForm, SearchForm
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
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
2019-02-11 22:52:48 +01:00
2020-01-27 19:58:25 +01:00
2020-01-15 20:58:10 +01:00
@login_required
2020-09-15 13:32:11 +02:00
def fiche(request, user):
profile = get_object_or_404(Profile, user__username=user)
2020-02-13 00:11:28 +01:00
return render(request, "fiches/fiche.html", {"profile": profile})
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-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-01-15 21:27:37 +01:00
@login_required
2020-02-08 11:02:39 +01:00
def home(request):
2020-02-13 00:11:28 +01:00
if request.method == "POST":
2020-01-27 19:58:25 +01:00
form = SearchForm(request.POST)
if form.is_valid():
2020-02-13 00:11:28 +01:00
result = Profile.objects.filter(
Q(full_name__icontains=form.cleaned_data["name"])
| Q(nickname__icontains=form.cleaned_data["name"])
)
return render(request, "fiches/home.html", {"form": form, "result": result})
2020-02-08 11:02:39 +01:00
2020-01-27 19:58:25 +01:00
else:
form = SearchForm()
2020-02-13 00:11:28 +01:00
return render(request, "fiches/home.html", {"form": form})
2020-02-08 14:10:14 +01:00
@login_required
def birthday(request):
today = timezone.now()
2020-02-13 00:11:28 +01:00
result = list(
Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month)
)
for i in range(1, 7):
2020-02-08 14:10:14 +01:00
today = today + timedelta(days=1)
2020-02-13 00:11:28 +01:00
result += list(
Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month
)
)
return render(request, "fiches/birthday.html", {"result": result})