On corrige la recherche pour filtrer sur les promotions et les départements

This commit is contained in:
Tom Hubrecht 2021-10-09 11:52:20 +02:00
parent ebab4f5cb2
commit b3f7e944c3
2 changed files with 15 additions and 10 deletions

View file

@ -1,10 +1,11 @@
from django.urls import path
from . import views
urlpatterns = [
path("", views.HomeView.as_view(), name="home"),
path("fiche/edit", views.EditView.as_view(), name="fiche_modif"),
path("fiche/reset", views.ResetView.as_view(), name="fiche_reset"),
path("fiche/<user>", views.FicheView.as_view(), name="fiche"),
path("fiche/_edit", views.EditView.as_view(), name="fiche_modif"),
path("fiche/_reset", views.ResetView.as_view(), name="fiche_reset"),
path("fiche/<slug:slug>", views.FicheView.as_view(), name="fiche"),
path("birthday", views.BirthdayView.as_view(), name="birthday"),
]

View file

@ -4,7 +4,6 @@ from django.contrib.auth.decorators import login_required
from django.core.mail import send_mail
from django.db.models import DateTimeField, Q, Value
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.template.loader import render_to_string
from django.urls import reverse, reverse_lazy
from django.utils import timezone
@ -29,9 +28,7 @@ from fiches.utils import get_ldap_infos
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"))
slug_field = "user__username"
@method_decorator(login_required, name="dispatch")
@ -131,10 +128,17 @@ class HomeView(FormView):
form_class = SearchForm
def form_valid(self, form):
name = form.cleaned_data["name"]
promotion = form.cleaned_data["year"]
result = Profile.objects.filter(
Q(full_name__icontains=form.cleaned_data["name"])
| Q(nickname__icontains=form.cleaned_data["name"])
)
Q(full_name__icontains=name)
| Q(nickname__icontains=name)
| Q(user__username__icontains=name)
).filter(department__in=form.cleaned_data["department"])
if promotion is not None:
result = result.filter(promotion=promotion)
return self.render_to_response(self.get_context_data(result=result))