fix: Handle if place of search is not found
This commit is contained in:
parent
d55d22b86c
commit
32eb7e8083
@ -9,5 +9,8 @@
|
||||
{{ search_form.as_p }}
|
||||
<input class="btn" type="submit" value="Search" name="search">
|
||||
</form>
|
||||
{% if not place_found %}
|
||||
<p class="error">{% translate "Ort nicht gefunden" %}</p>
|
||||
{% endif %}
|
||||
{% include "fellchensammlung/lists/list-adoption-notices.html" %}
|
||||
{% endblock %}
|
@ -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):
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user