def _delete_friendship_in(origin_username, destiny_username): Logger(__name__).info('Deleting Friendship state in {}: {}.'.format( origin_username, destiny_username)) entry = "friends." + destiny_username new_origin = User._delete_field_by_username(origin_username, {entry: ""}) Logger(__name__).info( 'Friendship state in {}: {} is now deleted.'.format( origin_username, destiny_username)) return new_origin
def sanitize_boolean(input_data): if input_data in ['true', 'True', True]: Logger(__name__).info( 'Input {} recognized as boolean True.'.format(str(input_data))) return True if input_data in ['false', 'False', False]: Logger(__name__).info( 'Input {} recognized as boolean False.'.format( str(input_data))) return False Logger(__name__).info('Input {} not recognized as a boolean.'.format( str(input_data))) raise InvalidFormatException(input_data, "boolean")
def _change_friendship_in_to(origin_username, destiny_username, new_friendship_state): Logger(__name__).info( 'Changing Friendship state in {}: {} to state {}.'.format( origin_username, destiny_username, str(new_friendship_state))) entry = "friends." + destiny_username new_origin = User._update_user_by_username( origin_username, {entry: new_friendship_state}) Logger(__name__).info( 'Friendship state in {}: {} is now at state {}.'.format( origin_username, destiny_username, str(new_friendship_state))) return new_origin
def identify(token): """Receives a token and looks for it in the database, returning the username of the owner or raising an exception if it was not found or had expired""" Logger(__name__).info('Looking for token {}'.format(token)) tk = Token._find_token(int(token)) if tk is None: Logger(__name__).info("Token {} not found".format(token)) raise InvalidTokenException if Token._get_current_epochs() > tk['expiresAt']: Logger(__name__).info( "Token {} was found but it had expired".format(token)) raise ExpiredTokenException Logger(__name__).info("Token {} found".format(token)) return tk['username']
def save_new(token, expiration_epochs, username): """Saves to DB a newly created Token with expiration date (in epochs) associated with a user""" tk = Token._get_tokens_db().find_one({'username': username}) new_token_id = Token._create_token(token, expiration_epochs, username) Logger(__name__).info('Token {} stored for user {}, with id {}'.format( token, username, new_token_id)) # enforce only one active session if tk is not None: Logger(__name__).info( 'Old token ({}) removed for user ({}).'.format( token, username)) Token._get_tokens_db().remove({'_id': tk['_id']}) return new_token_id
def wants_to_not_be_friends_with(origin_username, destiny_username): """Declares an intention from origin to not be friends with destiny, being that a rejection of a previously received friendship request or the deletion of an existing friendship. Returns a FriendshipState which can be request_received or friends depending on the previous state.""" Logger(__name__).info( 'Username {} wants to NOT be friends with {}.'.format( origin_username, destiny_username)) # retrieve the DB user representations origin_user = User._get_one({'username': origin_username}) destiny_user = User._get_one({'username': destiny_username}) # if one of them was not found raise exception if origin_user is None or destiny_user is None: raise UserNotFoundException # if origin doesnt know destiny or origin was the one requesting raise exception if destiny_username not in origin_user['friends'] or \ origin_user['friends'][destiny_username] == FRIENDSHIP_STATE_SENT: raise NotFriendsException # if here they were already friends or destiny had sent request to origin, so delete assert Friendship._are_friends(origin_username, destiny_username) or \ Friendship._had_sent_request(destiny_username, origin_username) return Friendship._reject_friendship(origin_username, destiny_username)
def wants_to_be_friends_with(origin_username, destiny_username): """Declares an intention from origin to be friends with destiny, sending a request to destiny or confirming the relation if the inverse had been previously stated. Returns a FriendshipState which can be request_sent or friends depending on the previous.""" Logger(__name__).info( 'Username {} wants to be friends with {}.'.format( origin_username, destiny_username)) # retrieve the DB user representations origin_user = User._get_one({'username': origin_username}) destiny_user = User._get_one({'username': destiny_username}) # if one of them was not found raise exception if origin_user is None or destiny_user is None: raise UserNotFoundException # if destiny had NOT already wanted to be friends with origin if origin_username not in destiny_user['friends']: return Friendship._send_request_from_to(origin_username, destiny_username) # if destiny is already friends with origin if Friendship._are_friends(origin_username, destiny_username): raise AlreadyFriendsException # only other cases are: invitation already sent or needed confirmation # and this first case should not be possible assert _user(destiny_username )['friends'][origin_username] == FRIENDSHIP_STATE_SENT assert _user(origin_username)['friends'][ destiny_username] == FRIENDSHIP_STATE_RECEIVED # so now it should be confirmed return Friendship._confirm_friends(origin_username, destiny_username)
def delete_comment(comment_id): """Delete the comment identified by comment_id.""" Logger(__name__).info("Deleting comment id {}".format(comment_id)) deleted_comment = StoryComment._delete_one(comment_id) if deleted_comment is None: raise StoryCommentNotFoundException return deleted_comment['_id']
def add_item_to_one(target_collection, query, pushed_param_dict): Logger(__name__).debug('Pushing to one element of collection {} matching query {} with value {}'.format( target_collection.name, query, pushed_param_dict)) return target_collection.find_one_and_update(filter=query, update={"$push": pushed_param_dict}, return_document=ReturnDocument.AFTER)
def update_one(target_collection, query, updated_param_dict): Logger(__name__).debug('Updating one element in collection {} matching query {} with value {}'.format( target_collection.name, query, updated_param_dict)) return target_collection.find_one_and_update(filter=query, update={"$set": updated_param_dict}, return_document=ReturnDocument.AFTER)
def _delete_field_by_username(username, deleted_param_dict): Logger(__name__).info( 'Deleting fields of user {} with value {}'.format( username, deleted_param_dict)) return Persistence.unset_on_one(User._get_coll(), {'username': username}, deleted_param_dict)
def get_field_from_request_or_default(key, default_value): try: return RequestBuilder.get_field_from_request(key) except MissingFieldException: Logger(__name__).error("Default value returned: {}".format( str(default_value))) return default_value
def get_flash_feed_for_username(username): Logger(__name__).info('Getting flash-feed for user {}.'.format(username)) # retrieve up to 10 flashes per friend feed_flashes = User.get_feed_flashes(username, FEED_FLASHES_PER_USER) Logger(__name__).info('Serving {} flashes for user {}\'s flash-feed.'.format( len(feed_flashes), username)) # sort by timestamp in descending order and format (add name and profile_pic) prioritized_flashes = [FlashFeedBuilder._format_feed_flash(srz_flash) for srz_flash in sorted(feed_flashes, key=lambda ffd: ffd['timestamp'], reverse=True)] Logger(__name__).info('Serving flash-feed for user {}.'.format(username)) return prioritized_flashes
def get_comment(comment_id): """Get comment identified by comment_id or raise StoryCommentNotFoundException if none was found.""" Logger(__name__).info("Retrieving comment id {}".format(comment_id)) comment_obj = StoryComment._get_one_by_id(comment_id) if comment_obj is None: raise StoryCommentNotFoundException return StoryComment._serialize_comment(comment_obj)
def _are_friends(username1, username2): Logger(__name__).info( 'Evaluating whether users {} and {} are friends.'.format( username1, username2)) if username2 in _user(username1)['friends'] and \ _user(username1)['friends'][username2] == FRIENDSHIP_STATE_FRIENDS: return True return False
def get_story(story_id): """Get story represented by story_id formatted to be JSON serializable, or raise StoryNotFound exception if no story was found""" Logger(__name__).info('Looking for story {}.'.format(story_id)) story = Story._get_one_by_id(story_id) if story is None: raise StoryNotFoundException return Story._serialize_story(story)
def _had_sent_request(origin_username, destiny_username): Logger(__name__).info( 'Evaluating whether user {} wanted to be friends with {} ' 'beforehand.'.format(origin_username, destiny_username)) if _user(destiny_username )['friends'][origin_username] == FRIENDSHIP_STATE_RECEIVED: return True return False
def get_flash(flash_id): """Get flash represented by flash_id formatted to be JSON serializable, or raise FlashNotFoundException exception if no flash was found""" Logger(__name__).info('Looking for flash {}.'.format(flash_id)) flash = Flash._get_one_by_id(flash_id) if flash is None: raise FlashNotFoundException return Flash._serialize_flash(flash)
def get_profile(username, caller_username): """Returns username's profile or raises a UserNotFoundException""" user = User._get_one({'username': username}) if user is None: raise UserNotFoundException profile = User._build_profile_from_user(user, caller_username) Logger(__name__).info( "Profile for user {} was retrieved".format(username)) return profile
def delete_user(username): """Deletes user from the Application Server or raises a UserNotFoundException""" user = User._get_one({'username': username}) if user is None: raise UserNotFoundException deleted_name = User._safe_delete_user(user) Logger(__name__).info( "Account for user {} was deleted at User".format(deleted_name)) return deleted_name
def react_to_story(story_id, username, sanitized_reaction): """React to story represented by story_id as username, with reaction 'reaction'""" if Story._get_one_by_id(story_id) is None: raise StoryNotFoundException updated_story = Story._update_story( story_id, {"reactions." + username: sanitized_reaction}) Logger(__name__).info("User {} has reacted {} to Story_id {}.".format( username, sanitized_reaction, story_id)) return updated_story["reactions"][username]
def change_account_info(username, new_data): Logger(__name__).info( 'Changing account info for user {}.'.format(username)) User._update_user_by_username(username, new_data) new_user_data = {} updated_user = _user(username) for field in new_data.keys(): new_user_data[field] = updated_user[field] return new_user_data
def delete_flash(flash_id): """Safely delete flash""" Logger(__name__).info("Deleting flash_id {}.".format(flash_id)) # try to delete it deleted_flash = Flash._delete_one(flash_id) if deleted_flash is None: raise FlashNotFoundException return str(deleted_flash['_id'])
def get_friends(username): """Returns a list of username friends as profile previews, or raises UserNotFoundException if none was found.""" Logger(__name__).info( 'Retrieving friends for user {}.'.format(username)) friend_ids = Friendship._get_friends_of(username) return [ User.get_profile_preview(friend_id) for friend_id in friend_ids ]
def get_account_info(username): """Gets account information related to username""" user = User._get_one({'username': username}) if user is None: raise UserNotFoundException account_info = User._make_account_info_from_user(user) Logger(__name__).info( "Account info for user {} was retrieved".format(username)) return account_info
def comment_on_story(story_id, username, comment_text, timestamp): """Comment on a story represented by story_id as username, with comment_text and at timestamp.""" Logger(__name__).info( 'Trying to set comment from user {} on story {} at timestamp {}.'. format(username, story_id, timestamp)) if Story._get_one_by_id(story_id) is None: raise StoryNotFoundException new_comment_id = StoryComment.make_new_comment(comment_text, username, timestamp, story_id) return StoryComment.get_comment(new_comment_id)
def delete_reaction(story_id, username): """Delete username's reaction on the story represented by story_id.""" Logger(__name__).info( 'Deleting reaction from user {} on story {}.'.format( username, story_id)) updated_story = Story._delete_field_on_story( story_id, {"reactions." + username: ""}) if updated_story is None: raise StoryReactionNotFoundException return updated_story["reactions"][username]
def save_new_flash(flash_data): """Facade for Flash.save_new, also checks that user indeed exists""" username = flash_data['username'] Logger(__name__).info( 'Trying to save new flash for user {}.'.format(username)) # check user exists user = User._get_one({'username': username}) if user is None: raise UserNotFoundException return Flash.save_new(flash_data)
def delete_deprecated_flashes(): # get all and keep deprecated ones Logger(__name__).info('Deleting deprecated flashes') target_ids = [str(flash_obj['_id']) for flash_obj in Flash._get_all() if Flash._flash_is_deprecated(flash_obj)] # delete one by one for target_id in target_ids: Flash._delete_one(target_id) return len(target_ids)
def delete_flashes_from_user(username): """Delete all flashes uploaded by user username""" Logger(__name__).info("Deleting all flashes from user {}.".format(username)) flash_ids = [str(flash_obj['_id']) for flash_obj in Flash._get_many({'username': username})] deleted_ids = [] for flash_id in flash_ids: deleted_ids.append(Flash.delete_flash(flash_id)) return deleted_ids