feat: allow not searching for location

This commit is contained in:
moanos [he/him] 2024-11-21 23:07:27 +01:00
parent a42a3fa177
commit 885622e581
2 changed files with 17 additions and 11 deletions

View File

@ -1,7 +1,7 @@
from django import forms from django import forms
from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \ from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \
Comment Comment, SexChoicesWithAll
from django_registration.forms import RegistrationForm from django_registration.forms import RegistrationForm
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field, Hidden from crispy_forms.layout import Submit, Layout, Fieldset, HTML, Row, Column, Field, Hidden
@ -66,7 +66,8 @@ class AdoptionNoticeForm(forms.ModelForm):
class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm): class AdoptionNoticeFormWithDateWidget(AdoptionNoticeForm):
class Meta: class Meta:
model = AdoptionNotice model = AdoptionNotice
fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string", "organization"] fields = ['name', "group_only", "further_information", "description", "searching_since", "location_string",
"organization"]
widgets = { widgets = {
'searching_since': DateInput(), 'searching_since': DateInput(),
} }
@ -185,7 +186,7 @@ def _get_distances():
class AdoptionNoticeSearchForm(forms.Form): class AdoptionNoticeSearchForm(forms.Form):
location = forms.CharField(max_length=20, label=_("Stadt")) location = forms.CharField(max_length=20, label=_("Stadt"), required=False)
max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz")) max_distance = forms.ChoiceField(choices=_get_distances, label=_("Max. Distanz"))
sex = forms.ChoiceField(choices=SexChoicesWithAll, label=_("Geschlecht"), required=False, sex = forms.ChoiceField(choices=SexChoicesWithAll, label=_("Geschlecht"), required=False,
initial=SexChoicesWithAll.ALL) initial=SexChoicesWithAll.ALL)

View File

@ -178,6 +178,11 @@ def search(request):
active_adoptions = [adoption for adoption in active_adoptions if sex in adoption.sexes] active_adoptions = [adoption for adoption in active_adoptions if sex in adoption.sexes]
search_form = AdoptionNoticeSearchForm(request.POST) search_form = AdoptionNoticeSearchForm(request.POST)
search_form.is_valid()
if search_form.cleaned_data["location"] == "":
adoption_notices_in_distance = active_adoptions
place_not_found = False
else:
max_distance = int(request.POST.get('max_distance')) max_distance = int(request.POST.get('max_distance'))
if max_distance == "": if max_distance == "":
max_distance = None max_distance = None