diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 0fd9f77..7075826 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -134,6 +134,12 @@ class Location(models.Model): ) return location + @staticmethod + def add_location_to_object(instance): + """Search the location given in the location string and add it to the object""" + location = Location.get_location_from_string(instance.location_string) + instance.location = location + instance.save() class RescueOrganization(models.Model): def __str__(self): diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index 174aad6..05d6d51 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -1,6 +1,15 @@ from notfellchen.celery import app as celery_app from .tools.admin import clean_locations +from .models import Location, AdoptionNotice + @celery_app.task(name="admin.clean_locations") def task_clean_locations(): clean_locations() + + +@celery_app.task(name="commit.add_location") +def add_adoption_notice_location(pk): + instance = AdoptionNotice.objects.get(pk=pk) + Location.add_location_to_object(instance) + diff --git a/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html b/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html index ef32db8..0e2d16a 100644 --- a/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html +++ b/src/fellchensammlung/templates/fellchensammlung/details/detail_adoption_notice.html @@ -2,7 +2,7 @@ {% load custom_tags %} {% load i18n %} -{% block title %}{{adoption_notice.name }} %} +{% block title %}{{adoption_notice.name }}{% endblock %} {% block content %}
diff --git a/src/fellchensammlung/tools/geo.py b/src/fellchensammlung/tools/geo.py index ceacbfc..dad98df 100644 --- a/src/fellchensammlung/tools/geo.py +++ b/src/fellchensammlung/tools/geo.py @@ -7,7 +7,6 @@ from notfellchen import __version__ as nf_version from notfellchen import settings - def calculate_distance_between_coordinates(position1, position2): """ Calculate the distance between two points identified by coordinates @@ -65,7 +64,8 @@ class GeoAPI: 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"] @@ -77,9 +77,9 @@ class GeoAPI: def get_geojson_for_query(self, location_string): try: result = self.requests.get(self.api_url, - {"q": location_string, - "format": "jsonv2"}, - headers=self.headers).json() + {"q": location_string, + "format": "jsonv2"}, + headers=self.headers).json() except Exception as e: logging.warning(f"Exception {e} when querying Nominatim") return None diff --git a/src/fellchensammlung/views.py b/src/fellchensammlung/views.py index a3fc810..7c9fe6e 100644 --- a/src/fellchensammlung/views.py +++ b/src/fellchensammlung/views.py @@ -22,6 +22,7 @@ from .models import Language, Announcement from .tools.geo import GeoAPI from .tools.metrics import gather_metrics_data from .tools.admin import clean_locations +from .tasks import add_adoption_notice_location def user_is_trust_level_or_above(user, trust_level=User.MODERATOR): @@ -188,11 +189,11 @@ def add_adoption_notice(request): if form.is_valid(): instance = form.save(commit=False) instance.owner = request.user - """Search the location given in the location string and add it to the adoption notice""" - location = Location.get_location_from_string(instance.location_string) - instance.location = location instance.save() + """Spin up a task that adds the location""" + add_adoption_notice_location.delay_on_commit(instance.pk) + # Set correct status if request.user.trust_level >= User.TRUST_LEVEL[User.COORDINATOR]: major_status = AdoptionNoticeStatus.ACTIVE diff --git a/src/notfellchen/__init__.py b/src/notfellchen/__init__.py index d3ec452..d3a5f12 100644 --- a/src/notfellchen/__init__.py +++ b/src/notfellchen/__init__.py @@ -1 +1,8 @@ __version__ = "0.2.0" + +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ('celery_app',) +