refactor: Move get_location to model to avoid circulari mports
This commit is contained in:
parent
6799dbe901
commit
d2219188d3
@ -10,7 +10,7 @@ from django.db.models.signals import post_save
|
|||||||
from django.contrib.auth.models import Group
|
from django.contrib.auth.models import Group
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
|
|
||||||
from fellchensammlung.tools import misc
|
from fellchensammlung.tools import misc, geo
|
||||||
from notfellchen.settings import MEDIA_URL
|
from notfellchen.settings import MEDIA_URL
|
||||||
|
|
||||||
|
|
||||||
@ -71,6 +71,26 @@ class Location(models.Model):
|
|||||||
longitude = models.FloatField()
|
longitude = models.FloatField()
|
||||||
name = models.CharField(max_length=2000)
|
name = models.CharField(max_length=2000)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.name} ({self.latitude:.5}, {self.longitude:.5})"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_location_from_string(location_string):
|
||||||
|
geo_api = geo.GeoAPI()
|
||||||
|
get_geojson = geo_api.get_geojson_for_query(location_string)
|
||||||
|
result = get_geojson[0]
|
||||||
|
if "name" in result:
|
||||||
|
name = result["name"]
|
||||||
|
else:
|
||||||
|
name = result["display_name"]
|
||||||
|
location = Location.objects.create(
|
||||||
|
place_id=result["place_id"],
|
||||||
|
latitude=result["lat"],
|
||||||
|
longitude=result["lon"],
|
||||||
|
name=name,
|
||||||
|
)
|
||||||
|
return location
|
||||||
|
|
||||||
|
|
||||||
class RescueOrganization(models.Model):
|
class RescueOrganization(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -3,7 +3,6 @@ import logging
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
from notfellchen import __version__ as nf_version
|
from notfellchen import __version__ as nf_version
|
||||||
from fellchensammlung.models import Location
|
|
||||||
from notfellchen import settings
|
from notfellchen import settings
|
||||||
|
|
||||||
from math import radians, sqrt, sin, cos, atan2
|
from math import radians, sqrt, sin, cos, atan2
|
||||||
@ -69,7 +68,7 @@ class GeoAPI:
|
|||||||
result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers)
|
result = self.requests.get(self.api_url, {"q": location_string, "format": "jsonv2"}, headers=self.headers)
|
||||||
return result.content
|
return result.content
|
||||||
|
|
||||||
def get_location_from_string(self, location_string):
|
def get_geojson_for_query(self, location_string):
|
||||||
try:
|
try:
|
||||||
result = self.requests.get(self.api_url,
|
result = self.requests.get(self.api_url,
|
||||||
{"q": location_string,
|
{"q": location_string,
|
||||||
@ -81,19 +80,7 @@ class GeoAPI:
|
|||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
logging.warning(f"Couldn't find a result for {location_string} when querying Nominatim")
|
logging.warning(f"Couldn't find a result for {location_string} when querying Nominatim")
|
||||||
return None
|
return None
|
||||||
|
return result
|
||||||
result = result[0]
|
|
||||||
if "name" in result:
|
|
||||||
name = result["name"]
|
|
||||||
else:
|
|
||||||
name = result["display_name"]
|
|
||||||
location = Location.objects.create(
|
|
||||||
place_id=result["place_id"],
|
|
||||||
latitude=result["lat"],
|
|
||||||
longitude=result["lon"],
|
|
||||||
name=name,
|
|
||||||
)
|
|
||||||
return location
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user