Skip to content

Commit

Permalink
Ingest country data and update station model (#15)
Browse files Browse the repository at this point in the history
* Add country ingestor

* Add code to the station

* Fix flake
  • Loading branch information
meomancer authored Jun 27, 2024
1 parent 1dea7d0 commit 84049c2
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 20 deletions.
2 changes: 1 addition & 1 deletion deployment/.template.env
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ REDIS_PASSWORD=redis_password

RABBITMQ_HOST=rabbitmq
SENTRY_DSN=
SECRET_KEY=SECRET_KEY
INITIAL_FIXTURES=
4 changes: 2 additions & 2 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ x-common-django:

- RABBITMQ_HOST=${RABBITMQ_HOST:-rabbitmq}
- SENTRY_DSN=${SENTRY_DSN:-}
- SECRET_KEY=SECRET_KEY
- INITIAL_FIXTURES=${INITIAL_FIXTURES:-}
volumes:
- static-data:/home/web/static
- media-data:/home/web/media
Expand Down Expand Up @@ -98,7 +98,7 @@ services:

dev:
<<: *default-common-django
entrypoint: []
entrypoint: [ ]
volumes:
- static-data:/home/web/static
- media-data:/home/web/media
Expand Down
5 changes: 2 additions & 3 deletions django_project/core/settings/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
'webpack_loader',
'guardian',
'django_cleanup.apps.CleanupConfig',
'django_celery_beat',
'django_celery_results',
'drf_yasg',
'drf_yasg'
)

WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'frontend/', # must end with slash
Expand Down
6 changes: 3 additions & 3 deletions django_project/gap/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class StationAdmin(admin.ModelAdmin):
"""Station admin."""

list_display = (
'name', 'country', 'provider'
'code', 'name', 'country', 'provider'
)
list_filter = ('country',)
search_fields = ('name',)
list_filter = ('provider', 'country')
search_fields = ('code', 'name')
2 changes: 1 addition & 1 deletion django_project/gap/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class GAPConfig(AppConfig):
"""App Config for GroundObservations."""

name = 'gap'
verbose_name = _('Ground Observations')
verbose_name = _('Global Access Platform')
1 change: 1 addition & 0 deletions django_project/gap/fixtures/1.country.json

Large diffs are not rendered by default.

Empty file.
6 changes: 6 additions & 0 deletions django_project/gap/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# coding=utf-8
"""
Tomorrow Now GAP.
.. note:: Commands
"""
19 changes: 19 additions & 0 deletions django_project/gap/management/commands/load_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# coding=utf-8
"""
Tomorrow Now GAP.
.. note:: Load fixtures
"""

from django.core.management import call_command
from django.core.management.base import BaseCommand


class Command(BaseCommand):
"""Command to load fixtures."""

help = 'Generate country geometry'

def handle(self, *args, **options):
"""Handle load fixtures."""
call_command('loaddata', 'gap/fixtures/1.country.json')
4 changes: 3 additions & 1 deletion django_project/gap/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.7 on 2024-06-26 03:55
# Generated by Django 4.2.7 on 2024-06-27 04:30

import django.contrib.gis.db.models.fields
from django.db import migrations, models
Expand Down Expand Up @@ -35,6 +35,7 @@ class Migration(migrations.Migration):
],
options={
'verbose_name_plural': 'countries',
'ordering': ['name'],
},
),
migrations.CreateModel(
Expand Down Expand Up @@ -65,6 +66,7 @@ class Migration(migrations.Migration):
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=512)),
('description', models.TextField(blank=True, null=True)),
('code', models.CharField(max_length=512)),
('geometry', django.contrib.gis.db.models.fields.PointField(srid=4326)),
('country', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gap.country')),
('observation_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='gap.observationtype')),
Expand Down
15 changes: 14 additions & 1 deletion django_project/gap/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point

from core.models.common import Definition

Expand Down Expand Up @@ -49,7 +50,15 @@ class Country(Definition):
)

class Meta: # noqa
verbose_name_plural = "countries"
verbose_name_plural = 'countries'
ordering = ['name']

@staticmethod
def get_countries_by_point(point: Point):
"""Get country by point."""
return Country.objects.filter(
geometry__contains=point
)


class Station(Definition):
Expand All @@ -59,6 +68,7 @@ class Station(Definition):
Attributes:
name (str): Name of the station.
code (str): Code of the station.
country (ForeignKey):
Foreign key referencing the Country model based on country_ISO_A3.
geometry (Point):
Expand All @@ -67,6 +77,9 @@ class Station(Definition):
Foreign key referencing the Provider model based on provider_id.
"""

code = models.CharField(
max_length=512
)
country = models.ForeignKey(
Country, on_delete=models.CASCADE
)
Expand Down
11 changes: 3 additions & 8 deletions django_project/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,12 @@
#########################################################
# 4. Loading fixtures
#########################################################

print("-----------------------------------------------------")
print("4. Loading fixtures")

# Disable fixtures loading in prod by including environment variable:
# INITIAL_FIXTURES=False

_load_initial_fixtures = ast.literal_eval(
os.getenv('INITIAL_FIXTURES', 'False')
)
if _load_initial_fixtures:
print("-----------------------------------------------------")
print("4. Loading fixtures")
call_command('load_fixtures')

#########################################################
Expand All @@ -89,4 +84,4 @@

print("-----------------------------------------------------")
print("5. Collecting static files")
call_command('collectstatic', '--noinput', verbosity=0)
call_command('collectstatic', '--noinput', verbosity=0)

0 comments on commit 84049c2

Please sign in to comment.