Skip to content

Commit

Permalink
Code cleanup and extracting generator ID
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Feb 9, 2017
1 parent f9f3801 commit ef9bc9b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
16 changes: 9 additions & 7 deletions admin.py
@@ -1,33 +1,35 @@
from django.contrib import admin
from django.contrib.gis import admin

from .models import DataPoint, DataBundle, DataSource, DataSourceGroup, \
DataPointVisualizations, ReportJob

@admin.register(DataPointVisualizations)
class DataPointVisualizationsAdmin(admin.ModelAdmin):
class DataPointVisualizationsAdmin(admin.OSMGeoAdmin):
list_display = ('source', 'generator_identifier', 'last_updated',)
list_filter = ('source', 'generator_identifier', 'last_updated',)

@admin.register(DataPoint)
class DataPointAdmin(admin.ModelAdmin):
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',)

@admin.register(DataBundle)
class DataBundleAdmin(admin.ModelAdmin):
class DataBundleAdmin(admin.OSMGeoAdmin):
list_display = ('recorded', 'processed',)
list_filter = ('processed', 'recorded',)

@admin.register(DataSourceGroup)
class DataBundleAdmin(admin.ModelAdmin):
class DataSourceGroupAdmin(admin.OSMGeoAdmin):
list_display = ('name',)

@admin.register(DataSource)
class DataBundleAdmin(admin.ModelAdmin):
class DataSourceAdmin(admin.OSMGeoAdmin):
list_display = ('name', 'identifier', 'group')
list_filter = ('group',)

@admin.register(ReportJob)
class ReportJobAdmin(admin.ModelAdmin):
class ReportJobAdmin(admin.OSMGeoAdmin):
list_display = ('requester', 'requested', 'started', 'completed')
list_filter = ('requested', 'started', 'completed',)
21 changes: 13 additions & 8 deletions management/commands/process_bundles.py
@@ -1,5 +1,6 @@
import datetime

from django.contrib.gis.geos import *
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

Expand Down Expand Up @@ -28,16 +29,20 @@ def handle(self, *args, **options):

for bundle in DataBundle.objects.filter(processed=False)[:options['bundle_count']]:
for bundle_point in bundle.properties:
point = DataPoint(recorded=timezone.now())
point.source = bundle_point['passive-data-metadata']['source']
point.generator = bundle_point['passive-data-metadata']['generator']
if 'passive-data-metadata' in bundle_point:
point = DataPoint(recorded=timezone.now())
point.source = bundle_point['passive-data-metadata']['source']
point.generator = bundle_point['passive-data-metadata']['generator']

if 'generator-id' in bundle_point['passive-data-metadata']:
point.generator_identifier = bundle_point['passive-data-metadata']['generator-id']
if 'generator-id' in bundle_point['passive-data-metadata']:
point.generator_identifier = bundle_point['passive-data-metadata']['generator-id']

if 'latitude' in bundle_point['passive-data-metadata'] and 'longitude' in bundle_point['passive-data-metadata']:
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
point.save()
point.created = datetime.datetime.fromtimestamp(bundle_point['passive-data-metadata']['timestamp'], tz=timezone.get_default_timezone())
point.properties = bundle_point
point.save()

bundle.processed = True
bundle.save()
Expand Down
14 changes: 9 additions & 5 deletions views.py
Expand Up @@ -90,12 +90,16 @@ def add_data_bundle(request):
response['Access-Control-Allow-Methods'] = 'CREATE, POST'
response['Access-Control-Request-Headers'] = 'Content-Type'
response['Access-Control-Allow-Headers'] = 'Content-Type'

points = json.loads(request.POST['payload'])

try:
points = json.loads(request.POST['payload'])

bundle = DataBundle(recorded=timezone.now())
bundle.properties = points
bundle.save()
bundle = DataBundle(recorded=timezone.now())
bundle.properties = points
bundle.save()
except ValueError:
response = { 'message': 'Unable to parse data bundle.' }
response = HttpResponse(json.dumps(response, indent=2), content_type='application/json', status=201)

return response
elif request.method == 'OPTIONS':
Expand Down

0 comments on commit ef9bc9b

Please sign in to comment.