def update_match(match_id, is_accepted): match = Match.query.get(match_id) if not match: response_object = { 'status': 'fail', 'message': 'Match no encontrado' } return response_object, 404 match.status_request = 1 if is_accepted else -1 update() if is_accepted: friend1 = Friend(user_id=match.user_from_id, friend_id=match.user_to_id) friend2 = Friend(user_id=match.user_to_id, friend_id=match.user_from_id) save_changes(friend1) save_changes(friend2) response_object = { 'status': 'success', 'message': 'Solicitud actualizada correctamente' } return response_object, 200
def post(self, user_id): """ Accept the friend request Add the user into friend list Remove the request from friend request """ args = friendsParser.parse_args() profile_id = args['profile_id'] if profile_id is None: abort(400) success = Request.objects(user=user_id).only( 'requests_list').update_one(pull__requests_list=profile_id) if success is 0: abort(400) success = Friend.objects(user=user_id).only( 'friends_list').update_one(add_to_set__friends_list=profile_id) if success is 0: friends = Friend(user=user_id, friends_list=[profile_id]) friends.save() return {'status': 'success', 'message': 'The user has been added to your friend list'}
def post(self, user_id): """ Add a specific user to friend list, and send a friend request to that user """ args = friendsParser.parse_args() profile_id = args['profile_id'] if profile_id is None: abort(400) friend_profile = Profile.objects(id=profile_id).only('user').first() if friend_profile is None: abort(400) # add the user to friend list success = Friend.objects(user=user_id).only( 'friends_list').update_one(add_to_set__friends_list=profile_id) if success is 0: friends = Friend(user=user_id, friends_list=[profile_id]) friends.save() # put the friend request to the user's request list user_profile = Profile.objects(user=user_id).only('id').first() success = Request.objects(user=friend_profile.user).update_one( add_to_set__requests_list=user_profile) if success is 0: friend_request = Request( user=friend_profile.user, type='friends', requests_list=[user_profile]) friend_request.save() return {'status': 'success', 'message': 'The user has been added to your friend list'}
def create(profile, user): steamid = profile['steamid'] name = profile['personaname'] avatar = profile['avatar'] recently_played_games = user_data.recently_played_games(steamid) genres = [] for id in recently_played_games: genres.extend(game_factory.get_or_create(id).genres) friend_genre_scores = score.genre_scores(genres) user_genre_scores = json.loads(user.genre_scores) compatibility_score = score.compatibility_percentages( user_genre_scores, friend_genre_scores) friend_user = User.get_or_insert( key_name=steamid, games=recently_played_games, name=name, avatar=avatar, genre_scores=json.dumps(friend_genre_scores)) Friend(key_name=steamid, parent=user, user=friend_user, compatibility_score=compatibility_score).put()