def leave_game(user, game): """ Remove a user from a game. TODO: Need to manage the situation of multiple user game lists. """ user = get_model(user, User) game = get_model(game, models.Game) users_game_list = models.UserGameList.get_or_create_addable_game_list(user) # Check to make sure that the user is in the game if user.key not in game.players: raise NotFoundException # Check to make sure that the game is found with the user if game.key not in users_game_list.games: raise NotFoundException # Remove user from the game game.players.remove(user.key) game.players_joined -= 1 # Remove game from the user users_game_list.remove_game(game.key) if len(game.players) == 0: game.key.delete() else: game.put() users_game_list.put() return game
def test_get_model(self): a = models.get_model(self.dummy, DummyModel) b = models.get_model(self.dummy.key, DummyModel) c = models.get_model(self.dummy.key.id(), DummyModel) assert self.dummy == a assert self.dummy == b assert self.dummy == c
def add_comment_to_game(user, game_key, text): """ Adds a comment to a game written. """ user = get_model(user, User) game = ndb.Key(urlsafe=game_key).get() comment = models.GameComment(user=user.key, body=text) number = models.GameCommentThread.get_thread_number(game.key) if number is None: new_key = models.GameCommentThread.make_key(game.key, 1) thread = models.GameCommentThread(key=new_key) else: thread = models.GameCommentThread.get_by_key(game.key, number) # If this thread is full, create another one if thread.is_full(): new_key = models.GameCommentThread.make_key(game.key, thread.key.id() + 1) #pylint: disable=maybe-no-member thread = Thread(key=new_key) thread.comments.append(comment) thread.put() #pylint: disable=maybe-no-member return game_comment_to_message(comment)
def get_upcoming(auth_user): """Gets the upcoming games that a user is attending.""" user = get_model(auth_user, User) game_list = models.UserGameList.get_or_create_addable_game_list(user) games = ndb.get_multi(game_list.games) upcoming_games = [g for g in games if g.time > datetime.now()] upcoming_games.sort(key=lambda g: g.time) return list_of_games_to_message(upcoming_games)
def join_game(user, game): """Adds a user to a game.""" user = get_model(user, User) game = get_model(game, models.Game) if game.players_full: raise exceptions.GameIsFullException() # Add user to the game game.players.append(user.key) game.players_joined += 1 # Add game to the user game_list = models.UserGameList.get_or_create_addable_game_list(user) game_list.add_game(game.key) game.put() game_list.put() return game
def delete_sport_profile(auth_user, msg): """ Delete a sport profile. Param: msg - messages.SportProfileRequest """ user = get_model(auth_user, User) profile_key = models.SportProfile.make_key(user.key, msg.sport_category_id) profile_key.delete()
def get_user_sport_profiles(user): """ Returns a list of all a user's profiles. """ user = get_model(user, User) query = models.SportProfile.query(ancestor=user.key) profiles = query.fetch() msg = messages.SportProfileList() msg.profiles = [sports_profile_to_message(p) for p in profiles] return msg
def add_sport_profile(auth_user, msg): """ Creates a new sport profile. Param: msg - messages.SportProfileRequest """ user = get_model(auth_user, User) sport = models.SportCategory.get_by_name(msg.sport_category_id) profile = models.SportProfile(parent=user.key, sport=sport.key, level=msg.level) profile.put() return sports_profile_to_message(profile)
def test_get_model_not_found(self): models.get_model("alskfjadlkfja", DummyModel)