diff --git a/admin.py b/admin.py index 7deda7d..2f219b4 100644 --- a/admin.py +++ b/admin.py @@ -12,8 +12,8 @@ class DataPointVisualizationsAdmin(admin.OSMGeoAdmin): class DataPointAdmin(admin.OSMGeoAdmin): openlayers_url = 'https://openlayers.org/api/2.13.1/OpenLayers.js' - list_display = ('source', 'generator_identifier', 'created', 'recorded',) - list_filter = ('created', 'recorded', 'generator_identifier',) + list_display = ('source', 'generator_identifier', 'secondary_identifier', 'created', 'recorded',) + list_filter = ('created', 'recorded', 'generator_identifier', 'secondary_identifier',) @admin.register(DataBundle) class DataBundleAdmin(admin.OSMGeoAdmin): diff --git a/management/commands/process_bundles.py b/management/commands/process_bundles.py index 6b591c8..4baf557 100644 --- a/management/commands/process_bundles.py +++ b/management/commands/process_bundles.py @@ -5,8 +5,8 @@ from django.core.management.base import BaseCommand, CommandError from django.utils import timezone -from ..decorators import handle_lock -from ..models import DataPoint, DataBundle, install_supports_jsonfield +from ...decorators import handle_lock +from ...models import DataPoint, DataBundle, install_supports_jsonfield class Command(BaseCommand): help = 'Convert unprocessed DataBundle instances into DataPoint instances.' @@ -50,6 +50,8 @@ def handle(self, *args, **options): point.properties = bundle_point else: point.properties = json.dumps(bundle_point, indent=2) + + point.fetch_secondary_identifier() point.save() diff --git a/migrations/0016_datapoint_secondary_identifier.py b/migrations/0016_datapoint_secondary_identifier.py new file mode 100644 index 0000000..152ff32 --- /dev/null +++ b/migrations/0016_datapoint_secondary_identifier.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.6 on 2017-04-02 01:00 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('passive_data_kit', '0015_datafile_identifier'), + ] + + operations = [ + migrations.AddField( + model_name='datapoint', + name='secondary_identifier', + field=models.CharField(blank=True, db_index=True, max_length=1024, null=True), + ), + ] diff --git a/models.py b/models.py index 04f0b21..bb20500 100644 --- a/models.py +++ b/models.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import json + import importlib import psycopg2 @@ -35,6 +37,7 @@ class DataPoint(models.Model): source = models.CharField(max_length=1024, db_index=True) generator = models.CharField(max_length=1024, db_index=True) generator_identifier = models.CharField(max_length=1024, db_index=True, default='unknown-generator') + secondary_identifier = models.CharField(max_length=1024, db_index=True, null=True, blank=True) created = models.DateTimeField(db_index=True) generated_at = models.PointField(null=True) @@ -45,7 +48,26 @@ class DataPoint(models.Model): properties = JSONField() else: properties = models.TextField(max_length=(32 * 1024 * 1024 * 1024)) + + def fetch_secondary_identifier(self): + if self.secondary_identifier is not None: + return self.secondary_identifier + else: + if self.generator_identifier == 'pdk-app-event': + props = self.fetch_properties() + + self.secondary_identifier = props['event_name'] + self.save() + + return self.secondary_identifier + + return None + def fetch_properties(self): + if install_supports_jsonfield(): + return self.properties + + return json.loads(self.properties) class DataBundle(models.Model): recorded = models.DateTimeField(db_index=True) diff --git a/views.py b/views.py index cf0cc07..d5311ff 100644 --- a/views.py +++ b/views.py @@ -12,7 +12,7 @@ from django.contrib.admin.views.decorators import staff_member_required -from .models import DataPoint, DataBundle, DataFile, DataSourceGroup, DataSource, ReportJob, generator_label +from .models import DataPoint, DataBundle, DataFile, DataSourceGroup, DataSource, ReportJob, generator_label, install_supports_jsonfield @csrf_exempt def add_data_point(request): @@ -31,7 +31,11 @@ def add_data_point(request): point.source = point['passive-data-metadata']['source'] point.generator = point['passive-data-metadata']['generator'] point.created = datetime.datetime.fromtimestamp(point['passive-data-metadata']['source'], tz=timezone.get_default_timezone()) - point.properties = point + + if install_supports_jsonfield(): + point.properties = point + else: + point.properties = json.dumps(point, indent=2) point.save() @@ -49,7 +53,11 @@ def add_data_point(request): point.source = point['passive-data-metadata']['source'] point.generator = point['passive-data-metadata']['generator'] point.created = datetime.datetime.fromtimestamp(point['passive-data-metadata']['source'], tz=timezone.get_default_timezone()) - point.properties = point + + if install_supports_jsonfield(): + point.properties = point + else: + point.properties = json.dumps(point, indent=2) point.save() @@ -80,7 +88,12 @@ def add_data_bundle(request): points = json.loads(request.body) bundle = DataBundle(recorded=timezone.now()) - bundle.properties = points + + if install_supports_jsonfield(): + bundle.properties = points + else: + bundle.properties = json.dumps(points, indent=2) + bundle.save() return response @@ -95,7 +108,12 @@ def add_data_bundle(request): points = json.loads(request.POST['payload']) bundle = DataBundle(recorded=timezone.now()) - bundle.properties = points + + if install_supports_jsonfield(): + bundle.properties = points + else: + bundle.properties = json.dumps(points, indent=2) + bundle.save() except ValueError: response = { 'message': 'Unable to parse data bundle.' }