class Team(DB.Model): """ A class that stores information about a team. Columns: id: the unique team id sponsor_id: the sposor id the team is associated with home_games: the home games of the team away_games: the away games of the team players: the players on the team's roster bats: the bats of the team league_id: the league id the team is part of year: the year the team played espys: the espy transaction that team has """ id = DB.Column(DB.Integer, primary_key=True) color = DB.Column(DB.String(120)) sponsor_id = DB.Column(DB.Integer, DB.ForeignKey('sponsor.id')) home_games = DB.relationship('Game', backref='home_team', lazy='dynamic', foreign_keys='[Game.home_team_id]') away_games = DB.relationship('Game', backref='away_team', lazy='dynamic', foreign_keys='[Game.away_team_id]') players = DB.relationship('Player', secondary=roster, backref=DB.backref('teams', lazy='dynamic')) bats = DB.relationship('Bat', backref='team', lazy='dynamic') league_id = DB.Column(DB.Integer, DB.ForeignKey('league.id')) year = DB.Column(DB.Integer) player_id = DB.Column(DB.Integer, DB.ForeignKey('player.id')) espys = DB.relationship('Espys', backref='team', lazy='dynamic') def __init__(self, color=None, sponsor_id=None, league_id=None, year=date.today().year): """ The constructor. Raises InvalidField SponsorDoesNotExist LeagueDoesNotExist """ if color is not None and not string_validator(color): raise InvalidField(payload={'details': "Team - color"}) if sponsor_id is not None and Sponsor.query.get(sponsor_id) is None: raise SponsorDoesNotExist(payload={'details': sponsor_id}) if league_id is not None and League.query.get(league_id) is None: raise LeagueDoesNotExist(payload={'details': league_id}) if year is not None and not year_validator(year): raise InvalidField(payload={'details': "Team - year"}) self.color = color self.sponsor_id = sponsor_id self.league_id = league_id self.year = year self.kik = None def __repr__(self): """Returns the string representation.""" result = [] if self.sponsor_id is not None: sponsor = Sponsor.query.get(self.sponsor_id) if sponsor is not None and sponsor.nickname is not None: result.append(sponsor.nickname) if self.color is not None: result.append(self.color) return " ".join(result) def espys_awarded(self): """Returns the number of espy ponts the team has.""" count = 0 for espy in self.espys: count += espy.points return count def json(self): """Returns a jsonserializable object.""" captain = (None if self.player_id is None else Player.query.get( self.player_id).json()) return { 'team_id': self.id, 'team_name': str(self), 'color': self.color, 'sponsor_id': self.sponsor_id, 'league_id': self.league_id, 'year': self.year, 'espys': self.espys_awarded(), 'captain': captain } def update(self, color=None, sponsor_id=None, league_id=None, year=None): """Updates an existing team. Raises: InvalidField SponsorDoesNotExist LeagueDoesNotExist """ # does nothing with espys given if color is not None and string_validator(color): self.color = color elif color is not None: raise InvalidField(payload={'details': "Team - color"}) if (sponsor_id is not None and Sponsor.query.get(sponsor_id) is not None): self.sponsor_id = sponsor_id elif sponsor_id is not None: raise SponsorDoesNotExist(payload={'details': sponsor_id}) if league_id is not None and League.query.get(league_id) is not None: self.league_id = league_id elif league_id is not None: raise LeagueDoesNotExist(payload={'details': league_id}) if year is not None and year_validator(year): self.year = year elif year is not None: raise InvalidField(payload={'details': "Team - year"}) 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 remove_player(self, player_id): """Removes a player from a team. Parameter: player_id: the id of the player to remove Raises: MissingPlayer """ player = Player.query.get(player_id) if player not in self.players: raise PlayerNotOnTeam(payload={'details': player_id}) self.players.remove(player) def check_captain(self, player_name, password): """Checks if the player is the captain of the team. Parameters: player_name: the name of the player (str) password: the password of the player (str) Return: True of player is the captain False otherwise """ player = Player.query.get(self.player_id) return player.name == player_name and player.check_password(password) def team_stats(self): pass
class Team(DB.Model): """ A class that stores information about a team. Columns: id: the unique team id sponsor_id: the sposor id the team is associated with home_games: the home games of the team away_games: the away games of the team players: the players on the team's roster bats: the bats of the team league_id: the league id the team is part of year: the year the team played espys: the espy transaction that team has """ id = DB.Column(DB.Integer, primary_key=True) color = DB.Column(DB.String(120)) sponsor_id = DB.Column(DB.Integer, DB.ForeignKey('sponsor.id')) home_games = DB.relationship('Game', backref='home_team', lazy='dynamic', foreign_keys='[Game.home_team_id]') away_games = DB.relationship('Game', backref='away_team', lazy='dynamic', foreign_keys='[Game.away_team_id]') players = DB.relationship('Player', secondary=roster, backref=DB.backref('teams', lazy='dynamic')) bats = DB.relationship('Bat', backref='team', lazy='dynamic') league_id = DB.Column(DB.Integer, DB.ForeignKey('league.id')) year = DB.Column(DB.Integer) player_id = DB.Column(DB.Integer, DB.ForeignKey('player.id')) espys = DB.relationship('Espys', backref='team', lazy='dynamic') espys_total = column_property(select([func.sum(Espys.points)]).where( Espys.team_id == id).correlate_except(Espys), deferred=True) sponsor_name = column_property(select([Sponsor.nickname]).where( Sponsor.id == sponsor_id).correlate_except(Sponsor)) def __init__(self, color: str = None, sponsor_id: int = None, league_id: int = None, year: int = date.today().year): """ The constructor. Raises InvalidField SponsorDoesNotExist LeagueDoesNotExist """ if color is not None and not string_validator(color): raise InvalidField(payload={'details': "Team - color"}) if sponsor_id is not None and Sponsor.query.get(sponsor_id) is None: raise SponsorDoesNotExist(payload={'details': sponsor_id}) if league_id is not None and League.query.get(league_id) is None: raise LeagueDoesNotExist(payload={'details': league_id}) if year is not None and not year_validator(year): raise InvalidField(payload={'details': "Team - year"}) self.color = color self.sponsor_id = sponsor_id self.league_id = league_id self.year = year self.kik = None def __repr__(self) -> str: """Returns the string representation.""" if self.sponsor_name is not None and self.color is not None: return f"{self.sponsor_name} {self.color}" elif self.sponsor_name is not None: return f"{self.sponsor_name}" elif self.color is not None: return f"{self.color}" else: return f"Team: {self.id}" def json(self, admin: bool = False) -> dict: """Returns a jsonserializable object.""" if admin: captain = (None if self.player_id is None else Player.query.get(self.player_id).admin_json()) else: captain = (None if self.player_id is None else Player.query.get(self.player_id).json()) return { 'team_id': self.id, 'team_name': str(self), 'color': self.color, 'sponsor_id': self.sponsor_id, 'league_id': self.league_id, 'year': self.year, 'espys': self.espys_total if self.espys_total is not None else 0, 'captain': captain} def update(self, color: str = None, sponsor_id: int = None, league_id: int = None, year: int = None) -> None: """Updates an existing team. Raises: InvalidField SponsorDoesNotExist LeagueDoesNotExist """ # does nothing with espys given if color is not None and string_validator(color): self.color = color elif color is not None: raise InvalidField(payload={'details': "Team - color"}) if (sponsor_id is not None and Sponsor.query.get(sponsor_id) is not None): self.sponsor_id = sponsor_id elif sponsor_id is not None: raise SponsorDoesNotExist(payload={'details': sponsor_id}) if league_id is not None and League.query.get(league_id) is not None: self.league_id = league_id elif league_id is not None: raise LeagueDoesNotExist(payload={'details': league_id}) if year is not None and year_validator(year): self.year = year elif year is not None: raise InvalidField(payload={'details': "Team - year"}) 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 remove_player(self, player_id: int) -> None: """Removes a player from a team. Parameter: player_id: the id of the player to remove Raises: MissingPlayer """ player = Player.query.get(player_id) if not self.is_player_on_team(player): raise PlayerNotOnTeam(payload={'details': player_id}) self.players.remove(player) def is_player_on_team(self, player: 'Player') -> bool: """Returns whether the given player is on the team Parameter: player: the player model Returns: True if player on team otherwise False (boolean) """ if player is None: return False return player.id in [p.id for p in self.players] def check_captain(self, player_name: str, password: str) -> str: """Checks if the player is the captain of the team. Parameters: player_name: the name of the player (str) password: the password of the player (str) Return: True of player is the captain False otherwise """ player = Player.query.get(self.player_id) return player.name == player_name and player.check_password(password) def team_stats(self) -> None: pass