-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/local-unit-health' into fix/staging-issue
- Loading branch information
Showing
4 changed files
with
104 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
local_units/management/commands/import_local_units_health.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters