-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from groovecoder/refactor-load-scripts-28
fixes #28 - consolidate case-loading commands
- Loading branch information
Showing
8 changed files
with
235 additions
and
190 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,96 @@ | ||
from datetime import datetime | ||
from json import loads | ||
from sys import stderr | ||
from time import mktime | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
from django.contrib.gis.gdal import DataSource | ||
from django.contrib.gis.geos import GEOSGeometry | ||
from django.db import IntegrityError | ||
from parsedatetime import Calendar | ||
|
||
from bpz.models import Case | ||
|
||
''' | ||
Import BPZ case data | ||
''' | ||
|
||
|
||
def parse_datetime(value): | ||
calendar = Calendar() | ||
raw_date = loads('"%s"' % value) | ||
timestamp = mktime(calendar.parseDateText(raw_date)) | ||
return datetime.fromtimestamp(timestamp).date() | ||
|
||
|
||
def parse_feature_raw_value(feature, field_name): | ||
raw_value = feature[field_name].value | ||
value = raw_value | ||
if '\r\n' in raw_value: | ||
value = raw_value.split('\r\n', 1)[0] | ||
stderr.write('Invalid %s: "%s". Using first line: "%s"\n' % | ||
(field_name, repr(raw_value), value)) | ||
return loads('"%s"' % value) | ||
|
||
|
||
# http://stackoverflow.com/a/19357056/571420 | ||
def update_or_create(command, model, filter_kwargs, update_kwargs): | ||
if not model.objects.filter(**filter_kwargs).update(**update_kwargs): | ||
kwargs = filter_kwargs.copy() | ||
kwargs.update(update_kwargs) | ||
try: | ||
model.objects.create(**kwargs) | ||
print('New case "%s"' % kwargs['case_id']) | ||
command.new += 1 | ||
except IntegrityError: | ||
if not (model.objects.filter(**filter_kwargs) | ||
.update(**update_kwargs)): | ||
raise | ||
else: | ||
print('Updated case "%s"' % update_kwargs['case_id']) | ||
command.updated += 1 | ||
|
||
|
||
class Command(BaseCommand): | ||
args = '<cases.json>' | ||
help = 'Imports case GeoJSON' | ||
new, updated = 0, 0 | ||
|
||
def handle(self, *args, **options): | ||
if len(args) != 1: | ||
raise CommandError('Pass one json file to import') | ||
|
||
# Load data | ||
datasource = DataSource(args[0], encoding='iso-8859-1') | ||
layer = datasource[0] | ||
for feature in layer: | ||
# Get the common attributes' values | ||
attr = { | ||
'object_id': feature['OBJECTID'].value, | ||
'case_id': loads('"%s"' % feature['Case_'].value), | ||
'status': feature['Status'].value, | ||
'link': loads('"%s"' % feature['Link'].value), | ||
} | ||
attr['location'] = parse_feature_raw_value(feature, 'Location') | ||
attr['hearing_date'] = parse_datetime(feature['Date_'].value) | ||
attr['geom'] = GEOSGeometry(feature.geom.wkt) | ||
|
||
# Get the custom attribute values: case_type & domain | ||
domain = Case.DOMAIN_TMAPC | ||
case_type = None | ||
if 'BOA' in attr['case_id']: | ||
domain = Case.DOMAIN_BOA | ||
case_type = Case.NAME_BOA | ||
else: | ||
case_type = parse_feature_raw_value(feature, 'Type') | ||
attr['case_type'] = case_type | ||
|
||
filter_kwargs = {'object_id': attr['object_id'], | ||
'domain': domain} | ||
update_or_create(self, Case, filter_kwargs, attr) | ||
|
||
self.stdout.write( | ||
'Imported %d %s cases (%d new, %d updated)' % | ||
(self.new + self.updated, Case.DOMAIN_CHOICES[domain], | ||
self.new, self.updated) | ||
) |
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.