From f9a37b299d419bbdef67184cc1b60b2ccf198244 Mon Sep 17 00:00:00 2001 From: moanos Date: Thu, 24 Apr 2025 19:43:48 +0200 Subject: [PATCH] feat: Extend location model to allow specifying address --- .../migrations/0042_location_county.py | 18 +++++++++++++++ ...043_rename_country_location_countrycode.py | 18 +++++++++++++++ src/fellchensammlung/models.py | 12 +++++++--- src/fellchensammlung/tools/geo.py | 23 +++++++++++++++---- 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/fellchensammlung/migrations/0042_location_county.py create mode 100644 src/fellchensammlung/migrations/0043_rename_country_location_countrycode.py diff --git a/src/fellchensammlung/migrations/0042_location_county.py b/src/fellchensammlung/migrations/0042_location_county.py new file mode 100644 index 0000000..44b69f5 --- /dev/null +++ b/src/fellchensammlung/migrations/0042_location_county.py @@ -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), + ), + ] diff --git a/src/fellchensammlung/migrations/0043_rename_country_location_countrycode.py b/src/fellchensammlung/migrations/0043_rename_country_location_countrycode.py new file mode 100644 index 0000000..8c43bfa --- /dev/null +++ b/src/fellchensammlung/migrations/0043_rename_country_location_countrycode.py @@ -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', + ), + ] diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index c31c7d7..1edbf2f 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -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 diff --git a/src/fellchensammlung/tools/geo.py b/src/fellchensammlung/tools/geo.py index 339ed22..4674f5e 100644 --- a/src/fellchensammlung/tools/geo.py +++ b/src/fellchensammlung/tools/geo.py @@ -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