class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) user_username = db.Column(db.String(100), unique=True, nullable=False) user_email = db.Column(db.String(100), unique=True, nullable=True) user_image = db.Column(db.String(100), nullable=True, default='default.jpg') user_password = db.Column(db.String(100), nullable=False) user_status = db.Column(db.Boolean, nullable=False, default=False) questions = db.relationship('Question', backref='author', lazy=True) comments = db.relationship('Comment', backref='article_author', lazy=True) def vote_question(self, question): if not self.has_voted_question(question): vote = Vote(v_u_id=self.id, v_q_id=question.id) db.session.add(vote) db.session.commit() def unvote_question(self, question): if self.has_voted_question(question): Vote.query.filter_by(v_u_id=self.id, v_q_id=question.id).delete() db.session.commit() def has_voted_question(self, question): return Vote.query.filter(Vote.v_u_id == self.id, Vote.v_q_id == question.id).count() > 0 def __repr__(self): return f"User('{self.user_username}', '{self.user_email}', '{str(self.user_image)}')"
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 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 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)) def __init__(self, username, password): self.username = username self.password = User.hashed_password(password) @staticmethod def hashed_password(password): return bcrypt.generate_password_hash(password).decode("utf-8") @staticmethod def get_user_with_username_and_password(username, password): user = User.query.filter_by(username=username).first() if user and bcrypt.check_password_hash(user.password, password): return user else: return None def __repr__(self): return f"<User '{self.id}','{self.username}','{self.password}'>" @property def serialize(self): return { 'id': self.id, 'username': self.username, }
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 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 Author(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) 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
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 User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) 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 avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/' \ '{}?d=identicon&s={}'.format(digest, size)
class UserLogin(db.Model): """ Common user class with additional student's group attribute """ __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) login = db.Column(db.String(80), unique=True, index=True, nullable=False) group = db.Column(db.String(80)) hash = db.Column(db.Binary(60), nullable=False) tries = db.relationship('Try', backref='users', lazy=True) is_authenticated = True is_active = True is_anonymous = False @classmethod def init(cls, login: str, group: str, password: str) -> "UserLogin": """ Safe User constructor, performs hashing of the password Doesn't commit new user into the database """ user = cls(login=login, group=group, hash="") user.update_password(password) db.session.add(user) db.session.commit() return user def update_password(self, password): log("User {} changes the password".format(self.login)) self.hash = hashpw(bytes(password, "utf8"), gensalt()) db.session.commit() def check_password(self, password: str) -> bool: """ Compare stored hash with hash of input password """ return hashpw(bytes(password, "utf8"), self.hash) == self.hash @classmethod def get(cls: Type["UserLogin"], uid: int) -> Optional["UserLogin"]: """ Get User by user_id """ return cls.query.get(uid) def get_try(self, eid: int) -> Optional["Try"]: """ Check if user is doing exercise now """ for i in self.tries: if i.eid == eid and i.finished_at is None: return i return None def get_tries(self) -> List["Try"]: return self.tries def add_try(self, tr: "Try") -> None: pass def get_id(self) -> str: """ Necessary method for flask_login """ return str(self.id) def __repr__(self): return "<User(id='{}', group='{}', active={})>".format( self.id, self.group, self.in_action())