Skip to content

Commit

Permalink
Merge branch 'feature/local-unit-health' into fix/staging-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
k9845 committed Apr 1, 2024
2 parents 61df031 + 5fb9ea6 commit 0a5ba24
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
17 changes: 11 additions & 6 deletions local_units/management/commands/import-local-units-csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import csv
import pytz
from dateutil import parser as date_parser
from django.core.management.base import BaseCommand, CommandError
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Point


Expand Down Expand Up @@ -31,11 +31,16 @@ def handle(self, *args, **options):
unit = LocalUnit()
unit.country = Country.objects.get(iso3=row['ISO3'])
# We do not check COUNTRY or NATIONAL_SOCIETY, but only this ^
if row['TYPECODE'] == 'NS0': row['TYPECODE'] = 1
elif row['TYPECODE'] == 'NS1': row['TYPECODE'] = 2
elif row['TYPECODE'] == 'NS2': row['TYPECODE'] = 3
elif row['TYPECODE'] == 'NS3': row['TYPECODE'] = 4
else: row['TYPECODE'] = int(row['TYPECODE'])
if row['TYPECODE'] == 'NS0':
row['TYPECODE'] = 1
elif row['TYPECODE'] == 'NS1':
row['TYPECODE'] = 2
elif row['TYPECODE'] == 'NS2':
row['TYPECODE'] = 3
elif row['TYPECODE'] == 'NS3':
row['TYPECODE'] = 4
else:
row['TYPECODE'] = int(row['TYPECODE'])
unit.type = LocalUnitType.objects.get(
code=row['TYPECODE'],
)
Expand Down
47 changes: 47 additions & 0 deletions local_units/management/commands/import_local_units_health.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from django.db import transaction
import csv
import pytz
from dateutil import parser as date_parser
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Point


from api.models import Country
from ...models import LocalUnit, LocalUnitType


class Command(BaseCommand):
help = "Import LocalUnits data from CSV"
missing_args_message = "Filename is missing. Filename / path to CSV file required. Required headers in CSV: distr_code, GOadm2code, ADM2"

def add_arguments(self, parser):
parser.add_argument('filename', nargs='+', type=str)

@transaction.atomic
def handle(self, *args, **options):
filename = options['filename'][0]
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
for i, row in enumerate(reader):
# Without positions we can't use the row:
if not row['LONGITUDE'] or not row['LATITUDE']:
continue
unit = LocalUnit()
unit.country = Country.objects.get(name=row['COUNTRY'])
unit.type = LocalUnitType.objects.get(
code=row['TYPE CODE'],
)
unit.is_public = row['VISIBILITY'].lower() == 'public'
unit.validated = True
unit.local_branch_name = row['NAME_LOC']
unit.address_loc = row['ADDRESS_LOC']
unit.focal_person_loc = row['FOCAL_PERSON_LOC']
unit.location = Point(float(row['LONGITUDE']), float(row['LATITUDE']))
if row['DATE OF UPDATE']:
unit.date_of_data = date_parser.parse(row['DATE OF UPDATE']).replace(tzinfo=pytz.utc)
unit.save()
name = unit.local_branch_name if unit.local_branch_name else unit.english_branch_name
if name:
print(f'{i} | {name} saved')
else:
print(f'{i} | *** entity with ID saved')
28 changes: 28 additions & 0 deletions local_units/migrations/0006_auto_20240329_0849.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.25 on 2024-03-29 08:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('local_units', '0005_delegationoffice_delegationofficetype'),
]

operations = [
migrations.AddField(
model_name='localunit',
name='data_source_id',
field=models.IntegerField(blank=True, null=True, verbose_name='Data Source Id'),
),
migrations.AlterField(
model_name='localunit',
name='city_en',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='City in English'),
),
migrations.AlterField(
model_name='localunit',
name='city_loc',
field=models.CharField(blank=True, max_length=255, null=True, verbose_name='City in local language'),
),
]
20 changes: 18 additions & 2 deletions local_units/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ class LocalUnit(models.Model):
null=True,
verbose_name=_('Address in English')
)
city_loc = models.CharField(max_length=255, verbose_name=_('City in local language'))
city_en = models.CharField(max_length=255, verbose_name=_('City in English'))
city_loc = models.CharField(
max_length=255,
verbose_name=_('City in local language'),
null=True,
blank=True,
)
city_en = models.CharField(
max_length=255,
verbose_name=_('City in English'),
null=True,
blank=True,
)
focal_person_loc = models.CharField(
max_length=255,
blank=True,
Expand Down Expand Up @@ -141,6 +151,12 @@ class LocalUnit(models.Model):
verbose_name=_('Social link')
)
location = models.PointField()
# added to track health local unit type
data_source_id = models.IntegerField(
verbose_name=_('Data Source Id'),
null=True,
blank=True
)

def __str__(self):
branch_name = self.local_branch_name or self.english_branch_name
Expand Down

0 comments on commit 0a5ba24

Please sign in to comment.