Skip to content

Commit

Permalink
Updated UN regions
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrauth committed Jan 19, 2024
1 parent 5748aba commit 657b66e
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 31 deletions.
12 changes: 1 addition & 11 deletions src/travel/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',]

Expand Down
7 changes: 2 additions & 5 deletions src/travel/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()])
Expand Down
155 changes: 155 additions & 0 deletions src/travel/migrations/0009_alter_travelalias_options_and_more.py
Original file line number Diff line number Diff line change
@@ -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",
),
]
Original file line number Diff line number Diff line change
@@ -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),
),
]
30 changes: 15 additions & 15 deletions src/travel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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']
Expand All @@ -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()])
Expand Down
2 changes: 2 additions & 0 deletions src/travel/templates/travel/entities/detail/co.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
{% endblock travel_images %}
{% block travel_extra_details %}
{% having entity.get_entityinfo as info %}
<dt>Regions</dt>
<dd>{{ info.regions }}</dd>
<dt>Area</dt>
<dd>{% if info.area %}{{ info.area|intcomma }} km<sup>2</sup>{% else %}<em>Unknown</em>{% endif %}</dd>
<dt>Population</dt>
Expand Down

0 comments on commit 657b66e

Please sign in to comment.