feat: allow not searching for location
This commit is contained in:
parent
a42a3fa177
commit
885622e581
@ -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)
|
||||||
|
@ -178,16 +178,21 @@ 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)
|
||||||
max_distance = int(request.POST.get('max_distance'))
|
search_form.is_valid()
|
||||||
if max_distance == "":
|
if search_form.cleaned_data["location"] == "":
|
||||||
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
|
adoption_notices_in_distance = active_adoptions
|
||||||
|
place_not_found = False
|
||||||
else:
|
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,
|
context = {"adoption_notices": adoption_notices_in_distance, "search_form": search_form,
|
||||||
"place_not_found": place_not_found}
|
"place_not_found": place_not_found}
|
||||||
|
Loading…
Reference in New Issue
Block a user