From a42a3fa17760c9dbdd9b883c7d2cb5798b046fd8 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 21 Nov 2024 22:51:15 +0100 Subject: [PATCH] feat: allow search for sex --- src/fellchensammlung/forms.py | 2 ++ src/fellchensammlung/views.py | 12 +++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index f728fac..f2d4b6d 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -187,3 +187,5 @@ def _get_distances(): class AdoptionNoticeSearchForm(forms.Form): location = forms.CharField(max_length=20, label=_("Stadt")) max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz")) + sex = forms.ChoiceField(choices=SexChoicesWithAll, label=_("Geschlecht"), required=False, + initial=SexChoicesWithAll.ALL) diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index fa34fcb..a0c3418 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -16,7 +16,7 @@ from notfellchen import settings from fellchensammlung import logger from .models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ User, Location, AdoptionNoticeStatus, Subscriptions, CommentNotification, BaseNotification, RescueOrganization, \ - Species, Log, Timestamp, TrustLevel + Species, Log, Timestamp, TrustLevel, SexChoicesWithAll from .forms import AdoptionNoticeForm, AdoptionNoticeFormWithDateWidget, ImageForm, ReportAdoptionNoticeForm, \ CommentForm, ReportCommentForm, AnimalForm, \ AdoptionNoticeSearchForm, AnimalFormWithDateWidget, AdoptionNoticeFormWithDateWidgetAutoAnimal @@ -169,9 +169,13 @@ def animal_detail(request, animal_id): def search(request): place_not_found = None + latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") + active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] if request.method == 'POST': - latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") - active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] + + sex = request.POST.get("sex") + if sex != SexChoicesWithAll.ALL: + active_adoptions = [adoption for adoption in active_adoptions if sex in adoption.sexes] search_form = AdoptionNoticeSearchForm(request.POST) max_distance = int(request.POST.get('max_distance')) @@ -188,8 +192,6 @@ def search(request): context = {"adoption_notices": adoption_notices_in_distance, "search_form": search_form, "place_not_found": place_not_found} else: - latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") - active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] search_form = AdoptionNoticeSearchForm() context = {"adoption_notices": active_adoptions, "search_form": search_form} return render(request, 'fellchensammlung/search.html', context=context)