<s> import os <EOL> import json <EOL> import random <EOL> from django . http import HttpResponse , HttpResponseBadRequest <EOL> from openpds import settings <EOL> from openpds . authorization import PDSAuthorization <EOL> from openpds . connectors . opensense import getfunfdata , getmotiondata <EOL> from openpds . core . models import Profile <EOL> from openpds import getInternalDataStore <EOL> def data ( request ) : <EOL> print "<STR_LIT>" <EOL> '''<STR_LIT>''' <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return HttpResponseBadRequest ( "<STR_LIT>" , content_type = '<STR_LIT>' ) <EOL> device_id = request . POST [ '<STR_LIT>' ] <EOL> token = request . POST [ '<STR_LIT>' ] <EOL> data = json . loads ( request . POST [ '<STR_LIT:data>' ] ) <EOL> result = { } <EOL> funfresult = { } <EOL> authorization = PDSAuthorization ( "<STR_LIT>" , audit_enabled = True ) <EOL> if ( not authorization . is_authorized ( request ) ) : <EOL> print "<STR_LIT>" <EOL> return HttpResponse ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> datastore_owner , ds_owner_created = Profile . objects . get_or_create ( uuid = device_id ) <EOL> internalDataStore = getInternalDataStore ( datastore_owner , "<STR_LIT>" , "<STR_LIT>" , token ) <EOL> result = extractAndInsertData ( data , internalDataStore , token ) <EOL> funfresult = insertfunf ( data , internalDataStore , token ) <EOL> blankdata = getfunfdata . insertblankdata ( ) <EOL> try : <EOL> internalDataStore . saveData ( blankdata , '<STR_LIT>' ) <EOL> blankdata = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> except Exception as e : <EOL> print "<STR_LIT>" % e <EOL> blankdata = { '<STR_LIT:success>' : False , '<STR_LIT>' : e . message } <EOL> print result <EOL> if result . get ( '<STR_LIT:status>' ) : <EOL> return HttpResponse ( json . dumps ( result ) , content_type = '<STR_LIT:application/json>' ) <EOL> else : <EOL> return HttpResponseBadRequest ( json . dumps ( result ) , content_type = '<STR_LIT:application/json>' ) <EOL> def extractAndInsertData ( data , internalDataStore , token ) : <EOL> result = { } <EOL> print '<STR_LIT>' <EOL> print data <EOL> print '<STR_LIT>' <EOL> try : <EOL> for json_object in data : <EOL> internalDataStore . saveData ( json_object , '<STR_LIT>' ) <EOL> result = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : len ( data ) } <EOL> except Exception as e : <EOL> print "<STR_LIT>" % e <EOL> result = { '<STR_LIT:success>' : False , '<STR_LIT>' : e . message } <EOL> return result <EOL> def insertfunf ( data , internalDataStore , token ) : <EOL> funfresult = { } <EOL> motiondata = [ ] <EOL> print '<STR_LIT>' <EOL> print data <EOL> print '<STR_LIT>' <EOL> for object in data : <EOL> if object [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> motiondata . append ( object ) <EOL> activitydata = getmotiondata . ondatareceived ( motiondata ) <EOL> activityresult = { } <EOL> for object in activitydata : <EOL> try : <EOL> internalDataStore . saveData ( object , '<STR_LIT>' ) <EOL> activityresult = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> except Exception as e : <EOL> print "<STR_LIT>" % e <EOL> funfresult = { '<STR_LIT:success>' : False , '<STR_LIT>' : e . message } <EOL> for object in data : <EOL> funfdata = getfunfdata . getfunfdata ( object ) <EOL> try : <EOL> internalDataStore . saveData ( funfdata , '<STR_LIT>' ) <EOL> funfresult = { '<STR_LIT:status>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> except Exception as e : <EOL> print "<STR_LIT>" % e <EOL> funfresult = { '<STR_LIT:success>' : False , '<STR_LIT>' : e . message } <EOL> return funfresult <EOL> def register ( request ) : <EOL> '''<STR_LIT>''' <EOL> print "<STR_LIT>" <EOL> data = request . POST <EOL> if not data . get ( '<STR_LIT>' ) : <EOL> responseData = { '<STR_LIT:error>' : '<STR_LIT>' } <EOL> return HttpResponseBadRequest ( json . dumps ( data ) , content_type = '<STR_LIT:application/json>' ) <EOL> else : <EOL> key = generateKey ( ) <EOL> responseData = { '<STR_LIT:key>' : key } <EOL> return HttpResponse ( json . dumps ( responseData ) , content_type = '<STR_LIT:application/json>' ) <EOL> def config ( request ) : <EOL> '''<STR_LIT>''' <EOL> response = open ( os . path . join ( settings . STATIC_ROOT , '<STR_LIT>' ) ) . read ( ) <EOL> return HttpResponse ( json . dumps ( response ) , content_type = '<STR_LIT:application/json>' ) <EOL> def generateKey ( ) : <EOL> validCharacters = '<STR_LIT>' <EOL> sr = random . SystemRandom ( ) <EOL> key = '<STR_LIT>' <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> index = sr . randint ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> key += validCharacters [ index ] <EOL> return key <EOL> def test ( request ) : <EOL> print "<STR_LIT>" <EOL> return "<STR_LIT>" </s>
