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 check_price_alerts(latest_ticker: Ticker): # only get non-hit alerts alerts = PriceAlert.select().where((PriceAlert.hit == False) & (PriceAlert.coin == latest_ticker.coin)) for alert in alerts: if alert.above: if alert.strike_price < latest_ticker.price: send_notification(alert.profile, f'{latest_ticker.coin.name} is now above {alert.strike_price}!') alert.hit = True alert.save() else: if alert.strike_price > latest_ticker.price: send_notification(alert.profile, f'{latest_ticker.coin.name} is now below {alert.strike_price}!') alert.hit = True alert.save()
def test_users_can_access_only_own_notifications(self): msg = 'This is a notification' send_notification(self.profile, msg) Notification.get((Notification.content == msg) & (Notification.profile == self.profile)) res = self.client.get( '/notification', headers={'Authorization': 'Bearer ' + self.token.token}) self.assertEqual(200, res._status_code) self.assertEqual(1, len(res.json['notifications'])) res = self.client.get( '/notification', headers={'Authorization': 'Bearer ' + self.token1.token}) self.assertEqual(200, res._status_code) self.assertEqual(0, len(res.json['notifications']))
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 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 check_global_timed_game(): game = Game.get(Game.shareable_link == 'TIMED') if game.ends_at < datetime.utcnow(): # end global timed game, and start another profiles = [] for game_profile in GameProfile.select().where(GameProfile.game == game): profiles.append(game_profile.profile) send_notification(game_profile.profile, 'The global timed game has expired') game_profile.delete_instance(recursive=True) game.delete_instance(recursive=True) global_timed = Game.create(name='Global Timed', starting_cash=10000.00, shareable_link='TIMED', shareable_code='TIMED', ends_at=datetime.utcnow() + timedelta(minutes=1)) # CHANGEME for devel purposes, making it 1 min for now GameCoin.create(game=global_timed, coin=Coin.get()) for profile in profiles: GameProfile.create( game=global_timed, profile=profile, cash=global_timed.starting_cash )
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 test_send_notification_creates_notifications(self): msg = 'This is a notification' send_notification(self.profile, msg) Notification.get((Notification.content == msg) & (Notification.profile == self.profile))