diff --git a/src/fellchensammlung/forms.py b/src/fellchensammlung/forms.py index f2d4b6d..3f640bf 100644 --- a/src/fellchensammlung/forms.py +++ b/src/fellchensammlung/forms.py @@ -1,7 +1,7 @@ from django import forms from .models import AdoptionNotice, Animal, Image, ReportAdoptionNotice, ReportComment, ModerationAction, User, Species, \ - Comment + Comment, SexChoicesWithAll from django_registration.forms import RegistrationForm from crispy_forms.helper import FormHelper 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 Meta: 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 = { 'searching_since': DateInput(), } @@ -185,7 +186,7 @@ def _get_distances(): 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")) 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 a0c3418..3daf124 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -178,16 +178,21 @@ def search(request): 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')) - if max_distance == "": - max_distance = None - geo_api = GeoAPI() - search_position = geo_api.get_coordinates_from_query(request.POST['location']) - if search_position is None: - place_not_found = True + search_form.is_valid() + if search_form.cleaned_data["location"] == "": adoption_notices_in_distance = active_adoptions + place_not_found = False else: - adoption_notices_in_distance = [a for a in active_adoptions if a.in_distance(search_position, max_distance)] + max_distance = int(request.POST.get('max_distance')) + if max_distance == "": + max_distance = None + geo_api = GeoAPI() + search_position = geo_api.get_coordinates_from_query(request.POST['location']) + if search_position is None: + place_not_found = True + adoption_notices_in_distance = active_adoptions + else: + adoption_notices_in_distance = [a for a in active_adoptions if a.in_distance(search_position, max_distance)] context = {"adoption_notices": adoption_notices_in_distance, "search_form": search_form, "place_not_found": place_not_found}