annuaire-eleves/fiches/views.py
Ju Luiselli 8a2afe3893 flake8
2020-02-08 14:48:44 +01:00

60 lines
1.9 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
from django.urls import reverse
from django.db.models import Q
from django.utils import timezone
from datetime import timedelta
@login_required
def fiche(request, id):
profile = get_object_or_404(Profile, id=id)
return render(request, 'fiches/fiche.html', {"profile": profile})
@login_required
def fiche_modif(request):
profile = request.user.profile
if request.method == 'POST':
form = ProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
return redirect(reverse('fiche', args=(profile.id,)))
else:
form = ProfileForm(instance=profile)
return render(request, 'fiches/fiches_modif.html', {"form": form})
@login_required
def home(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
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}
)
else:
form = SearchForm()
return render(request, 'fiches/home.html', {"form": form})
@login_required
def birthday(request):
today = timezone.now()
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)
result += list(Profile.objects.filter(
birth_date__day=today.day, birth_date__month=today.month)
)
return render(request, 'fiches/birthday.html', {"result": result})