diff --git a/src/fellchensammlung/templates/fellchensammlung/search.html b/src/fellchensammlung/templates/fellchensammlung/search.html index 6bcdaea..ccd4ce8 100644 --- a/src/fellchensammlung/templates/fellchensammlung/search.html +++ b/src/fellchensammlung/templates/fellchensammlung/search.html @@ -9,5 +9,8 @@ {{ search_form.as_p }} + {% if not place_found %} +

{% translate "Ort nicht gefunden" %}

+ {% endif %} {% include "fellchensammlung/lists/list-adoption-notices.html" %} {% endblock %} \ No newline at end of file diff --git a/src/fellchensammlung/tools/geo.py b/src/fellchensammlung/tools/geo.py index 7bf03ad..e1297a7 100644 --- a/src/fellchensammlung/tools/geo.py +++ b/src/fellchensammlung/tools/geo.py @@ -49,19 +49,25 @@ class RequestMock: class GeoAPI: api_url = settings.GEOCODING_API_URL + # Set User-Agent headers as required by most usage policies (and it's the nice thing to do) headers = { 'User-Agent': f"Notfellchen {nf_version}", 'From': 'info@notfellchen.org' # This is another valid field } def __init__(self, debug=False): + # If debug mode is on, we replace the actual goecoding server with a cheap local mock + # In order to do this without changing how we normally do things, we replace the requests library with our mock if debug: self.requests = RequestMock else: self.requests = requests def get_coordinates_from_query(self, location_string): - result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers).json()[0] + try: + result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers).json()[0] + except IndexError: + return None return result["lat"], result["lon"] def _get_raw_response(self, location_string): diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index 6e9aa83..77f7bfc 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -138,17 +138,23 @@ def animal_detail(request, animal_id): def search(request): 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] + 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['postcode']) - - latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") - active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] - 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} + if search_position is None: + place_found = False + adoption_notices_in_distance = active_adoptions + else: + place_found = True + 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_found": place_found} else: latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active]