Skip to content

Commit

Permalink
Updating management command with support for older database.
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Apr 1, 2017
1 parent fba126d commit 9a51d39
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions 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.'
Expand All @@ -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())
Expand All @@ -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
Expand Down

0 comments on commit 9a51d39

Please sign in to comment.