fix: Handle if place of search is not found

This commit is contained in:
moanos [he/him] 2024-09-28 22:47:39 +02:00
parent d55d22b86c
commit 32eb7e8083
3 changed files with 21 additions and 6 deletions

View File

@ -9,5 +9,8 @@
{{ search_form.as_p }} {{ search_form.as_p }}
<input class="btn" type="submit" value="Search" name="search"> <input class="btn" type="submit" value="Search" name="search">
</form> </form>
{% if not place_found %}
<p class="error">{% translate "Ort nicht gefunden" %}</p>
{% endif %}
{% include "fellchensammlung/lists/list-adoption-notices.html" %} {% include "fellchensammlung/lists/list-adoption-notices.html" %}
{% endblock %} {% endblock %}

View File

@ -49,19 +49,25 @@ class RequestMock:
class GeoAPI: class GeoAPI:
api_url = settings.GEOCODING_API_URL 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 = { headers = {
'User-Agent': f"Notfellchen {nf_version}", 'User-Agent': f"Notfellchen {nf_version}",
'From': 'info@notfellchen.org' # This is another valid field 'From': 'info@notfellchen.org' # This is another valid field
} }
def __init__(self, debug=False): 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: if debug:
self.requests = RequestMock self.requests = RequestMock
else: else:
self.requests = requests self.requests = requests
def get_coordinates_from_query(self, location_string): def get_coordinates_from_query(self, location_string):
try:
result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers).json()[0] 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"] return result["lat"], result["lon"]
def _get_raw_response(self, location_string): def _get_raw_response(self, location_string):

View File

@ -138,17 +138,23 @@ def animal_detail(request, animal_id):
def search(request): def search(request):
if request.method == 'POST': 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) search_form = AdoptionNoticeSearchForm(request.POST)
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
geo_api = GeoAPI() geo_api = GeoAPI()
search_position = geo_api.get_coordinates_from_query(request.POST['postcode']) search_position = geo_api.get_coordinates_from_query(request.POST['postcode'])
if search_position is None:
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") place_found = False
active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] 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)] 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_found": place_found}
else: else:
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")
active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active] active_adoptions = [adoption for adoption in latest_adoption_list if adoption.is_active]