def post_reauthenticated( self, params ): app_id = params.get( 'delete' ) if app_id: EnkiModelRestAPIDataStore.delete_user_app_data( self.user_id , app_id ) self.add_infomessage( MSG.SUCCESS(), MSG.APP_DATA_DELETED()) data_list = EnkiModelApp.user_data_list( self.user_id ) self.render_tmpl( 'appdatastores.html', active_menu = 'profile', data = data_list, apps_list = EnkiModelApp.user_apps_list( data_list ), )
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') if user_id and auth_token and app_secret and data_type and data_id: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid data_store_item = EnkiModelRestAPIDataStore.get_by_user_id_app_id_data_type_data_id_not_expired( user_id, token_valid.app_id, data_type, data_id ) if data_store_item: answer.update({ 'data_payload' : data_store_item.data_payload, 'time_expires' : enki.libutil.seconds_from_epoch( data_store_item.time_expires ) , 'read_access' : data_store_item.read_access, 'server_time' : int( time.time())}) success = True error = '' else: error = 'Not found' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def fetch_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_read_access_not_expired( user_id, app_id, data_type, read_access ): entity = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.read_access == read_access, EnkiModelRestAPIDataStore.time_expires > datetime.datetime.now())).fetch() return entity
def get_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id_not_expired( user_id, app_id, data_type, data_id ): entity = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id, EnkiModelRestAPIDataStore.time_expires > datetime.datetime.now())).get() return entity
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') if user_id and auth_token and app_secret and data_type and data_id: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid data_stores = EnkiModelRestAPIDataStore.fetch_by_user_id_app_id_data_type_data_id( user_id, token_valid.app_id, data_type, data_id ) ndb.delete_multi( data_stores ) success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def fetch_EnkiModelRestAPIDataStore_by_user_id(user_id): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.user_id == user_id).order( EnkiModelRestAPIDataStore.app_id, EnkiModelRestAPIDataStore.data_type, EnkiModelRestAPIDataStore.data_id, EnkiModelRestAPIDataStore.time_expires).fetch() return list
def get_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id( user_id, app_id, data_type, data_id): entity = EnkiModelRestAPIDataStore.query( ndb.AND(EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id)).get() return entity
def fetch_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id( user_id, app_id, data_type, data_id): list = EnkiModelRestAPIDataStore.query( ndb.AND(EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id)).fetch( keys_only=True) return list
def fetch_EnkiModelRestAPIDataStore_by_app_id_data_type_read_access_not_expired( app_id, data_type, read_access): list = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.read_access == read_access, EnkiModelRestAPIDataStore.time_expires > datetime.datetime.now())).fetch() return list
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '' )) auth_token = jsonobject.get( 'auth_token', '' ) app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '' ) read_access = jsonobject.get( 'read_access', '' ) if user_id and auth_token and app_secret and data_type and ( read_access == 'public' or read_access == 'private' or read_access == 'friends' ): if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid error = 'Not found' data_store_list = [] if read_access == 'public': # returns all data with read-access "public" data_store_list = EnkiModelRestAPIDataStore.fetch_by_app_id_data_type_read_access_not_expired( token_valid.app_id, data_type, read_access ) else: people_list = [] if read_access == 'private': # returns all user's data with read-access "private" people_list = [ user_id ] elif read_access == 'friends': # returns list of user's friends' data with friends' read_access "friends" people_list = EnkiModelFriends.get_friends_user_id( user_id ) # get the user's friends' ids if people_list: for person_id in people_list: # get each persons' data data_store_list = EnkiModelRestAPIDataStore.fetch_by_user_id_app_id_data_type_read_access_not_expired( person_id, token_valid.app_id, data_type, read_access ) if data_store_list: data_payloads = [] for data_store_item in data_store_list: data_payloads.append({ 'user_id' : str( data_store_item.user_id ), 'data_id' : data_store_item.data_id, 'data_payload' : data_store_item.data_payload, 'time_expires' : enki.libutil.seconds_from_epoch( data_store_item.time_expires )}) if data_payloads: answer.update({ 'data_payloads' : data_payloads, 'server_time' : int( time.time())}) success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def get_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id_not_expired( user_id, app_id, data_type, data_id): entity = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id, EnkiModelRestAPIDataStore.time_expires > datetime.datetime.now())).get() return entity
def cleanup_item(self): number = random.randint(1, 1000) likelihood = 10 # occurs with a probability of 1% if number < likelihood: ndb.delete_multi_async(self.fetch_keys_old_sessions(3)) ndb.delete_multi_async(EnkiModelBackoffTimer.fetch_keys_old(3)) ndb.delete_multi_async(EnkiModelTokenAuth.fetch_keys_expired()) ndb.delete_multi_async( EnkiModelRestAPIConnectToken.fetch_expired()) ndb.delete_multi_async(EnkiModelRestAPIDataStore.fetch_expired()) ndb.delete_multi_async( EnkiModelTokenVerify.fetch_keys_old_tokens_by_types( 0.007, [ 'loginaddconfirm_1', 'loginaddconfirm_2', 'loginaddconfirm_3' ])) EnkiModelRestAPIDataStore.refresh_non_expiring() ndb.delete_multi_async( EnkiModelTokenVerify.fetch_keys_old_tokens_by_types( 1, ['emailsubscriptionconfirm'])) ndb.delete_multi_async(EnkiModelBackoffTimer.fetch_keys_old(1))
def user_data_list(cls, user_id): list = [] data_stores = EnkiModelRestAPIDataStore.fetch_by_user_id(user_id) for data_store in data_stores: app_name = '' app = cls.get_by_id(int(data_store.app_id)) if app: app_name = app.name list.append([ data_store.app_id, app_name, data_store.data_type, data_store.data_id, data_store.data_payload, data_store.read_access, data_store.time_expires ]) return list
def post( self ): jsonobject = json.loads( self.request.body ) success = False error = 'Invalid request' answer = {} if jsonobject: user_id = int( jsonobject.get( 'user_id', '')) auth_token = jsonobject.get( 'auth_token', '') app_secret = jsonobject.get( 'app_secret', '') data_type = jsonobject.get( 'data_type', '') data_id = jsonobject.get( 'data_id', '') data_payload = jsonobject.get( 'data_payload' ) # set the expiry time time_to_expiry = int(jsonobject.get( 'time_expires', EnkiModelRestAPIDataStore.DATASTORE_EXPIRY_DEFAULT )) # default lifetime if unspecified if time_to_expiry == 0: # non-expiring lifetime time_to_expiry = EnkiModelRestAPIDataStore.DATASTORE_NON_EXPIRING time_expires = datetime.datetime.now() + datetime.timedelta( seconds = time_to_expiry ) read_access = jsonobject.get( 'read_access', '' ) if user_id and auth_token and app_secret and data_type and data_id and data_payload and time_expires: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): token_valid = EnkiModelRestAPITokenVerify.get_by_user_id_token( user_id, auth_token ) if token_valid: # user is valid # add optional calculated properties to the data payload if 'calc_ip_addr' in data_payload: # IP address of the request remote_address = self.request.remote_addr data_payload.update({ 'calc_ip_addr' : remote_address }) data_store = EnkiModelRestAPIDataStore.get_by_user_id_app_id_data_type_data_id( user_id, token_valid.app_id, data_type, data_id ) if data_store: # update data_store.data_payload = data_payload data_store.time_expires = time_expires # update the expiry time if read_access: # if read_access is not specified, don't update it data_store.read_access = read_access else: # create new if not read_access: # if read_access is not specified, use the default value defined in the model data_store = EnkiModelRestAPIDataStore( user_id = user_id, app_id = token_valid.app_id, data_type = data_type, data_id = data_id, data_payload = data_payload, time_expires = time_expires ) else: data_store = EnkiModelRestAPIDataStore( user_id = user_id, app_id = token_valid.app_id, data_type = data_type, data_id = data_id, data_payload = data_payload, time_expires = time_expires, read_access = read_access ) data_store.put() success = True error = '' else: error = 'Unauthorised user' else: error = 'Unauthorised app' answer.update({ 'success' : success, 'error' : error }) self.response.headers[ 'Content-Type' ] = 'application/json' self.response.write( json.dumps( answer, separators=(',',':') ))
def fetch_EnkiModelRestAPIDataStore_non_expiring(): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.time_expires > (datetime.datetime.now() + datetime.timedelta(seconds=DATASTORE_NON_EXPIRING_REFRESH))).fetch() return list
def fetch_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id( user_id, app_id, data_type, data_id ): list = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id )).fetch( keys_only = True ) return list
def get_EnkiModelRestAPIDataStore_by_user_id_app_id_data_type_data_id( user_id, app_id, data_type, data_id ): entity = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id, EnkiModelRestAPIDataStore.data_type == data_type, EnkiModelRestAPIDataStore.data_id == data_id )).get() return entity
def fetch_EnkiModelRestAPIDataStore_expired(): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.time_expires < datetime.datetime.now() ).fetch(keys_only=True) return list
def fetch_EnkiModelRestAPIDataStore_by_user_id( user_id ): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.user_id == user_id ).order( EnkiModelRestAPIDataStore.app_id, EnkiModelRestAPIDataStore.data_type, EnkiModelRestAPIDataStore.data_id, EnkiModelRestAPIDataStore.time_expires ).fetch() return list
def fetch_EnkiModelRestAPIDataStore_by_user_id_app_id(user_id, app_id): keys = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id)).fetch(keys_only=True) return keys
def fetch_EnkiModelRestAPIDataStore_by_user_id_app_id( user_id, app_id ): keys = EnkiModelRestAPIDataStore.query( ndb.AND( EnkiModelRestAPIDataStore.user_id == user_id, EnkiModelRestAPIDataStore.app_id == app_id )).fetch( keys_only = True ) return keys
def fetch_EnkiModelRestAPIDataStore_expired(): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.time_expires < datetime.datetime.now()).fetch( keys_only = True ) return list
def fetch_EnkiModelRestAPIDataStore_non_expiring(): list = EnkiModelRestAPIDataStore.query( EnkiModelRestAPIDataStore.time_expires > ( datetime.datetime.now() + datetime.timedelta( seconds = DATASTORE_NON_EXPIRING_REFRESH ))).fetch() return list