Skip to content

Commit

Permalink
refact(geo): add extra properties, created, updated attrs; help text
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlougheed committed Feb 3, 2025
1 parent 78a9ee9 commit 50938ce
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 27 deletions.
25 changes: 19 additions & 6 deletions chord_metadata_service/geo/descriptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
__all__ = ["GEO_LOCATION"]
__all__ = [
"PROP_LABEL",
"PROP_CITY",
"PROP_COUNTRY",
"PROP_ISO3166_ALPHA_3",
"PROP_PRECISION",
"GEO_LOCATION",
]

PROP_LABEL = "Address or other human-readable location name."
PROP_CITY = "Optional name of the city where this location rests."
PROP_COUNTRY = "Optional name of the country where this location rests."
PROP_ISO3166_ALPHA_3 = "Optional ISO 3166-1 alpha 3 country code (three letters)."
PROP_PRECISION = "Optional, human-readable indication of how precise this location is (e.g., \"city\")."

GEO_LOCATION = {
"description": "A GeoJSON-compatible object representing a geographic location.",
Expand All @@ -20,11 +33,11 @@
"schema block."
),
"properties": {
"label": "Address or other human-readable location name.",
"city": "Optional name of the city where this location rests.",
"country": "Optional name of the country where this location rests.",
"ISO3166alpha3": "Optional ISO 3166-1 alpha 3 country code (three letters).",
"precision": "Optional, human-readable indication of how precise this location is (e.g., \"city\")."
"label": PROP_LABEL,
"city": PROP_CITY,
"country": PROP_COUNTRY,
"ISO3166alpha3": PROP_ISO3166_ALPHA_3,
"precision": PROP_PRECISION,
},
},
},
Expand Down
18 changes: 11 additions & 7 deletions chord_metadata_service/geo/migrations/0001_v11_0_0.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by Django 5.0.11 on 2025-01-29 22:17
# Generated by Django 5.0.11 on 2025-02-03 20:14

import chord_metadata_service.restapi.validators
import django.contrib.gis.db.models.fields
from django.db import migrations, models

Expand All @@ -16,12 +17,15 @@ class Migration(migrations.Migration):
name='GeoLocation',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('point', django.contrib.gis.db.models.fields.PointField(srid=4326)),
('label', models.TextField(blank=True)),
('city', models.TextField(blank=True)),
('country', models.TextField(blank=True)),
('iso_a3_code', models.CharField(choices=[('ABW', 'Aruba'), ('AFG', 'Afghanistan'), ('AGO', 'Angola'), ('AIA', 'Anguilla'), ('ALA', 'Åland Islands'), ('ALB', 'Albania'), ('AND', 'Andorra'), ('ARE', 'United Arab Emirates'), ('ARG', 'Argentina'), ('ARM', 'Armenia'), ('ASM', 'American Samoa'), ('ATA', 'Antarctica'), ('ATF', 'French Southern Territories'), ('ATG', 'Antigua and Barbuda'), ('AUS', 'Australia'), ('AUT', 'Austria'), ('AZE', 'Azerbaijan'), ('BDI', 'Burundi'), ('BEL', 'Belgium'), ('BEN', 'Benin'), ('BES', 'Bonaire, Sint Eustatius and Saba'), ('BFA', 'Burkina Faso'), ('BGD', 'Bangladesh'), ('BGR', 'Bulgaria'), ('BHR', 'Bahrain'), ('BHS', 'Bahamas'), ('BIH', 'Bosnia and Herzegovina'), ('BLM', 'Saint Barthélemy'), ('BLR', 'Belarus'), ('BLZ', 'Belize'), ('BMU', 'Bermuda'), ('BOL', 'Bolivia, Plurinational State of'), ('BRA', 'Brazil'), ('BRB', 'Barbados'), ('BRN', 'Brunei Darussalam'), ('BTN', 'Bhutan'), ('BVT', 'Bouvet Island'), ('BWA', 'Botswana'), ('CAF', 'Central African Republic'), ('CAN', 'Canada'), ('CCK', 'Cocos (Keeling) Islands'), ('CHE', 'Switzerland'), ('CHL', 'Chile'), ('CHN', 'China'), ('CIV', "Côte d'Ivoire"), ('CMR', 'Cameroon'), ('COD', 'Congo, The Democratic Republic of the'), ('COG', 'Congo'), ('COK', 'Cook Islands'), ('COL', 'Colombia'), ('COM', 'Comoros'), ('CPV', 'Cabo Verde'), ('CRI', 'Costa Rica'), ('CUB', 'Cuba'), ('CUW', 'Curaçao'), ('CXR', 'Christmas Island'), ('CYM', 'Cayman Islands'), ('CYP', 'Cyprus'), ('CZE', 'Czechia'), ('DEU', 'Germany'), ('DJI', 'Djibouti'), ('DMA', 'Dominica'), ('DNK', 'Denmark'), ('DOM', 'Dominican Republic'), ('DZA', 'Algeria'), ('ECU', 'Ecuador'), ('EGY', 'Egypt'), ('ERI', 'Eritrea'), ('ESH', 'Western Sahara'), ('ESP', 'Spain'), ('EST', 'Estonia'), ('ETH', 'Ethiopia'), ('FIN', 'Finland'), ('FJI', 'Fiji'), ('FLK', 'Falkland Islands (Malvinas)'), ('FRA', 'France'), ('FRO', 'Faroe Islands'), ('FSM', 'Micronesia, Federated States of'), ('GAB', 'Gabon'), ('GBR', 'United Kingdom'), ('GEO', 'Georgia'), ('GGY', 'Guernsey'), ('GHA', 'Ghana'), ('GIB', 'Gibraltar'), ('GIN', 'Guinea'), ('GLP', 'Guadeloupe'), ('GMB', 'Gambia'), ('GNB', 'Guinea-Bissau'), ('GNQ', 'Equatorial Guinea'), ('GRC', 'Greece'), ('GRD', 'Grenada'), ('GRL', 'Greenland'), ('GTM', 'Guatemala'), ('GUF', 'French Guiana'), ('GUM', 'Guam'), ('GUY', 'Guyana'), ('HKG', 'Hong Kong'), ('HMD', 'Heard Island and McDonald Islands'), ('HND', 'Honduras'), ('HRV', 'Croatia'), ('HTI', 'Haiti'), ('HUN', 'Hungary'), ('IDN', 'Indonesia'), ('IMN', 'Isle of Man'), ('IND', 'India'), ('IOT', 'British Indian Ocean Territory'), ('IRL', 'Ireland'), ('IRN', 'Iran, Islamic Republic of'), ('IRQ', 'Iraq'), ('ISL', 'Iceland'), ('ISR', 'Israel'), ('ITA', 'Italy'), ('JAM', 'Jamaica'), ('JEY', 'Jersey'), ('JOR', 'Jordan'), ('JPN', 'Japan'), ('KAZ', 'Kazakhstan'), ('KEN', 'Kenya'), ('KGZ', 'Kyrgyzstan'), ('KHM', 'Cambodia'), ('KIR', 'Kiribati'), ('KNA', 'Saint Kitts and Nevis'), ('KOR', 'Korea, Republic of'), ('KWT', 'Kuwait'), ('LAO', "Lao People's Democratic Republic"), ('LBN', 'Lebanon'), ('LBR', 'Liberia'), ('LBY', 'Libya'), ('LCA', 'Saint Lucia'), ('LIE', 'Liechtenstein'), ('LKA', 'Sri Lanka'), ('LSO', 'Lesotho'), ('LTU', 'Lithuania'), ('LUX', 'Luxembourg'), ('LVA', 'Latvia'), ('MAC', 'Macao'), ('MAF', 'Saint Martin (French part)'), ('MAR', 'Morocco'), ('MCO', 'Monaco'), ('MDA', 'Moldova, Republic of'), ('MDG', 'Madagascar'), ('MDV', 'Maldives'), ('MEX', 'Mexico'), ('MHL', 'Marshall Islands'), ('MKD', 'North Macedonia'), ('MLI', 'Mali'), ('MLT', 'Malta'), ('MMR', 'Myanmar'), ('MNE', 'Montenegro'), ('MNG', 'Mongolia'), ('MNP', 'Northern Mariana Islands'), ('MOZ', 'Mozambique'), ('MRT', 'Mauritania'), ('MSR', 'Montserrat'), ('MTQ', 'Martinique'), ('MUS', 'Mauritius'), ('MWI', 'Malawi'), ('MYS', 'Malaysia'), ('MYT', 'Mayotte'), ('NAM', 'Namibia'), ('NCL', 'New Caledonia'), ('NER', 'Niger'), ('NFK', 'Norfolk Island'), ('NGA', 'Nigeria'), ('NIC', 'Nicaragua'), ('NIU', 'Niue'), ('NLD', 'Netherlands'), ('NOR', 'Norway'), ('NPL', 'Nepal'), ('NRU', 'Nauru'), ('NZL', 'New Zealand'), ('OMN', 'Oman'), ('PAK', 'Pakistan'), ('PAN', 'Panama'), ('PCN', 'Pitcairn'), ('PER', 'Peru'), ('PHL', 'Philippines'), ('PLW', 'Palau'), ('PNG', 'Papua New Guinea'), ('POL', 'Poland'), ('PRI', 'Puerto Rico'), ('PRK', "Korea, Democratic People's Republic of"), ('PRT', 'Portugal'), ('PRY', 'Paraguay'), ('PSE', 'Palestine, State of'), ('PYF', 'French Polynesia'), ('QAT', 'Qatar'), ('REU', 'Réunion'), ('ROU', 'Romania'), ('RUS', 'Russian Federation'), ('RWA', 'Rwanda'), ('SAU', 'Saudi Arabia'), ('SDN', 'Sudan'), ('SEN', 'Senegal'), ('SGP', 'Singapore'), ('SGS', 'South Georgia and the South Sandwich Islands'), ('SHN', 'Saint Helena, Ascension and Tristan da Cunha'), ('SJM', 'Svalbard and Jan Mayen'), ('SLB', 'Solomon Islands'), ('SLE', 'Sierra Leone'), ('SLV', 'El Salvador'), ('SMR', 'San Marino'), ('SOM', 'Somalia'), ('SPM', 'Saint Pierre and Miquelon'), ('SRB', 'Serbia'), ('SSD', 'South Sudan'), ('STP', 'Sao Tome and Principe'), ('SUR', 'Suriname'), ('SVK', 'Slovakia'), ('SVN', 'Slovenia'), ('SWE', 'Sweden'), ('SWZ', 'Eswatini'), ('SXM', 'Sint Maarten (Dutch part)'), ('SYC', 'Seychelles'), ('SYR', 'Syrian Arab Republic'), ('TCA', 'Turks and Caicos Islands'), ('TCD', 'Chad'), ('TGO', 'Togo'), ('THA', 'Thailand'), ('TJK', 'Tajikistan'), ('TKL', 'Tokelau'), ('TKM', 'Turkmenistan'), ('TLS', 'Timor-Leste'), ('TON', 'Tonga'), ('TTO', 'Trinidad and Tobago'), ('TUN', 'Tunisia'), ('TUR', 'Türkiye'), ('TUV', 'Tuvalu'), ('TWN', 'Taiwan, Province of China'), ('TZA', 'Tanzania, United Republic of'), ('UGA', 'Uganda'), ('UKR', 'Ukraine'), ('UMI', 'United States Minor Outlying Islands'), ('URY', 'Uruguay'), ('USA', 'United States'), ('UZB', 'Uzbekistan'), ('VAT', 'Holy See (Vatican City State)'), ('VCT', 'Saint Vincent and the Grenadines'), ('VEN', 'Venezuela, Bolivarian Republic of'), ('VGB', 'Virgin Islands, British'), ('VIR', 'Virgin Islands, U.S.'), ('VNM', 'Viet Nam'), ('VUT', 'Vanuatu'), ('WLF', 'Wallis and Futuna'), ('WSM', 'Samoa'), ('YEM', 'Yemen'), ('ZAF', 'South Africa'), ('ZMB', 'Zambia'), ('ZWE', 'Zimbabwe')], default=None, max_length=3, null=True)),
('precision', models.TextField(blank=True)),
('point', django.contrib.gis.db.models.fields.PointField(help_text='Point (coordinates) specifying a precise geographic location.', srid=4326)),
('label', models.TextField(blank=True, default='', help_text='Address or other human-readable location name.')),
('city', models.TextField(blank=True, default='', help_text='Optional name of the city where this location rests.')),
('country', models.TextField(blank=True, default='', help_text='Optional name of the country where this location rests.')),
('iso_a3_code', models.CharField(choices=[('ABW', 'Aruba'), ('AFG', 'Afghanistan'), ('AGO', 'Angola'), ('AIA', 'Anguilla'), ('ALA', 'Åland Islands'), ('ALB', 'Albania'), ('AND', 'Andorra'), ('ARE', 'United Arab Emirates'), ('ARG', 'Argentina'), ('ARM', 'Armenia'), ('ASM', 'American Samoa'), ('ATA', 'Antarctica'), ('ATF', 'French Southern Territories'), ('ATG', 'Antigua and Barbuda'), ('AUS', 'Australia'), ('AUT', 'Austria'), ('AZE', 'Azerbaijan'), ('BDI', 'Burundi'), ('BEL', 'Belgium'), ('BEN', 'Benin'), ('BES', 'Bonaire, Sint Eustatius and Saba'), ('BFA', 'Burkina Faso'), ('BGD', 'Bangladesh'), ('BGR', 'Bulgaria'), ('BHR', 'Bahrain'), ('BHS', 'Bahamas'), ('BIH', 'Bosnia and Herzegovina'), ('BLM', 'Saint Barthélemy'), ('BLR', 'Belarus'), ('BLZ', 'Belize'), ('BMU', 'Bermuda'), ('BOL', 'Bolivia, Plurinational State of'), ('BRA', 'Brazil'), ('BRB', 'Barbados'), ('BRN', 'Brunei Darussalam'), ('BTN', 'Bhutan'), ('BVT', 'Bouvet Island'), ('BWA', 'Botswana'), ('CAF', 'Central African Republic'), ('CAN', 'Canada'), ('CCK', 'Cocos (Keeling) Islands'), ('CHE', 'Switzerland'), ('CHL', 'Chile'), ('CHN', 'China'), ('CIV', "Côte d'Ivoire"), ('CMR', 'Cameroon'), ('COD', 'Congo, The Democratic Republic of the'), ('COG', 'Congo'), ('COK', 'Cook Islands'), ('COL', 'Colombia'), ('COM', 'Comoros'), ('CPV', 'Cabo Verde'), ('CRI', 'Costa Rica'), ('CUB', 'Cuba'), ('CUW', 'Curaçao'), ('CXR', 'Christmas Island'), ('CYM', 'Cayman Islands'), ('CYP', 'Cyprus'), ('CZE', 'Czechia'), ('DEU', 'Germany'), ('DJI', 'Djibouti'), ('DMA', 'Dominica'), ('DNK', 'Denmark'), ('DOM', 'Dominican Republic'), ('DZA', 'Algeria'), ('ECU', 'Ecuador'), ('EGY', 'Egypt'), ('ERI', 'Eritrea'), ('ESH', 'Western Sahara'), ('ESP', 'Spain'), ('EST', 'Estonia'), ('ETH', 'Ethiopia'), ('FIN', 'Finland'), ('FJI', 'Fiji'), ('FLK', 'Falkland Islands (Malvinas)'), ('FRA', 'France'), ('FRO', 'Faroe Islands'), ('FSM', 'Micronesia, Federated States of'), ('GAB', 'Gabon'), ('GBR', 'United Kingdom'), ('GEO', 'Georgia'), ('GGY', 'Guernsey'), ('GHA', 'Ghana'), ('GIB', 'Gibraltar'), ('GIN', 'Guinea'), ('GLP', 'Guadeloupe'), ('GMB', 'Gambia'), ('GNB', 'Guinea-Bissau'), ('GNQ', 'Equatorial Guinea'), ('GRC', 'Greece'), ('GRD', 'Grenada'), ('GRL', 'Greenland'), ('GTM', 'Guatemala'), ('GUF', 'French Guiana'), ('GUM', 'Guam'), ('GUY', 'Guyana'), ('HKG', 'Hong Kong'), ('HMD', 'Heard Island and McDonald Islands'), ('HND', 'Honduras'), ('HRV', 'Croatia'), ('HTI', 'Haiti'), ('HUN', 'Hungary'), ('IDN', 'Indonesia'), ('IMN', 'Isle of Man'), ('IND', 'India'), ('IOT', 'British Indian Ocean Territory'), ('IRL', 'Ireland'), ('IRN', 'Iran, Islamic Republic of'), ('IRQ', 'Iraq'), ('ISL', 'Iceland'), ('ISR', 'Israel'), ('ITA', 'Italy'), ('JAM', 'Jamaica'), ('JEY', 'Jersey'), ('JOR', 'Jordan'), ('JPN', 'Japan'), ('KAZ', 'Kazakhstan'), ('KEN', 'Kenya'), ('KGZ', 'Kyrgyzstan'), ('KHM', 'Cambodia'), ('KIR', 'Kiribati'), ('KNA', 'Saint Kitts and Nevis'), ('KOR', 'Korea, Republic of'), ('KWT', 'Kuwait'), ('LAO', "Lao People's Democratic Republic"), ('LBN', 'Lebanon'), ('LBR', 'Liberia'), ('LBY', 'Libya'), ('LCA', 'Saint Lucia'), ('LIE', 'Liechtenstein'), ('LKA', 'Sri Lanka'), ('LSO', 'Lesotho'), ('LTU', 'Lithuania'), ('LUX', 'Luxembourg'), ('LVA', 'Latvia'), ('MAC', 'Macao'), ('MAF', 'Saint Martin (French part)'), ('MAR', 'Morocco'), ('MCO', 'Monaco'), ('MDA', 'Moldova, Republic of'), ('MDG', 'Madagascar'), ('MDV', 'Maldives'), ('MEX', 'Mexico'), ('MHL', 'Marshall Islands'), ('MKD', 'North Macedonia'), ('MLI', 'Mali'), ('MLT', 'Malta'), ('MMR', 'Myanmar'), ('MNE', 'Montenegro'), ('MNG', 'Mongolia'), ('MNP', 'Northern Mariana Islands'), ('MOZ', 'Mozambique'), ('MRT', 'Mauritania'), ('MSR', 'Montserrat'), ('MTQ', 'Martinique'), ('MUS', 'Mauritius'), ('MWI', 'Malawi'), ('MYS', 'Malaysia'), ('MYT', 'Mayotte'), ('NAM', 'Namibia'), ('NCL', 'New Caledonia'), ('NER', 'Niger'), ('NFK', 'Norfolk Island'), ('NGA', 'Nigeria'), ('NIC', 'Nicaragua'), ('NIU', 'Niue'), ('NLD', 'Netherlands'), ('NOR', 'Norway'), ('NPL', 'Nepal'), ('NRU', 'Nauru'), ('NZL', 'New Zealand'), ('OMN', 'Oman'), ('PAK', 'Pakistan'), ('PAN', 'Panama'), ('PCN', 'Pitcairn'), ('PER', 'Peru'), ('PHL', 'Philippines'), ('PLW', 'Palau'), ('PNG', 'Papua New Guinea'), ('POL', 'Poland'), ('PRI', 'Puerto Rico'), ('PRK', "Korea, Democratic People's Republic of"), ('PRT', 'Portugal'), ('PRY', 'Paraguay'), ('PSE', 'Palestine, State of'), ('PYF', 'French Polynesia'), ('QAT', 'Qatar'), ('REU', 'Réunion'), ('ROU', 'Romania'), ('RUS', 'Russian Federation'), ('RWA', 'Rwanda'), ('SAU', 'Saudi Arabia'), ('SDN', 'Sudan'), ('SEN', 'Senegal'), ('SGP', 'Singapore'), ('SGS', 'South Georgia and the South Sandwich Islands'), ('SHN', 'Saint Helena, Ascension and Tristan da Cunha'), ('SJM', 'Svalbard and Jan Mayen'), ('SLB', 'Solomon Islands'), ('SLE', 'Sierra Leone'), ('SLV', 'El Salvador'), ('SMR', 'San Marino'), ('SOM', 'Somalia'), ('SPM', 'Saint Pierre and Miquelon'), ('SRB', 'Serbia'), ('SSD', 'South Sudan'), ('STP', 'Sao Tome and Principe'), ('SUR', 'Suriname'), ('SVK', 'Slovakia'), ('SVN', 'Slovenia'), ('SWE', 'Sweden'), ('SWZ', 'Eswatini'), ('SXM', 'Sint Maarten (Dutch part)'), ('SYC', 'Seychelles'), ('SYR', 'Syrian Arab Republic'), ('TCA', 'Turks and Caicos Islands'), ('TCD', 'Chad'), ('TGO', 'Togo'), ('THA', 'Thailand'), ('TJK', 'Tajikistan'), ('TKL', 'Tokelau'), ('TKM', 'Turkmenistan'), ('TLS', 'Timor-Leste'), ('TON', 'Tonga'), ('TTO', 'Trinidad and Tobago'), ('TUN', 'Tunisia'), ('TUR', 'Türkiye'), ('TUV', 'Tuvalu'), ('TWN', 'Taiwan, Province of China'), ('TZA', 'Tanzania, United Republic of'), ('UGA', 'Uganda'), ('UKR', 'Ukraine'), ('UMI', 'United States Minor Outlying Islands'), ('URY', 'Uruguay'), ('USA', 'United States'), ('UZB', 'Uzbekistan'), ('VAT', 'Holy See (Vatican City State)'), ('VCT', 'Saint Vincent and the Grenadines'), ('VEN', 'Venezuela, Bolivarian Republic of'), ('VGB', 'Virgin Islands, British'), ('VIR', 'Virgin Islands, U.S.'), ('VNM', 'Viet Nam'), ('VUT', 'Vanuatu'), ('WLF', 'Wallis and Futuna'), ('WSM', 'Samoa'), ('YEM', 'Yemen'), ('ZAF', 'South Africa'), ('ZMB', 'Zambia'), ('ZWE', 'Zimbabwe')], default=None, help_text='Optional ISO 3166-1 alpha 3 country code (three letters).', max_length=3, null=True)),
('precision', models.TextField(blank=True, default='', help_text='Optional ISO 3166-1 alpha 3 country code (three letters).')),
('extra_properties', models.JSONField(blank=True, default=dict, help_text='Extra properties that do not have a predefined field in the database.', validators=[chord_metadata_service.restapi.validators.JsonSchemaValidator({'$id': '/chord_metadata_service/schemas/phenopacket/extra_properties', '$schema': 'http://json-schema.org/draft-07/schema#', 'type': 'object'}, formats=None)])),
('created', models.DateTimeField(auto_now_add=True)),
('updated', models.DateTimeField(auto_now=True)),
],
),
]
42 changes: 32 additions & 10 deletions chord_metadata_service/geo/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from django.contrib.gis.db import models as geo_models
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point

from chord_metadata_service.restapi.validators import base_extra_properties_validator

from . import descriptions as d
from .constants import ISO_3166_1_ALPHA_3_COUNTRY_CODE_CHOICES


Expand All @@ -9,27 +12,46 @@
]


class GeoLocation(geo_models.Model):
class GeoLocation(models.Model):
"""
Model describing a specific geographical location. Heavily inspired by the Progenetix GeoLocation schema block:
https://schemablocks.org/schema_pages/Progenetix/GeoLocation/
"""

# geometry:
# - serializes into a GeoJSON geometry object when rendering any instances as JSON
point = geo_models.PointField(spatial_index=True)
point = models.PointField(
spatial_index=True, help_text="Point (coordinates) specifying a precise geographic location."
)

# metadata / free-text data:
# - serializes into a GeoJSON properties object when rendering any instances as JSON
label = geo_models.TextField(blank=True)
city = geo_models.TextField(blank=True)
country = geo_models.TextField(blank=True)
iso_a3_code = geo_models.CharField(
max_length=3, choices=ISO_3166_1_ALPHA_3_COUNTRY_CODE_CHOICES, null=True, default=None
label = models.TextField(blank=True, default="", help_text=d.PROP_LABEL)
city = models.TextField(blank=True, default="", help_text=d.PROP_CITY)
country = models.TextField(blank=True, default="", help_text=d.PROP_COUNTRY)
iso_a3_code = models.CharField(
max_length=3,
choices=ISO_3166_1_ALPHA_3_COUNTRY_CODE_CHOICES,
null=True,
default=None,
help_text=d.PROP_ISO3166_ALPHA_3,
)
precision = models.TextField(blank=True, default="", help_text=d.PROP_ISO3166_ALPHA_3)

# properties (mapping to other properties in the GeoJSON object) which do not map to any of the above fields:
extra_properties = models.JSONField(
blank=True,
default=dict,
validators=[base_extra_properties_validator],
help_text="Extra properties that do not have a predefined field in the database.",
)
precision = geo_models.TextField(blank=True)

# TODO: extra properties
# ------------------------------------------------------------------------------------------------------------------

created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)

# ------------------------------------------------------------------------------------------------------------------

def __str__(self):
# noinspection PyTypeChecker
Expand Down
2 changes: 2 additions & 0 deletions chord_metadata_service/geo/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
},
"required": ["type", "coordinates"],
"additionalProperties": False, # Geometry must be just type and coordinates
},
"properties": {
"type": "object",
Expand All @@ -51,6 +52,7 @@
"ISO3166alpha3": enum_of(constants.ISO_3166_1_ALPHA_3_COUNTRY_CODES),
"precision": base_type(SchemaTypes.STRING),
},
"additionalProperties": True, # Explicitly allow "extra properties"
}
},
"required": ["type", "geometry", "properties"],
Expand Down
12 changes: 9 additions & 3 deletions chord_metadata_service/geo/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class GeoLocationPropertiesSerializer(serializers.Serializer):
precision: serializers.CharField(required=False, allow_blank=True)


GEO_LOCATION_PREDEF_ATTRS = ("label", "city", "country", "iso_a3_code", "precision")


class GeoLocationSerializer(serializers.Serializer):

type = serializers.CharField(validators=[type_is_feature])
Expand All @@ -51,8 +54,11 @@ def to_representation(self, instance: GeoLocation):
"coordinates": list(instance.point.coords),
},
"properties": {
MODEL_ATTRS_TO_PREDEF_PROPS[k]: getattr(instance, k)
for k in ("label", "city", "country", "iso_a3_code", "precision")
if getattr(instance, k)
**({k: v for k, v in instance.extra_properties.items() if k not in GEO_LOCATION_PREDEF_ATTRS}),
**({
MODEL_ATTRS_TO_PREDEF_PROPS[k]: getattr(instance, k)
for k in GEO_LOCATION_PREDEF_ATTRS
if getattr(instance, k)
}),
},
}
Loading

0 comments on commit 50938ce

Please sign in to comment.