class Surface(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(16)) name = db.Column(db.String(128)) category = db.Column(db.String(8)) def __repr__(self): return f"<Surface(id={self.id}, code='{self.code}', " \ f"name='{self.name}')>"
class Player(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) first_name = db.Column(db.String(64)) last_name = db.Column(db.String(64)) dob = db.Column(db.Date, nullable=True) @property def name(self): return f"{self.first_name} {self.last_name}"
class Subdivision(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(8)) name = db.Column(db.String(32)) division = db.Column(db.Integer) @property def teams(self): _teams = [] for c in self.conferences: _teams += c.members return _teams def __repr__(self): return f"<Subdivision(id={self.id}, code='{self.code}')>"
class Conference(db.Model): id = db.Column(db.Integer, primary_key=True) subdivision_id = db.Column(db.Integer, db.ForeignKey("subdivision.id")) code = db.Column(db.String(8), unique=True, nullable=False) name = db.Column(db.String(256), unique=True, nullable=False) short_name = db.Column(db.String(32), unique=True, nullable=True) inception_year = db.Column(db.Integer) subdivision = db.relationship("Subdivision", backref="conferences") @property def links(self): return dict() def __repr__(self): return f"<Conference(id={self.id}, code='{self.code}'," \ f" name='{self.name}', short_name='{self.short_name}')>"
class ExternalTeamIdentifier(db.Model): id = db.Column(db.Integer, primary_key=True) team_id = db.Column(db.Integer, db.ForeignKey("team.id")) source_id = db.Column(db.Integer, db.ForeignKey("data_source.id")) value = db.Column(db.String(128)) external_id = db.Column(db.Integer, nullable=True) team = db.relationship("Team", backref="external_identifiers") source = db.relationship("DataSource")
class Stadium(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(16)) name = db.Column(db.String(128)) state = db.Column(db.String(2)) city = db.Column(db.String(64)) latitude = db.Column(db.String(16)) longitude = db.Column(db.String(16)) built = db.Column(db.Integer) capacity = db.Column(db.Integer) surface_id = db.Column(db.Integer, db.ForeignKey("surface.id")) surface = db.relationship("Surface", backref="stadiums") def __repr__(self): return f"<Stadium(id={self.id}, name='{self.name}')>"
class DataSource(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(32), unique=True) name = db.Column(db.String(128), unique=True) url = db.Column(db.String(256))
class Network(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) code = db.Column(db.String(16), unique=True) name = db.Column(db.String(128), unique=True) website = db.Column(db.String(256))
class Poll(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) code = db.Column(db.String(16)) name = db.Column(db.String(128)) url = db.Column(db.String(256))
class LocationType(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=False) name = db.Column(db.String(32), unique=True, nullable=False) def __repr__(self): return f"<LocationType(id={self.id}, name='{self.name}')>"
class Team(db.Model): id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(8), unique=True) name = db.Column(db.String(128), unique=True, nullable=False) short_name = db.Column(db.String(64), nullable=True) mascot = db.Column(db.String(128), nullable=False) conference_id = db.Column(db.Integer, db.ForeignKey('conference.id')) division_id = db.Column(db.Integer, db.ForeignKey('division.id')) stadium_id = db.Column(db.Integer, db.ForeignKey('stadium.id')) conference = db.relationship('Conference', backref='members') division = db.relationship('Division', backref='members') stadium = db.relationship('Stadium', uselist=False, backref='team') played_in = db.relationship('GameParticipant', backref='team') @property def games(self): return [g.game for g in self.played_in] @property def home_games(self): return [g.game for g in self.played_in if g.location_type_id == 1] @property def wins(self): return len([ g for g in self.games if g.winner and g.winner.team_id == self.id ]) @property def losses(self): return len([ g for g in self.games if g.winner and g.winner.team_id != self.id ]) @property def conference_wins(self): return len([ g for g in self.games if g.winner and g.winner.team_id == self.id and g.loser.team.conference_id == self.conference_id ]) @property def conference_losses(self): return len([ g for g in self.games if g.winner and g.winner.team_id != self.id and g.winner.team.conference_id == self.conference_id ]) def record(self, season: int): return { "season": season, "wins": self.wins, "losses": self.losses, "confWins": self.conference_wins, "confLosses": self.conference_losses } def __repr__(self): return f"<Team(id={self.id}, name='{self.name}', " \ f"mascot='{self.mascot}', code='{self.code}')>"