feat: Add distance based search option

This commit is contained in:
moanos [he/him] 2024-05-31 13:40:06 +02:00
parent 1f87e486e0
commit 213bb3c45f
2 changed files with 34 additions and 5 deletions

View File

@ -2,5 +2,22 @@
{% load i18n %} {% load i18n %}
{% block content %} {% block content %}
<form method="post">
{% csrf_token %}
<input type="hidden" name="longitude" maxlength="200" id="longitude">
<input type="hidden" name="latitude" maxlength="200" id="latitude">
<label for="postcode">{% translate "Postleitzahl" %}
</label>
<input type="text" name="postcode" maxlength="200" class="textinput form-control" required="" id="postcode">
<select name="max_distance" class="select custom-select" required="" id="id_max_distance">
<option value="" selected="">---------</option>
<option value="25">20km</option>
<option value="50">50km</option>
<option value="100">100km</option>
<option value="200">200km</option>
<option value="500">500km</option>
</select>
<input type="submit" value="Search" name="search">
</form>
{% include "fellchensammlung/lists/list-adoption-notices.html" %} {% include "fellchensammlung/lists/list-adoption-notices.html" %}
{% endblock %} {% endblock %}

View File

@ -12,7 +12,7 @@ from notfellchen import settings
from fellchensammlung import logger from fellchensammlung import logger
from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \ from fellchensammlung.models import AdoptionNotice, Text, Animal, Rule, Image, Report, ModerationAction, \
Member Member, Location
from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, CommentForm, ReportCommentForm, AnimalForm from .forms import AdoptionNoticeForm, ImageForm, ReportAdoptionNoticeForm, CommentForm, ReportCommentForm, AnimalForm
from .models import Language, Announcement from .models import Language, Announcement
from .tools.geo import GeoAPI from .tools.geo import GeoAPI
@ -88,9 +88,21 @@ def animal_detail(request, animal_id):
def search(request): def search(request):
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at") if request.method == 'POST':
context = {"adoption_notices": latest_adoption_list} max_distance = int(request.POST.get('max_distance'))
return render(request, 'fellchensammlung/search.html', context=context) 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")
adoption_notices_in_distance = [a for a in latest_adoption_list if a.in_distance(search_position, max_distance)]
context = {"adoption_notices": adoption_notices_in_distance}
return render(request, 'fellchensammlung/search.html', context=context)
else:
latest_adoption_list = AdoptionNotice.objects.order_by("-created_at")
context = {"adoption_notices": latest_adoption_list}
return render(request, 'fellchensammlung/search.html', context=context)
@login_required @login_required
@ -103,7 +115,7 @@ def add_adoption_notice(request):
"""Search the location given in the location string and add it to the adoption notice""" """Search the location given in the location string and add it to the adoption notice"""
geo_api = GeoAPI() geo_api = GeoAPI()
location = geo_api.get_location_from_string(instance.location_string) location = Location.get_location_from_string(instance.location_string)
instance.location = location instance.location = location
instance.save() instance.save()