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 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 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 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 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 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
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 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()