class Vote(db.Model): id = db.Column(db.Integer, primary_key=True) v_q_id = db.Column(db.Integer, db.ForeignKey('question.id'), nullable=False) v_u_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Vote('{self.v_count}')"
class Task(db.Model): __tablename__ = "tasks" id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_id = db.Column(db.Integer, db.ForeignKey('groups.id')) formulae = db.Column(db.Text, nullable=False) contexts = db.relationship('Context', secondary=tags, lazy='subquery', backref=db.backref('tasks', lazy=True)) @classmethod def init(cls, group_id: int, formulae: str) -> "Task": task = cls(group_id=group_id, formulae=formulae) split = formulae.strip().split('$') res = set() for i, term in enumerate(split): if i % 2 == 1: res.add(Context.query.filter_by(term=term).first()) task.contexts = list(res) db.session.add(task) db.session.commit() return task def __repr__(self): return "<Task(id={}, group_id={}, formulae='{}')>".format( self.id, self.group_id, self.formulae)
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): return '<Post {}>'.format(self.body)
class Comment(db.Model): id = db.Column(db.Integer, primary_key=True) cm_body = db.Column(db.Text, nullable=False) cm_q_id = db.Column(db.Integer, db.ForeignKey('question.id', ondelete='CASCADE'), nullable=False) cm_status = db.Column(db.Boolean, default=True, nullable=False) cm_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) cm_datecreate = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) cm_vote = db.Column(db.Integer, nullable=True, default=0) def __repr__(self): return f"Comments('{self.cm_body}', '{self.cm_datecreate}')"
class Highlight(db.Model): id = db.Column(db.Integer, primary_key=True) video_id = db.Column(db.Integer, db.ForeignKey('video.id'), nullable=False) offset = db.Column(db.Integer, nullable=False) score = db.Column(db.Integer, nullable=False) emotion = db.Column(db.String(200)) def __repr__(self): return f'<Highlight of {self.video_id}>'
class Employee(db.Model): id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(64)) last_name = db.Column(db.String(64)) departament_id = db.Column(db.Integer, db.ForeignKey('departament.id')) departament = db.relationship('Departament', backref='employee') def __repr__(self): return f'<Employee ({self.id}) {self.first_name} {self.last_name} from {self.departament_id}>'
class Exercise(db.Model): __tablename__ = "exercises" id = db.Column(db.Integer, primary_key=True, autoincrement=True) description_url = db.Column(db.Text) start_task = db.Column(db.Integer, db.ForeignKey('groups.id')) tries = db.relationship('Try', backref='exercises', lazy=True) def __repr__(self): return "<Exercise(id={}, start_task={})>".format( self.id, self.start_task)
class TaskGroup(db.Model): __tablename__ = "groups" id = db.Column(db.Integer, primary_key=True) next_group = db.Column(db.Integer, db.ForeignKey('groups.id'), nullable=True) exercises = db.relationship('Exercise', backref='groups', lazy=True) tasks = db.relationship('Task', backref='groups', lazy=True) tries = db.relationship('Try', backref='groups', lazy=True) def __repr__(self): return "<Group(id={}, next_group={})>".format(self.id, self.next_group)
class Question(db.Model): id = db.Column(db.Integer, primary_key=True) q_title = db.Column(db.Text, nullable=False) q_datecreate = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) q_tag_name = db.Column(db.Text, nullable=True) q_vote = db.Column(db.Integer, nullable=False, default=0) q_status = db.Column(db.Boolean, nullable=False) q_body = db.Column(db.Text, nullable=False) q_user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) comments = db.relationship('Comment', backref='article', cascade="all, delete-orphan", lazy='dynamic', passive_deletes=True) votes = db.relationship('Vote', backref='question', lazy='dynamic') def __repr__(self): return f"question('{self.q_title}', '{self.q_datecreate}', '{self.q_status}')"
class Video(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200), nullable=False) image_url = db.Column(db.String(200), nullable=False) author_id = db.Column(db.Integer, db.ForeignKey('author.id'), nullable=False) highlights = db.relationship('Highlight', backref='video', lazy=True) processed = db.Column(db.Boolean, nullable=False, default=False) @property def serialize(self): return { 'id': self.id, 'image': self.image_url, 'profile': self.author.image_url, 'name': self.name, 'author': self.author.name, } def __repr__(self): return '<Video %r>' % self.name
class Try(db.Model): __tablename__ = "tries" id = db.Column(db.Integer, primary_key=True) result = db.Column(db.Integer, nullable=False) text = db.Column(db.Text, nullable=False) started_at = db.Column(db.DateTime, nullable=True) finished_at = db.Column(db.DateTime, nullable=True) uid = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True) eid = db.Column(db.Integer, db.ForeignKey('exercises.id'), nullable=False) step = db.Column(db.Integer, db.ForeignKey('groups.id')) @classmethod def init(cls, user, exercise): # type: (Any["AnonymousUserLogin", "UserLogin"], "Exercise") -> "Try" uid = user.get_id() if uid: log("User {} starts the exercise {}".format(user.id, exercise.id)) else: log("Anonymous starts the exercise {}".format(exercise.id)) new_try = cls(result=0, text="", started_at=datetime.now(), finished_at=None, uid=uid, eid=exercise.id, step=exercise.start_task) db.session.add(new_try) db.session.commit() user.add_try(new_try) return new_try def append(self, sentence: str) -> None: self.text = " ".join([self.text, sentence]) db.session.commit() def inc_fault(self) -> None: self.result += 1 db.session.commit() def next(self) -> bool: """ Update stage of the current try Return True if uses finished exercise """ self.step = self.groups.next_group if self.step: flash("Правильно!", "success") db.session.commit() return False flash("Упражнение завершено.", "success") self.finish(True) return True def finish(self: "Try", success: bool) -> None: fin_type = "finished" if success else "cancelled" if self.uid: u_type = "User {}".format(self.uid) else: u_type = "Anonymous" log("{} {} the exercise {}".format(u_type, fin_type, self.eid)) self.finished_at = datetime.now() if not success: self.result = -1 if self.users: db.session.add(self) db.session.commit() def __repr__(self): return "<Try(id={}, user={}, exercise={}, step={})>".format( self.id, self.uid, self.eid, self.step)
@lm.user_loader def load_user(uid: str) -> Optional[UserLogin]: """ Necessary Flask_login callback """ return UserLogin.get(int(uid)) # Exercises tags = db.Table( 'tags', db.Column('task_id', db.Integer, db.ForeignKey('tasks.id'), primary_key=True), db.Column('context_term', db.Integer, db.ForeignKey('contexts.id'), primary_key=True)) class Try(db.Model): __tablename__ = "tries" id = db.Column(db.Integer, primary_key=True) result = db.Column(db.Integer, nullable=False) text = db.Column(db.Text, nullable=False) started_at = db.Column(db.DateTime, nullable=True) finished_at = db.Column(db.DateTime, nullable=True) uid = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True)
class players(db.Model): player_id = db.Column(db.String(64), index=True, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
image_url = db.Column(db.String(200), nullable=False) videos = db.relationship('Video', backref='author', lazy=True) @property def serialize(self): return {'id': self.id, 'image': self.image_url, 'name': self.name} def __repr__(self): return '<Author %r>' % self.name users_videos = db.Table( 'users_videos', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('video_id', db.Integer, db.ForeignKey('video.id'), primary_key=True)) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) videos = db.relationship('Video', secondary=users_videos, lazy='subquery', backref=db.backref('users', lazy=True))