def post(self): if self.ensure_is_logged_in() and self.ensure_has_display_name(): self.check_CSRF() user_id = self.user_id friend_id_invite = self.request.get('invite') friend_id_remove = self.request.get('remove') friend_name_search = self.request.get('search').strip()[:( EnkiModelDisplayName.DISPLAY_NAME_LENGTH_MAX + 4)] # 4 allows for some leading and trailing characters already_friends = '' has_friends = EnkiModelFriends.exist_by_user_id(user_id) error_message = '' result = '' if friend_id_invite: # send invitation to user to become friend outcome = EnkiModelFriends.send_friend_request( user_id, int(friend_id_invite)) if outcome == EnkiModelFriends.INFO_FRIENDS: self.add_infomessage( MSG.SUCCESS(), MSG.FRIEND_ADDED( EnkiModelDisplayName.get_display_name( int(friend_id_invite)))) elif outcome == enki.libutil.ENKILIB_OK: self.add_infomessage( MSG.SUCCESS(), MSG.FRIEND_INVITATION_SENT( EnkiModelDisplayName.get_display_name( int(friend_id_invite)))) elif friend_id_remove: # unfriend EnkiModelFriends.remove_friend(user_id, int(friend_id_remove)) has_friends = EnkiModelFriends.exist_by_user_id(user_id) self.add_infomessage( MSG.SUCCESS(), MSG.FRIEND_REMOVED( EnkiModelDisplayName.get_display_name( int(friend_id_remove)))) elif friend_name_search: # search for user to invite users_ids_to_ignore = [user_id] if has_friends: users_ids_to_ignore += EnkiModelFriends.get_friends_user_id( user_id) result = EnkiModelDisplayName.find_users_by_display_name( friend_name_search, users_ids_to_ignore) if result.error == EnkiModelDisplayName.ERROR_DISPLAY_NAME_INVALID: error_message = MSG.DISPLAY_NAME_INVALID() elif result.error == EnkiModelDisplayName.ERROR_DISPLAY_NAME_NOT_EXIST: error_message = MSG.DISPLAY_NAME_NOT_EXIST() else: error_message = MSG.DISPLAY_NAME_NEEDED() if has_friends: already_friends = EnkiModelFriends.get_friends_user_id_display_name_url( user_id) self.render_tmpl('friends.html', data=already_friends, error=error_message, result=result, friend_name=friend_name_search)
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=(',',':') ))