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', '') products = jsonobject.get( 'products', '') if user_id and auth_token and app_secret: if EnkiModelApp.check_secret( user_id, auth_token, app_secret ): if EnkiModelRestAPITokenVerify.exist_by_user_id_token( user_id, auth_token ): if products: # check which products in the list are activated by the user and return them list_entities = EnkiModelProductKey.fetch_by_activator_products_list( user_id, products ) else: # no product specified, return all products activated by the user list_entities = EnkiModelProductKey.fetch_by_activator( user_id ) if list_entities: list_products = [] for i, item in enumerate( list_entities ): list_products.append( item.product_name ) answer.update({ 'products_owned' : list_products }) 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 get_data(self): user_id = self.enki_user.key.id() licences_to_activate = [] # licences purchased by the user, available to activate or give. The user can only activate one licence per product. Licences activated by another user don't appear. licences_to_give = [] # licences purchased by the user, available to give only as the user already activated a licence for the same product. licences_activated = [] # licences activated byt the user (user purchased or received as gift). list_purchased = EnkiModelProductKey.fetch_by_purchaser(user_id) if list_purchased: for i, item in enumerate(list_purchased): item_licence_key = EnkiModelProductKey.insert_dashes_5_10( item.licence_key) product_already_owned = EnkiModelProductKey.exist_product_by_activator( user_id, item.product_name) if item.activated_by_user == -1: if not product_already_owned: licences_to_activate.append([ item.product_name, item_licence_key, settings.product_displayname[item.product_name] ]) else: licences_to_give.append([ item.product_name, item_licence_key, settings.product_displayname[item.product_name] ]) list_activated = EnkiModelProductKey.fetch_by_activator(user_id) if list_activated: for i, item in enumerate(list_activated): item_licence_key = EnkiModelProductKey.insert_dashes_5_10( item.licence_key) licences_activated.append([ item.product_name, item_licence_key, settings.product_displayname[item.product_name] ]) error = self.session.pop('error_library', None) licence_key_value = self.session.pop('error_library_licence', None) data = [ error, licences_to_activate, licences_to_give, licences_activated, licence_key_value ] return data