class Round(db.Model): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) name = db.Column(db.String(30), nullable=False) start_date = db.Column(db.DateTime(), nullable=False) end_date = db.Column(db.DateTime(), nullable=False) def __repr__(self): return "<Round\nid: {}\n name: {}\n start_date: {}>".format(self.id, self.name, self.start_date) def get_round(self, id): return self.query.filter_by(id=id).first() def get_all_rounds(self): return self.query.all() def get_num_rounds_between_dates(self, start_date, end_date): return db.session.query(func.count(distinct(Round.id))).filter( or_( and_(start_date > Round.start_date, start_date < Round.end_date), and_(end_date > Round.start_date, end_date < Round.end_date))).first() def get_active_round(self): return db.session.query(Round).filter(between(datetime.datetime.utcnow(), Round.start_date, Round.end_date)).first() def get_round_by_name(self, name): return db.session.query(Round).filter_by(name=name).first() def get_future_round_times(self): return db.session.query(Round).filter(Round.start_date > datetime.datetime.utcnow()).all()
class RaceParticipants(db.Model): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) id_snail = db.Column(db.Integer(), db.ForeignKey("snail.id")) id_race = db.Column(db.Integer(), db.ForeignKey("race.id")) def __repr__(self): return "<Race Participants\nid: {}\n snail_id: {}\n race_id: {}>".format(self.id, self.id_snail, self.id_race) def get_race_participant(self, id): return self.query.filter_by(id=id).first() def get_all_race_participants(self): return self.query.all() def get_race_participants_race_id(self, id_race): return self.query.filter_by(id_race=id_race).all() def get_race_results(self): return db.session.query(RaceParticipants, RaceResult).join( RaceResult, RaceParticipants.id == RaceResult.id_race_participants ).join( Race, Race.id == RaceParticipants.id_race ).all()
class Trainer(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(12), nullable=False) 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 Snail(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(12), nullable=False) trainer_id = db.Column(db.Integer, db.ForeignKey('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 Round(db.Model): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) name = db.Column(db.String(12), nullable=False) start_date = db.Column(db.DateTime(), nullable=False) end_date = db.Column(db.DateTime(), nullable=False) def __repr__(self): return "<Round\nid: {}\n name: {}\n start_date: {}>".format(self.id, self.name, self.start_date) def get_round(self, id): return self.query.filter_by(id=id).first() def get_all_rounds(self): return self.query.all()
class Trainer(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), nullable=False) 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() def get_trainer_by_name(self, name): return db.session.query(Trainer.name).filter(func.lower(Trainer.name) == func.lower(name)).scalar() is not None def get_all_trainers(self): return self.query.all()
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(128), index=True, unique=True) body = db.Column(db.String(1000), index=True, unique=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr(self): return 'Post {}'.format(self.body) def add_post(self): db.session.add(self) db.session.commit() def get_post(self, id): res = self.query.filter_by(id=id).first() return res
class RaceResult(db.Model): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) position = db.Column(db.Integer(), nullable=False) time_to_finish = db.Column(db.Integer(), nullable=False) did_not_finish = db.Column(db.Boolean(), nullable=False) id_race_participants = db.Column( db.Integer(), db.ForeignKey("race_participants.id")) def __repr__(self): return "<Race Result\nid: {}\n position: {}\n time_to_finish: {}\n did_not_finish: {}\n race_participants_id: {}>".format( self.id, self.position, self.time_to_finish, self.did_not_finish, self.id_race_participants) def get_race_result(self, id): return self.query.order_by(RaceResult.position).filter_by(id_race_participants=id).first() def get_all_race_results(self): return self.query.all()
class Race(db.Model): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) date = db.Column(db.DateTime(), nullable=False) status = db.Column(db.String(), nullable=False) id_round = db.Column(db.Integer(), db.ForeignKey("round.id")) def __repr__(self): return "<Race\nid: {}\n date: {}\n status: {}\n round_id: {}>".format(self.id, self.date, self.status, self.id_round) def get_race(self, id): return self.query.filter_by(id=id).first() def get_all_races(self): return self.query.all() def get_round_race_ids(self, id_round): return db.session.query(Race.id).filter_by(id_round=id_round).all()
class User(db.Model, UserMixin): id = db.Column(db.Integer(), primary_key=True, autoincrement=True) username = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), nullable=False) password = db.Column(db.String(128), nullable=False) def __repr__(self): return "<User\nid: {}\n username: {}\n email: {}\n password: {}>".format( self.id, self.username, self.email, self.password) def get_user(self, id): return self.query.filter_by(id=id).first() def get_users(self): return self.query.all() def get_user_by_username(self, username): return self.query.filter_by(username=username).first() def get_user_by_email(self, email): return self.query.filter_by(email=email).first()
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic', primaryjoin="User.id == Post.user_id") def __init__(self, new_user=False, username=None, email=None): if new_user: self.username = username self.email = email def __repr__(self): return '<User {}>'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def add_user(self): db.session.add(self) db.session.commit() def get_user(self, username): res = self.query.filter_by(username=username).first() return res def get_posts_by_user(self, id): res = self.query.filter_by(id=id).first() if res: return res.posts return None