Skip to content

Commit

Permalink
Extract secondary identifier extraction to reflective mechanism.
Browse files Browse the repository at this point in the history
* Start process_bundles job after export.
  • Loading branch information
audaciouscode committed Apr 29, 2017
1 parent 438ba40 commit 8ce84e1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
Empty file added generators/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions generators/pdk_app_event.py
@@ -0,0 +1,6 @@

def extract_secondary_identifier(properties):
if 'event_name' in properties:
return properties['event_name']

return None
6 changes: 6 additions & 0 deletions generators/pdk_withings_device.py
@@ -0,0 +1,6 @@

def extract_secondary_identifier(properties):
if 'datastream' in properties:
return properties['datastream']

return None
21 changes: 16 additions & 5 deletions models.py
Expand Up @@ -12,6 +12,7 @@
from django.db import connection
from django.db.models.signals import post_delete
from django.dispatch.dispatcher import receiver
from django.utils.text import slugify

def generator_label(identifier):
for app in settings.INSTALLED_APPS:
Expand All @@ -29,6 +30,8 @@ def generator_label(identifier):

return identifier

def generator_slugify(str_obj):
return slugify(str_obj.replace('.', ' ')).replace('-', '_')

def install_supports_jsonfield():
return connection.pg_version >= 90400
Expand All @@ -54,13 +57,21 @@ 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()
for app in settings.INSTALLED_APPS:
generator_name = generator_slugify(self.generator_identifier)

self.secondary_identifier = props['event_name']
self.save()
try:
generator = importlib.import_module(app + '.generators.' + generator_name)

return self.secondary_identifier
identifier = generator.extract_secondary_identifier(self.fetch_properties())

if identifier is not None:
self.secondary_identifier = identifier
self.save()

return self.secondary_identifier
except ImportError:
pass

return None

Expand Down
5 changes: 5 additions & 0 deletions views.py
Expand Up @@ -6,6 +6,7 @@
import os

from django.conf import settings
from django.core.management import call_command
from django.http import HttpResponse, HttpResponseNotAllowed, JsonResponse, HttpResponseNotFound, \
FileResponse
from django.shortcuts import render_to_response
Expand Down Expand Up @@ -106,6 +107,8 @@ def add_data_bundle(request):

bundle.save()

call_command('process_bundles')

return response
elif request.method == 'POST':
response = HttpResponse(json.dumps(response, indent=2), \
Expand Down Expand Up @@ -141,6 +144,8 @@ def add_data_bundle(request):
data_file.content_file.save(value.name, value)
data_file.save()

call_command('process_bundles')

return response
elif request.method == 'OPTIONS':
response = HttpResponse('', content_type='text/plain', status=200)
Expand Down

0 comments on commit 8ce84e1

Please sign in to comment.