feat: Extend location model to allow specifying address

This commit is contained in:
moanos [he/him] 2025-04-24 19:43:48 +02:00
parent 9950e87501
commit f9a37b299d
4 changed files with 64 additions and 7 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 5.1.4 on 2025-04-24 17:13
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('fellchensammlung', '0041_location_city_location_country_location_housenumber_and_more'),
]
operations = [
migrations.AddField(
model_name='location',
name='county',
field=models.CharField(blank=True, max_length=200, null=True),
),
]

View File

@ -0,0 +1,18 @@
# Generated by Django 5.1.4 on 2025-04-24 17:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('fellchensammlung', '0042_location_county'),
]
operations = [
migrations.RenameField(
model_name='location',
old_name='country',
new_name='countrycode',
),
]

View File

@ -47,11 +47,12 @@ class Location(models.Model):
housenumber = models.CharField(max_length=20, blank=True, null=True)
postcode = models.CharField(max_length=20, blank=True, null=True)
street = models.CharField(max_length=200, blank=True, null=True)
county = models.CharField(max_length=200, blank=True, null=True)
# Country code as per ISO 3166-1 alpha-2
# https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
country = models.CharField(max_length=2, verbose_name=_("Ländercode"),
help_text=_("Standardisierter Ländercode nach ISO 3166-1 ALPHA-2"),
blank=True, null=True)
countrycode = models.CharField(max_length=2, verbose_name=_("Ländercode"),
help_text=_("Standardisierter Ländercode nach ISO 3166-1 ALPHA-2"),
blank=True, null=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
@ -82,6 +83,11 @@ class Location(models.Model):
latitude=proxy.latitude,
longitude=proxy.longitude,
name=proxy.name,
postcode=proxy.postcode,
city=proxy.city,
street=proxy.street,
county=proxy.county,
countrycode=proxy.countrycode,
)
return location

View File

@ -74,13 +74,21 @@ class GeoFeature:
geofeatures = []
for feature in result["features"]:
geojson = {}
try:
geojson['name'] = feature["properties"]["name"]
except KeyError:
geojson['name'] = feature["properties"]["street"]
# Necessary features
geojson['place_id'] = feature["properties"]["osm_id"]
geojson['lat'] = feature["geometry"]["coordinates"][1]
geojson['lon'] = feature["geometry"]["coordinates"][0]
try:
geojson['name'] = feature["properties"]["name"]
except KeyError:
geojson['name'] = feature["properties"]["osm_id"]
optional_keys = ["housenumber", "street", "city", "postcode", "county", "countrycode"]
for key in optional_keys:
try:
geojson[key] = feature["properties"][key]
except KeyError:
pass
geofeatures.append(geojson)
return geofeatures
@ -161,6 +169,7 @@ class LocationProxy:
"""
self.geo_api = GeoAPI()
geofeatures = self.geo_api.get_geojson_for_query(location_string)
if geofeatures is None:
raise ValueError
result = geofeatures[0]
@ -168,6 +177,12 @@ class LocationProxy:
self.place_id = result["place_id"]
self.latitude = result["lat"]
self.longitude = result["lon"]
optional_keys = ["housenumber", "street", "city", "postcode", "county", "countrycode"]
for key in optional_keys:
try:
self.__setattr__(key, result[key])
except KeyError:
self.__setattr__(key, None)
def __eq__(self, other):
return self.place_id == other.place_id