class Race(db.Model): id = db.Column("race_id", db.Integer(), primary_key=True) date = db.Column("race_date", db.DateTime(), nullable=False) status = db.Column(db.String(), nullable=False) round_id = db.Column(db.Integer(), db.ForeignKey("round.round_id")) def __init__(self, id, date, status, round_id): self.id = id self.date = date self.status = status self.round_id = round_id def __repr__(self): return "<Race\nid: {}\n date: {}\n status: {}\n round_id: {}>".format( self.id, self.date, self.status, self.round_id) def get_race(self, id): return self.query.filter_by(id=id).first() def get_all_races(self): return self.query.all() @staticmethod def get_round_race_ids(round_id): return db.session.query(Race.id).filter_by(round_id=round_id).all()
class Feed(db.Model): """A feed that is being scraped for new posts""" __tablename__ = "feeds" id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(2000)) parser = db.Column(db.String(20)) time_format = db.Column(db.String(50)) last_updated = db.Column(db.TIMESTAMP(timezone=True)) def serialize(self): return {'id': self.id, 'url': self.url} def __str__(self): return f"'id': '{self.id}', 'url': '{self.url}', 'parser': '{self.parser}', 'time_format': '{self.time_format}', 'last_updated': '{self.last_updated}'"
class User(db.Model): """A user of the RSS Feed Aggregator application""" __tablename__ = "users" username = db.Column(db.String(256), primary_key=True) password = db.Column(db.String(128)) def hash_password(self, password): self.password = pbkdf2_sha256.hash(password) def verify_password(self, password): return pbkdf2_sha256.verify(password, self.password) def __str__(self): return f"User('usernam'e='{self.username}')"
class Trainer(db.Model): __tablename__ = "trainers" id = db.Column("trainer_id", db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) def __init__(self, id, name): self.id = id self.name = name def __repr__(self): return "<Trainer\nid: {}\n name: {}>".format(self.id, self.name) def get_trainer(self, id): return self.query.filter_by(id=id).first()
class Follows(db.Model): """Describes a follow relationship between a User and a Feed. For each feed a user follows, a record will be present in this table """ __tablename__ = "follows" id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String, db.ForeignKey('users.username')) user = db.relationship('User', backref=db.backref('follows', lazy=True)) feed_id = db.Column(db.Integer, db.ForeignKey('feeds.id')) feed = db.relationship('Feed', backref=db.backref('follows', lazy=True)) def __str__(self): pass
class Read(db.Model): """Describes an relationship between a User and a FeedItem Every item from a feed that a user follows that was already seen is considered an Unread item and has an entry here """ __tablename__ = "reads" id = db.Column(db.Integer(), primary_key=True) username = db.Column(db.String, db.ForeignKey('users.username')) user = db.relationship('User', backref=db.backref('reads', lazy=True)) item_id = db.Column(db.Integer, db.ForeignKey('feed_items.id')) item = db.relationship('FeedItem', backref=db.backref('reads', lazy=True)) feed_id = db.Column(db.Integer, db.ForeignKey('feeds.id')) feed = db.relationship('Feed', backref=db.backref('reads', lazy=True))
class Racecard(db.Model): id = db.Column("race_card_id", db.Integer(), primary_key=True) race_id = db.Column(db.Integer(), nullable=False) snail_id = db.Column(db.Integer(), nullable=False) def __init__(self, race_id, snail_id): self.race_id = race_id self.snail_id = snail_id def __repr__(self): return "<Racecard\nid: {}\n race_id: {}\n snail_id: {}>".format( self.id, self.race_id, self.snail_id) def get_racecard(self, id): return self.query.filter_by(id=id).first() def get_all_racecards(self): return self.query.all()
class Snail(db.Model): __tablename__ = "snails" id = db.Column("snail_id", db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) trainer_id = db.Column(db.Integer, db.ForeignKey('trainers.trainer_id')) def __init__(self, id, name, trainer_id): self.id = id self.name = name self.trainer_id = trainer_id def __repr__(self): return "<Snail\nid: {}\n name: {}\n trainer_id: {}>".format( self.id, self.name, self.trainer_id) def get_snail(self, id): return self.query.filter_by(id=id).first() def get_all_snails(self): return self.query.all()
class RoundResult(db.Model): __tablename__ = "roundresult" id = db.Column("round_result_id", db.Integer(), primary_key=True) user_id = db.Column(db.Integer(), nullable=False) round_id = db.Column(db.Integer(), nullable=False) score = db.Column(db.Integer(), nullable=False) def __init__(self, user_id, round_id, score): self.user_id = user_id self.round_id = round_id self.score = score def __repr__(self): return "<Roundresult\nid: {}\n user_id: {}\n round_id: {}\n score: {}>".format( self.id, self.user_id, self.round_id, self.score) def get_racecard(self, id): return self.query.filter_by(id=id).first() def get_all_racecards(self): return self.query.all()
class RaceResult(db.Model): __tablename__ = "raceresult" id = db.Column("race_result_id", db.Integer(), primary_key=True) race_id = db.Column(db.Integer(), db.ForeignKey("race.race_id")) snail_id = db.Column(db.Integer(), db.ForeignKey("snails.snail_id")) position = db.Column(db.Integer(), nullable=False) def __init__(self, race_id, snail_id, position): self.race_id = race_id self.snail_id = snail_id self.position = position def __repr__(self): return "<Race Result\nid: {}\n race_id: {}\n snail_id: {}\n position: {}>" \ .format(self.id, self.race_id, self.snail_id, self.position) def get_race_result(self, id): return self.query.order_by(RaceResult.position).filter_by(snail_id=id).first() def get_all_race_results(self): return self.query.all()
class Round(db.Model): __tablename__ = 'round' id = db.Column("round_id", db.Integer(), primary_key=True) name = db.Column("round_name", db.String(20), nullable=False) start_date = db.Column(db.DateTime(), nullable=False) closed = db.Column(db.Boolean(), nullable=False) def __init__(self, id, name, start_date): self.id = id self.name = name self.start_date = start_date self.closed = False def __repr__(self): return "<Round\nid: {}\n name: {}\n start_date: {}\n closed: {}>".format(self.id, self.name, self.start_date, self.closed) def get_round(self, id): return self.query.filter_by(round_id=id).first() def get_all_rounds(self): return self.query.all()
class FeedItem(db.Model): """A single post associated with a specific feed""" __tablename__ = "feed_items" id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(2000)) title = db.Column(db.String(100)) description = db.Column(db.String(5000)) feed_id = db.Column(db.Integer, db.ForeignKey('feeds.id')) feed = db.relationship('Feed', backref=db.backref('feed_items', lazy=True)) published = db.Column(db.TIMESTAMP(timezone=True)) def serialize(self): return { 'id': self.id, 'url': self.url, 'title': self.title, 'description': self.description, 'published': self.published, } def __str__(self): return f"FeedItem(id='{self.id}', title='{self.title}', feed_id='{self.feed_id}', url='{self.url}', published='{self.published}')"