From 9a51d39861ca07cc55bb5f0b0a93dac5cfc52053 Mon Sep 17 00:00:00 2001 From: Chris Karr Date: Sat, 1 Apr 2017 15:26:35 -0500 Subject: [PATCH] Updating management command with support for older database. --- management/commands/process_bundles.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/management/commands/process_bundles.py b/management/commands/process_bundles.py index 79023b1..6b591c8 100644 --- a/management/commands/process_bundles.py +++ b/management/commands/process_bundles.py @@ -1,11 +1,12 @@ import datetime +import json from django.contrib.gis.geos import * from django.core.management.base import BaseCommand, CommandError from django.utils import timezone -from passive_data_kit.decorators import handle_lock -from passive_data_kit.models import DataPoint, DataBundle +from ..decorators import handle_lock +from ..models import DataPoint, DataBundle, install_supports_jsonfield class Command(BaseCommand): help = 'Convert unprocessed DataBundle instances into DataPoint instances.' @@ -28,6 +29,9 @@ def handle(self, *args, **options): to_delete = [] for bundle in DataBundle.objects.filter(processed=False)[:options['bundle_count']]: + if install_supports_jsonfield() is False: + bundle.properties = json.loads(bundle.properties) + for bundle_point in bundle.properties: if 'passive-data-metadata' in bundle_point: point = DataPoint(recorded=timezone.now()) @@ -41,7 +45,12 @@ def handle(self, *args, **options): point.generated_at = GEOSGeometry('POINT(' + str(bundle_point['passive-data-metadata']['longitude']) + ' ' + str(bundle_point['passive-data-metadata']['latitude']) + ')') point.created = datetime.datetime.fromtimestamp(bundle_point['passive-data-metadata']['timestamp'], tz=timezone.get_default_timezone()) - point.properties = bundle_point + + if install_supports_jsonfield(): + point.properties = bundle_point + else: + point.properties = json.dumps(bundle_point, indent=2) + point.save() bundle.processed = True