def create_request(requester_name, requestee_name, status): requester_profile = Profile.get_or_none(Profile.username == requester_name) requestee_profile = Profile.get_or_none(Profile.username == requestee_name) if requester_profile is None: raise BadRequest('Could not find requester') if requestee_profile is None: send_notification(requester_profile, f'User {requestee_name} does not exist.') raise BadRequest('Could not find requestee') if requester_profile == requestee_profile: send_notification(requester_profile, f'Cannot friend yourself!') raise BadRequest('Cannot friend self') check_duplicates = Friends.get_or_none( (Friends.requester == requester_profile) & (Friends.requestee == requestee_profile)) if check_duplicates is not None: send_notification( requester_profile, f'Friend request to {requestee_profile.username} has already been created' ) raise BadRequest('Friend request has already been made') req = Friends.create(requester=requester_profile, requestee=requestee_profile, status=status) send_notification( requester_profile, f'Friend request to {requestee_profile.username} has been sent!') send_notification( requestee_profile, f'{requester_profile.username} has sent you a friend request!') return req
def register(username: str, password: str): profile = Profile.get_or_none(Profile.username == username) if profile is not None: raise BadRequest('A user with this username already exists') hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() profile = Profile.create( username=username, hashed_password=hashed, ) global_game = Game.get_or_none(Game.name == 'Global Indefinite') GameProfile.create(game=global_game, profile=profile, cash=global_game.starting_cash) global_game = Game.get_or_none(Game.name == 'Global Timed') GameProfile.create(game=global_game, profile=profile, cash=global_game.starting_cash) send_notification(profile, 'Welcome to Fortune!') send_notification( profile, 'Click the Play button in the menubar on top to create a new game.') send_notification( profile, 'To adjust account options, see achiements, and add friends, click on your username on the top and select Profile' ) return create_auth_token(profile)
def test_get_coins_with_invalid_time_span(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=10000) res = self.client.get( '/game/1/coins?timeSpan=6&sortBy=0&numPerPage=10&pageNum=1', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.BAD_REQUEST), res._status_code)
def login(username: str, password: str): profile = Profile.get_or_none(Profile.username == username) if profile is None: raise BadRequest('A user with this username does not exist') if profile.is_banned: raise BadRequest("This user has been banned.") if not bcrypt.checkpw(password.encode(), profile.hashed_password.encode()): raise BadRequest('Incorrect password') return create_auth_token(profile)
def accept_request(requester_name, requestee_name): requester_profile = Profile.get_or_none(Profile.username == requester_name) requestee_profile = Profile.get_or_none(Profile.username == requestee_name) if requester_profile is None: raise BadRequest('Could not find requester') if requestee_profile is None: raise BadRequest('Could not find requestee') req = Friends.get_or_none((Friends.requester == requester_profile) & (Friends.requestee == requestee_profile)) if req is None: raise BadRequest('Could not find request') req.status = 1 other_way_friendship = Friends.create(requester=requestee_profile, requestee=requester_profile, status=1) send_notification( requester_profile, f'{requestee_profile.username} has accepted your friend request!') return req
def send(profile): validated_data: dict = FriendsSerializer.deserialize(request.json) requester_name = validated_data['requester'] requestee_name = validated_data['requestee'] status = validated_data['status'] friend_req = create_request(requester_name, requestee_name, status) if friend_req is None: raise BadRequest('Could not create request') pending = get_pending_by_profile(Profile.get_or_none(Profile.username == requestee_name)) return jsonify(PendingList.serialize({'pending': pending,}))
def change_username(profile_id: int, username: str): profile = Profile.get_or_none(Profile.username == username) if profile is not None: raise BadRequest('A user with this username already exists') try: update_username = Profile.update({ Profile.username: username }).where(Profile.id == profile_id) update_username.execute() except Exception as e: raise BadRequest('Failure to change username: {}'.format(str(e)))
def ban_user(profile_id: int): rows = Profile.update({ Profile.is_banned: True }).where(Profile.id == profile_id).execute() if rows == 0: raise BadRequest("Cannot ban user") profile = Profile.get_or_none(Profile.id == profile_id) if not profile: raise BadRequest("Profile not found") print("BANNED PROFILE IS BANNED:", profile.is_banned) broadcast(profile, "ban", "Your account has been banned for violating our policy.", True)
def test_buy_coin_without_cash(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) Ticker.create(coin=1, price=10, price_change_day_pct=1.1) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.BAD_REQUEST), res._status_code)
def test_liquefy_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.delete( '/game/1/coins', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def test_get_coins_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') GameProfile.create(game=1, profile=profile, cash=10000) GameCoin.create( game=1, coin=1, ) for coin in Coin.select(): Ticker.create( coin=coin, price=30.0, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.get( '/game/1/coins?timeSpan=1&sortBy=0&numPerPage=10&pageNum=1', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def test_sell_coin_success(self): profile = Profile.get_or_none(Profile.username == 'theusername') game_profile = GameProfile.create(game=1, profile=profile, cash=0) GameCoin.create(game=1, coin=1) GameProfileCoin.create(game_profile=game_profile, coin=1, coin_amount=2) Ticker.create( coin=1, price=10, captured_at=(datetime.utcnow()).isoformat(), price_change_day_pct=1.1, ) res = self.client.post( '/game/1/coin', data=json.dumps({ 'coinId': '1', 'coinAmount': '-1', }), content_type='application/json', headers={'Authorization': 'Bearer ' + self.token}) self.assertEqual(int(HTTPStatus.OK), res._status_code)
def warn_user(profile_id, message): profile = Profile.get_or_none(Profile.id == profile_id) if not profile: raise BadRequest("Cannot find the user's profile.") send_notification(profile, "Warning: " + message)
def get_profile_to_notify(user_id: int): profile = Profile.get_or_none(Profile.id == user_id, ~Profile.is_admin) if not profile: raise BadRequest("Invalid user")