feat: Use celery for location queries
This commit is contained in:
parent
39893c2185
commit
28331f105a
@ -134,6 +134,12 @@ class Location(models.Model):
|
|||||||
)
|
)
|
||||||
return location
|
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):
|
class RescueOrganization(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
from notfellchen.celery import app as celery_app
|
from notfellchen.celery import app as celery_app
|
||||||
from .tools.admin import clean_locations
|
from .tools.admin import clean_locations
|
||||||
|
from .models import Location, AdoptionNotice
|
||||||
|
|
||||||
|
|
||||||
@celery_app.task(name="admin.clean_locations")
|
@celery_app.task(name="admin.clean_locations")
|
||||||
def task_clean_locations():
|
def task_clean_locations():
|
||||||
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)
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
{% load custom_tags %}
|
{% load custom_tags %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
{% block title %}<title>{{adoption_notice.name }}</title> %}
|
{% block title %}<title>{{adoption_notice.name }}</title>{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="detail-adoption-notice-header">
|
<div class="detail-adoption-notice-header">
|
||||||
|
@ -7,7 +7,6 @@ from notfellchen import __version__ as nf_version
|
|||||||
from notfellchen import settings
|
from notfellchen import settings
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_distance_between_coordinates(position1, position2):
|
def calculate_distance_between_coordinates(position1, position2):
|
||||||
"""
|
"""
|
||||||
Calculate the distance between two points identified by coordinates
|
Calculate the distance between two points identified by coordinates
|
||||||
@ -65,7 +64,8 @@ class GeoAPI:
|
|||||||
|
|
||||||
def get_coordinates_from_query(self, location_string):
|
def get_coordinates_from_query(self, location_string):
|
||||||
try:
|
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:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
return result["lat"], result["lon"]
|
return result["lat"], result["lon"]
|
||||||
|
@ -22,6 +22,7 @@ from .models import Language, Announcement
|
|||||||
from .tools.geo import GeoAPI
|
from .tools.geo import GeoAPI
|
||||||
from .tools.metrics import gather_metrics_data
|
from .tools.metrics import gather_metrics_data
|
||||||
from .tools.admin import clean_locations
|
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):
|
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():
|
if form.is_valid():
|
||||||
instance = form.save(commit=False)
|
instance = form.save(commit=False)
|
||||||
instance.owner = request.user
|
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()
|
instance.save()
|
||||||
|
|
||||||
|
"""Spin up a task that adds the location"""
|
||||||
|
add_adoption_notice_location.delay_on_commit(instance.pk)
|
||||||
|
|
||||||
# Set correct status
|
# Set correct status
|
||||||
if request.user.trust_level >= User.TRUST_LEVEL[User.COORDINATOR]:
|
if request.user.trust_level >= User.TRUST_LEVEL[User.COORDINATOR]:
|
||||||
major_status = AdoptionNoticeStatus.ACTIVE
|
major_status = AdoptionNoticeStatus.ACTIVE
|
||||||
|
@ -1 +1,8 @@
|
|||||||
__version__ = "0.2.0"
|
__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',)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user