annuaire-eleves/fiches/views.py

61 lines
1.9 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
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-01-27 19:58:25 +01:00
def fiche(request, id):
profile = get_object_or_404(Profile, id=id)
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
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})
2020-01-15 20:58:10 +01:00
2020-01-15 21:27:37 +01:00
@login_required
2020-02-08 11:02:39 +01:00
def home(request):
2020-01-27 19:58:25 +01:00
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
2020-02-08 14:48:44 +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-08 14:48:44 +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-08 14:48:44 +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-08 14:48:44 +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})