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)