From 657b66e4c8cd2a101ebcf22ab14295c74be5b7d6 Mon Sep 17 00:00:00 2001 From: David Krauth Date: Fri, 19 Jan 2024 17:48:04 +0900 Subject: [PATCH] Updated UN regions --- src/travel/admin.py | 12 +- src/travel/managers.py | 7 +- ...0009_alter_travelalias_options_and_more.py | 155 ++++++++++++++++++ ...region_travelentityinfo_region_and_more.py | 27 +++ src/travel/models.py | 30 ++-- .../templates/travel/entities/detail/co.html | 2 + 6 files changed, 202 insertions(+), 31 deletions(-) create mode 100644 src/travel/migrations/0009_alter_travelalias_options_and_more.py create mode 100644 src/travel/migrations/0010_travelentityinfo_intregion_travelentityinfo_region_and_more.py diff --git a/src/travel/admin.py b/src/travel/admin.py index 345398a..b2b4e86 100644 --- a/src/travel/admin.py +++ b/src/travel/admin.py @@ -93,19 +93,9 @@ class TravelCurrencyAdmin(admin.ModelAdmin): ] -@admin.register(travel.TravelRegion) -class TravelRegionAdmin(admin.ModelAdmin): - list_display = ['name', 'un_code', 'parent'] - - @admin.register(travel.TravelEntityInfo) class TravelEntityInfoAdmin(admin.ModelAdmin): - list_display = [ - 'iso3', - 'denom', - 'tld', - 'region', - ] + list_display = ['iso3', 'denom', 'tld'] filter_horizontal = ['languages', 'neighbors'] raw_id_fields = ['entity',] diff --git a/src/travel/managers.py b/src/travel/managers.py index ce21792..ae1b7be 100644 --- a/src/travel/managers.py +++ b/src/travel/managers.py @@ -72,7 +72,7 @@ class TravelEntityManager(Manager): common_select_related = base_select_related + state_select_related select_related_by_type = { - 'co': base_select_related + capital_select_related + ['continent'], + 'co': base_select_related + capital_select_related + ['continent', 'entityinfo'], 'st': base_select_related + capital_select_related + ['country__capital'], 'ct': common_select_related, 'ap': common_select_related, @@ -116,10 +116,7 @@ def countries(self): ) def country(self, code): - return self.select_related(*self.select_related_by_type['co']).get( - code=code, - type__abbr='co' - ) + return self.countries().get(code=code) def country_dict(self): return dict([(e.code, e) for e in self.countries()]) diff --git a/src/travel/migrations/0009_alter_travelalias_options_and_more.py b/src/travel/migrations/0009_alter_travelalias_options_and_more.py new file mode 100644 index 0000000..5ef23e5 --- /dev/null +++ b/src/travel/migrations/0009_alter_travelalias_options_and_more.py @@ -0,0 +1,155 @@ +# Generated by Django 4.2.8 on 2024-01-19 07:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("travel", "0008_auto_20230711_0701"), + ] + + operations = [ + migrations.AlterModelOptions( + name="travelalias", + options={"verbose_name_plural": "aliases"}, + ), + migrations.AlterModelOptions( + name="travelaliascategory", + options={"verbose_name_plural": "Alias Categories"}, + ), + migrations.AlterModelOptions( + name="travelbucketlist", + options={"verbose_name_plural": "bucket lists"}, + ), + migrations.AlterModelOptions( + name="travelclassification", + options={"verbose_name_plural": "classifications"}, + ), + migrations.AlterModelOptions( + name="travelcurrency", + options={"ordering": ["iso"], "verbose_name_plural": "currencies"}, + ), + migrations.AlterModelOptions( + name="travelentity", + options={"ordering": ("name",), "verbose_name_plural": "entities"}, + ), + migrations.AlterModelOptions( + name="travelentityinfo", + options={"ordering": ["entity"], "verbose_name_plural": "entity info"}, + ), + migrations.AlterModelOptions( + name="travelentitytype", + options={"verbose_name_plural": "entity types"}, + ), + migrations.AlterModelOptions( + name="travelflag", + options={"verbose_name_plural": "flags"}, + ), + migrations.AlterModelOptions( + name="travellanguage", + options={"ordering": ["name"], "verbose_name_plural": "languages"}, + ), + migrations.AlterModelOptions( + name="travellog", + options={ + "get_latest_by": "arrival", + "ordering": ("-arrival",), + "verbose_name_plural": "logs", + }, + ), + migrations.AlterModelOptions( + name="travelprofile", + options={"verbose_name_plural": "profile"}, + ), + migrations.RemoveField( + model_name="travelentityinfo", + name="region", + ), + migrations.AlterField( + model_name="externalreference", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="externalsource", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelalias", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelbucketlist", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelclassification", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelentity", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelentityinfo", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelentitytype", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelflag", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travellanguage", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travellog", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.AlterField( + model_name="travelprofile", + name="id", + field=models.BigAutoField( + auto_created=True, primary_key=True, serialize=False, verbose_name="ID" + ), + ), + migrations.DeleteModel( + name="TravelRegion", + ), + ] diff --git a/src/travel/migrations/0010_travelentityinfo_intregion_travelentityinfo_region_and_more.py b/src/travel/migrations/0010_travelentityinfo_intregion_travelentityinfo_region_and_more.py new file mode 100644 index 0000000..d2b4148 --- /dev/null +++ b/src/travel/migrations/0010_travelentityinfo_intregion_travelentityinfo_region_and_more.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2.8 on 2024-01-19 07:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("travel", "0009_alter_travelalias_options_and_more"), + ] + + operations = [ + migrations.AddField( + model_name="travelentityinfo", + name="intregion", + field=models.CharField(blank=True, max_length=16), + ), + migrations.AddField( + model_name="travelentityinfo", + name="region", + field=models.CharField(blank=True, max_length=10), + ), + migrations.AddField( + model_name="travelentityinfo", + name="subregion", + field=models.CharField(blank=True, max_length=32), + ), + ] diff --git a/src/travel/models.py b/src/travel/models.py index b3fb454..47fab45 100644 --- a/src/travel/models.py +++ b/src/travel/models.py @@ -265,11 +265,7 @@ def extern(self): @cached_property def get_entityinfo(self): try: - return TravelEntityInfo.objects.select_related( - 'currency', - 'region', - 'entity' - ).prefetch_related( + return TravelEntityInfo.objects.select_related('currency', 'entity').prefetch_related( 'languages', 'neighbors', ).get(entity=self) @@ -525,15 +521,6 @@ def __init__(self, entity, location): self.url = settings.MEDIA_URL + '/'.join(['travel/img', location, fn]) -class TravelRegion(models.Model): - name = models.CharField(max_length=50) - un_code = models.CharField(max_length=5, db_index=True) - parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE) - - class Meta: - verbose_name_plural = 'regions' - - class Electrical(models.Model): entity = models.OneToOneField(TravelEntity, related_name='electrical_info', on_delete=models.CASCADE) voltage = models.PositiveSmallIntegerField(blank=True, null=True) @@ -564,7 +551,9 @@ class TravelEntityInfo(models.Model): area = models.IntegerField(blank=True, null=True, default=None) languages = models.ManyToManyField(TravelLanguage, blank=True) neighbors = models.ManyToManyField(TravelEntity, blank=True) - region = models.ForeignKey(TravelRegion, blank=True, null=True, on_delete=models.SET_NULL) + region = models.CharField(max_length=10, blank=True) + subregion = models.CharField(max_length=32, blank=True) + intregion = models.CharField(max_length=16, blank=True) class Meta: ordering = ['entity'] @@ -577,6 +566,17 @@ def __str__(self): def related_neighbors(self): return self.neighbors.select_related('type') + @cached_property + def regions(self): + regions = [self.region] + if self.subregion: + regions.append(self.subregion) + + if self.intregion: + regions.append(self.intregion) + + return " / ".join(regions) + @cached_property def get_languages(self): lang = ', '.join([lang.name for lang in self.languages.all()]) diff --git a/src/travel/templates/travel/entities/detail/co.html b/src/travel/templates/travel/entities/detail/co.html index df90071..81e587f 100644 --- a/src/travel/templates/travel/entities/detail/co.html +++ b/src/travel/templates/travel/entities/detail/co.html @@ -9,6 +9,8 @@ {% endblock travel_images %} {% block travel_extra_details %} {% having entity.get_entityinfo as info %} +
Regions
+
{{ info.regions }}
Area
{% if info.area %}{{ info.area|intcomma }} km2{% else %}Unknown{% endif %}
Population