def get_value(self, username, token, key): """ Returns the value indexed by the specified key. If there is no entry indexed by that key, None is returned. """ logging.debug('### get_value() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return None else: kvp = json.loads(kvp) try: logging.debug('\tGetting value for key {} for user {}'.format( key, username)) value = kvp[key] logging.debug('\t\tValue for key {} is {}'.format(key, value)) return value except KeyError: # key-value entry does not exist, return None logging.warning('\t\t[!!]Key {} not found'.format(key)) return None
def get_key_value_bin(self, username, token): """ Returns the key-value dictionary for this user. """ logging.debug('### get_key_value_bin ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return {} else: kvp = json.loads(kvp) return kvp
def delete_message(self, username, password, msg_id): msg_obj = Message.get(id=msg_id) if not msg_obj: # if no message with such id found, just ignore logging.warning( '[!!] Request to delete message with id {},' ' which does NOT exist. Ignoring request...'.format(msg_id)) return requestor = User.get(username=username) if msg_obj.author != requestor: raise AuthorizationError( 'User \'{}\' cannot delete message with ' 'id {}, since that message was created by \'{}\''.format( username, msg_id, msg_obj.author)) msg_obj.delete() logging.debug('Deleted message with id {}'.format(msg_id))
def delete_location(self, username, password, location_name): location = Location.get(name=location_name) if not location: # if no message with such id found, just ignore logging.warning( '[!!] Request to delete location with name {},' ' which does NOT exist. Ignoring request...'.format( location_name)) return requestor = User.get(username=username) if location.author != requestor: raise AuthorizationError( 'User \'{}\' cannot delete location with ' 'name {}, since that location was created by \'{}\''.format( username, location_name, location.author)) location.delete() logging.debug('Deleted location with name {}'.format(location_name))
def update_key(self, username, token, key, value): """ Updates or adds (if does not exist) the key-value pair to the user's profile. To remove a key, simply update it with the None value. """ logging.debug('### update_key() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning( '\tKVP for user "{}" is None, instantiating'.format(username)) kvp = {} else: kvp = json.loads(kvp) logging.debug('\tAssociating {}:{} for user {}'.format( key, value, username)) kvp[key] = value u.kvp = json.dumps(kvp)
def delete_key(self, username, token, key): """ Deltes the key-value pair with the provided key from the user's profile. If a pair with that key does not exist, it's ignored. """ logging.debug('### delete_key() ###') u = User.get(username=username) kvp = u.kvp if kvp is None: logging.warning('\tKVP is None for user {}'.format(username)) return else: kvp = json.loads(kvp) try: logging.debug('\tDeleting key {} for user {}'.format( key, username)) del kvp[key] u.kvp = json.dumps(kvp) except KeyError: # all good: deleting a non-existing key, just ignore logging.debug('\t\t[!!] Key {} was non-existent'.format(key)) pass