class TasksToTournamets(db.Model): id = db.Column(db.Integer, primary_key=True) tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.id')) task_id = db.Column(db.Integer, db.ForeignKey('task.id')) tournament = db.relationship('Tournament', backref=db.backref('taskstotournaments', lazy='select'), lazy='subquery') task = db.relationship('Task', backref=db.backref('taskstotournaments', lazy='select'), lazy='subquery') def __init__(self, tournament_id, task_id, task, tournament): self.tournament_id = tournament_id self.task_id = task_id self.task = task self.tournament = tournament def add(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all(): return TasksToTournamets.query.all()
class Attempt(db.Model): id = db.Column(db.Integer, primary_key=True) task_id = db.Column(db.Integer(), db.ForeignKey('task.id')) try_flag = db.Column(db.String(256)) success = db.Column(db.Boolean()) time = db.Column(db.DateTime()) score = db.Column(db.Integer()) tournament_to_object_id = db.Column( db.Integer, db.ForeignKey('tournaments_to_object.id')) tournament_to_object = db.relationship('TournamentsToObject', backref=db.backref('attempts', lazy='select'), lazy='select') task = db.relationship('Task', backref=db.backref('task', lazy='select'), lazy='select') def __init__(self, flag, success, **kwargs): self.try_flag = flag self.success = success self.score = 0 self.time = dt.now() if 'tournament_to_object' in kwargs: self.tournament_to_object_id = kwargs['tournament_to_object'] if 'task' in kwargs: self.task_id = kwargs['task'] self.score = Task.get_by_id(self.task_id).score @staticmethod def get_scoreboard_for_contestant(tournament_to_object): scorehistory = db.session.query(Attempt). \ filter((Attempt.tournament_to_object_id == tournament_to_object) & Attempt.success).all() return scorehistory @staticmethod def already_solved(tournament_to_object_id, task_id): return bool( db.session.query(Attempt).filter( (Attempt.tournament_to_object_id == tournament_to_object_id) & (Attempt.task_id == task_id) & (Attempt.success == True)).first()) def save(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() def get_all(self): return db.session(Attempt).query.all()
class Team(Contestant): id = db.Column(db.Integer, db.ForeignKey('contestant.id'), primary_key=True) name = db.Column(db.String(128)) city = db.Column(db.String(128)) invite_code = db.Column(db.String(1024)) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref('created_teams', lazy='dynamic'), foreign_keys='Team.creator_id') members = db.relationship('User', secondary=user_to_team, backref=db.backref('teams', lazy='select')) __mapper_args__ = { 'polymorphic_identity': 'team' } def __init__(self, name, city, invite_code, creator): self.name = name self.city = city self.invite_code = invite_code self.creator_id = creator.id self.creator = creator self.members.append(creator) def save(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all(): return Team.query.all() @staticmethod def get_by_code(code): return Team.query.filter(Team.invite_code == code).first() def add_new_member(self, user): user.team_id = self.id self.members.append(user) db.session.commit() def remove_member(self, user): self.members.remove(user) db.session.commit() def if_user_in_the_team(self, user): return user in self.members @staticmethod def get_by_id(id): return db.session.query(Team).filter(Team.id == id).first()
class Safe(db.Model): __tablename__ = 'safe' pass_id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255)) entered_on = db.Column(db.DateTime()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('safe')) def __init__(self, name=None, password=None): self.username = username self.password = password self.registered_on = datetime.datetime.now()
class ProjectTbl(Base, Named, db.Model): comment = db.Column(db.BLOB) checkin_date = db.Column(db.Date) principal_investigatorID = db.Column(db.Integer, db.ForeignKey('PrincipalInvestigatorTbl.id')) principal_investigator = db.relationship('PrincipalInvestigatorTbl', backref=db.backref('projects', lazy=True))
class SampleTbl(Base, Named, db.Model): projectID = db.Column(db.Integer, db.ForeignKey('ProjectTbl.id')) materialID = db.Column(db.Integer, db.ForeignKey('MaterialTbl.id')) project = db.relationship('ProjectTbl', backref=db.backref('samples', lazy=True)) material = db.relationship('MaterialTbl', backref=db.backref('materials', lazy=True))
class TournamentsToObject(db.Model): id = db.Column(db.Integer, primary_key=True) tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.id')) contestant_id = db.Column(db.Integer, db.ForeignKey('contestant.id')) tournament = db.relationship('Tournament', backref=db.backref('tournamentstoobject', lazy='select'), lazy='subquery') contestant = db.relationship('Contestant', backref=db.backref('tournamentstoobject', lazy='select'), lazy='subquery') def __init__(self, tournament_id, contestant_id): self.tournament_id = tournament_id self.contestant_id = contestant_id def add(self): db.session.add(self) db.session.commit() return self.id @staticmethod def delete(tournament_id, contestant_id): TournamentsToObject.query.filter( TournamentsToObject.tournament_id == tournament_id and TournamentsToObject.contestant_id == contestant_id).delete() db.session.commit() @staticmethod def get_all_obj_in_tournament(id_tournament): return db.session.query(TournamentsToObject.contestant).filter( TournamentsToObject.tournament_id == id_tournament).all() @staticmethod def get_all_objects_to_tournament(tournament_id): return db.session.query(TournamentsToObject).filter( TournamentsToObject.tournament_id == tournament_id).all() @staticmethod def is_exist(tournament_id, contestant_id): if db.session.query(TournamentsToObject).filter( TournamentsToObject.tournament_id == tournament_id and TournamentsToObject.contestant_id == contestant_id).first(): return True return False @staticmethod def get_one_or_none(tournament_id, contestant_id): return db.session.query(TournamentsToObject).filter( TournamentsToObject.tournament_id == tournament_id and TournamentsToObject.contestant_id == contestant_id).first() @staticmethod def get_all(): return TournamentsToObject.query.all() @staticmethod def get_all_people_in_tournament(id_tournament): return db.session.query(TournamentsToObject).filter( TournamentsToObject.tournament_id == id_tournament).all() @staticmethod def get_all_by_user_id(contestant_id): return db.session.query(TournamentsToObject).filter( TournamentsToObject.contestant_id == contestant_id).all() def get_solved_tasks_for_tournament_for_contestant_indices(self): return db.session.query(Attempt.task_id).filter( (Attempt.success == True) & (Attempt.tournament_to_object_id == self.id))
class Tournament(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(256)) description = db.Column(db.String(1024)) private = db.Column(db.Boolean()) platform = db.Column(db.Boolean()) invite_link = db.Column(db.String(1024)) creator_id = db.Column(db.Integer(), db.ForeignKey('contestant.id')) creator = db.relationship("Contestant", backref=db.backref("tournaments", lazy='dynamic')) time = db.Column(db.DateTime()) time_to_live = db.Column(db.DateTime()) place = db.Column(db.String(256)) online = db.Column(db.Boolean()) for_team_allowed = db.Column(db.Boolean()) def __init__(self, name, description, creator, time, time_to_live, place, invite_link, private=False, platform=True, online=False, for_team_allowed=True): self.name = name self.description = description self.private = private self.platform = platform self.creator = creator self.time = time self.time_to_live = time_to_live self.place = place.replace(';', ',') self.online = online self.invite_link = invite_link self.for_team_allowed = for_team_allowed def add(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all(): return Tournament.query.all() @staticmethod def get_tournaments_in_future(time_to_live): return db.session.query(Tournament).filter( Tournament.time > time_to_live).all() @staticmethod def allowed_to_user(user_id, tournament_id): return True # TODO: check @staticmethod def update_by_id(id, key, value): db.session.query(Tournament).filter(Tournament.id == id).update( {key: value}, synchronize_session='evaluate') db.session.commit() @staticmethod def is_exist_by_id(id): if Tournament.query.filter(Tournament.id == id): return True return False @staticmethod def get_info(tournament_id): return db.session.query(Tournament).filter( Tournament.id == tournament_id).one()