def delete(self, player_id): """ DELETE request for Player Route: Routes['player']/<player_id: int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) oauths = OAuth.query.filter(OAuth.player_id == player_id).all() for oauth in oauths: DB.session.delete(oauth) query = TeamRequest.query.filter(TeamRequest.email == player.email) for team_request in query.all(): DB.session.delete(team_request) # delete a single user DB.session.delete(player) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.PLAYER, item=player.json()) return response
def post(self): """ POST request for the upcoming games for the given player Route: Route['botupcominggames'] Parameters: player_id: the id of the player (int) Returns: status: 200 mimetype: application/json result: list of games """ args = parser.parse_args() player_id = args['player_id'] player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) today = date.today() + timedelta(days=-1) next_two_weeks = today + timedelta(days=14) games = DB.session.query(Game) for team in player.teams: games = games.filter( or_(Game.away_team_id == team.id, Game.home_team_id == team.id)) games = games.filter(Game.date.between(today, next_two_weeks)) result = [] for game in games: result.append(game.json()) return Response(dumps(result), status=200, mimetype="application/json")
def post(self): """ POST request for the upcoming games for the given player Route: Route['kikupcominggames'] Parameters: name: the player's full name (str) Returns: status: 200 mimetype: application/json data: id: the captain's team id """ args = parser.parse_args() name = args['name'] players = (DB.session.query(Player).filter( Player.name.like("%" + name + "%"))).all() if players is None or len(players) == 0: raise PlayerDoesNotExist(payload={'details': name}) teams = [] today = date.today() next_two_weeks = today + timedelta(days=14) for player in players: for team in player.teams: teams.append(team.id) games = DB.session.query(Game).filter( or_(Game.away_team_id.in_(teams), (Game.home_team_id.in_(teams)))) games = games.filter(Game.date.between(today, next_two_weeks)) result = [] for game in games: result.append(game.json()) return Response(dumps(result), status=200, mimetype="application/json")
def put(self, player_id): """ PUT request for Player Route: Routes['player']/<player_id: int> Parameters : player_name: The player's name (string) gender: a one letter character representing gender (string) email: the players email (string) active: 1 if player is active and 0 otherwise (int) Returns: if found and successful status: 200 mimetype: application/json data: None if found but new email is duplicate status: NUESC mimetype: application/json data: None if found but invalid field status: IFSC mimetype: application/json data: None othwerwise status: 404 mimetype: application/json data: None """ # update a single user player = DB.session.query(Player).get(player_id) args = parser.parse_args() if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) player_name = None gender = None email = None active = True if args['player_name']: player_name = args['player_name'] if args['gender']: gender = args['gender'] if args['email']: email = args['email'] if args['active']: active = args['active'] == 1 if True else False player.update(name=player_name, gender=gender, email=email, active=active) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") handle_table_change(Tables.PLAYER, item=player.json()) return response
def post(self): """ POST request for submitting a transaction Route: Route['bottransaction'] Parameters: player_id: the id of the player submitting the receipt (int) amount: the amount spent (int) sponsor: the name of the sponsor (str) team_id: the id of the team (int) Returns: status: 200 mimetype: application/json data: True """ args = parser.parse_args() player_id = args['player_id'] team_id = args['team_id'] amount = args['amount'] sponsor_name = args['sponsor'] # ensure the player exists player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) # ensure the team exist team = Team.query.get(team_id) if team is None: raise TeamDoesNotExist(payload={'details': team_id}) # ensure the sponsor exists sponsor = Sponsor.query.filter_by(name=sponsor_name).first() if sponsor is None: raise SponsorDoesNotExist(payload={'details': sponsor_name}) # player can only submit receipts to their own team if not team.is_player_on_team(player): raise PlayerNotOnTeam(payload={'details': player_id}) # should be good to add the espy now espy = Espys(team_id=team.id, sponsor_id=sponsor.id, points=amount, description="Bot transaction") DB.session.add(espy) DB.session.commit() handle_table_change(Tables.ESPYS) return Response(dumps(espy.id), status=200, mimetype="application/json")
def post(self): """ POST request for retrieving a captain games that needs scores submitted Route: Route['kikcaptaingames'] Parameters: team: the team's id (str) kik: the captain's kik user name (str) Returns: status: 200 mimetype: application/json result: list of games objects """ args = parser.parse_args() team_id = args['team'] kik = args['kik'] team = Team.query.get(team_id) if team is None: raise TeamDoesNotExist(payload={'details': team_id}) if team.player_id is None: raise InvalidField( payload={"details": "Team's captain has not been set"}) team_captain = Player.query.get(team.player_id) if team_captain is None: raise PlayerDoesNotExist(payload={'details': team.player_id}) if team_captain.kik != kik: # something fishy is going on raise NotTeamCaptain(payload={'details': team_captain.kik}) # captain is authenticated # get all the bats for that team and its game id bats = (DB.session.query( Bat.game_id).filter(Bat.team_id == team_id)).all() if (len(bats) > 0): games = (DB.session.query(Game).filter( or_(Game.away_team_id == team_id, Game.home_team_id == team_id)).filter( Game.date <= datetime.today()).filter( ~Game.id.in_(bats))).all() else: games = (DB.session.query(Game).filter( or_(Game.away_team_id == team_id, Game.home_team_id == team_id)).filter( Game.date <= datetime.today())).all() # now get the teams games, that have past game date and have no bats result = [] for game in games: result.append(game.json()) return Response(dumps(result), status=200, mimetype="application/json")
def get(self, player_id): """ GET request for Player List Route: Routes['player']/<player_id: int> Returns: if found status: 200 mimetype: application/json data: {player_id:int, player_name:string, gender: string} otherwise status: 404 mimetype: application/json data: None """ # expose a single user entry = Player.query.get(player_id) if entry is None: raise PlayerDoesNotExist(payload={'details': player_id}) response = Response(dumps(entry.json()), status=200, mimetype="application/json") return response
def update(self, player_id=None, team_id=None, game_id=None, rbi=None, hit=None, inning=None): """Update an existing bat. Raises: TeamDoesNotExist GameDoesNotExist PlayerDoesNotExist InvalidField """ if team_id is not None and Team.query.get(team_id) is not None: self.team_id = team_id elif team_id is not None: raise TeamDoesNotExist(payload={'details': team_id}) if game_id is not None and Game.query.get(game_id) is not None: self.game_id = game_id elif game_id is not None: raise GameDoesNotExist(payload={'details': game_id}) if player_id is not None and Player.query.get(player_id) is not None: self.player_id = player_id elif player_id is not None: raise PlayerDoesNotExist(payload={'details': player_id}) if rbi is not None and rbi_validator(rbi): self.rbi = rbi elif rbi is not None: raise InvalidField(payload={'details': "Bat - rbi"}) if hit is not None and hit_validator(hit): self.classification = hit elif hit is not None: raise InvalidField(payload={'details': "Bat - hit"}) if inning is not None and inning_validator(inning): self.inning = inning elif inning is not None: raise InvalidField(payload={'details': "Bat - inning"})
def insert_player(self, player_id: int, captain: bool = False) -> None: """Insert a player on to the team. Parameter: player_id: the id of the player to add captain: True if the player is the team's captain Returns: True if player was added False otherwise Raises: PlayerDoesNotExist """ valid = False player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) if not self.is_player_on_team(player): self.players.append(player) valid = True if captain: self.player_id = player_id valid = True return valid
def __init__(self, player_id, team_id, game_id, classification, inning=1, rbi=0): """The constructor. Raises: PlayerDoesNotExist InvalidField TeamDoesNotExist GameDoesNotExist """ # check for exceptions classification = classification.lower().strip() player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) if not hit_validator(classification, player.gender): raise InvalidField(payload={'details': "Bat - hit"}) if not rbi_validator(rbi): raise InvalidField(payload={'details': "Bat - rbi"}) if not inning_validator(inning): raise InvalidField(payload={'details': "Bat - inning"}) if Team.query.get(team_id) is None: raise TeamDoesNotExist(payload={'details': team_id}) if Game.query.get(game_id) is None: raise GameDoesNotExist(payload={'details': game_id}) # otherwise good and a valid object self.classification = classification self.rbi = rbi self.player_id = player_id self.team_id = team_id self.game_id = game_id self.inning = inning
def insert_player(self, player_id, captain=False): """Insert a player on to the team. Parameter: player_id: the id of the player to add captain: True if the player is the team's captain Returns: True if player was added False otherwise Raises: PlayerDoesNotExist """ valid = False player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) if captain: self.player_id = player_id if player not in self.players: self.players.append(player) else: if player not in self.players: self.players.append(player) return valid
def delete(self, player_id): """ DELETE request for Player Route: Routes['player']/<player_id: int> Returns: if found status: 200 mimetype: application/json data: None otherwise status: 404 mimetype: application/json data: None """ player = Player.query.get(player_id) if player is None: raise PlayerDoesNotExist(payload={'details': player_id}) # delete a single user DB.session.delete(player) DB.session.commit() response = Response(dumps(None), status=200, mimetype="application/json") return response