class Auth(db.Model): __tablename__='auths' id = db.Column('auth_id', db.BigInteger, primary_key=True) auth_type = db.Column(auth_types_emun, nullable=False, server_default='PHONE') identifier = db.Column(db.String(128), unique=True, nullable=False) token = db.Column(db.String(128), nullable=False)
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(32), index=True, unique=True, nullable=False) email = db.Column(db.String(64), index=True, unique=True) password_hash = db.Column(db.String, index=True, nullable=False) avatar_url = db.Column(db.String) gender = db.Column(db.Boolean, default=False) # True: male; Flase: female reputation = db.Column(db.Float, default=0.0) school = db.Column(db.String) # this field can also be used as `role` status = db.Column(db.String) # 'NORMAL', 'ADMIN', 'FREEZE' created_time = db.Column(db.DateTime) # managed_courses = db.relationship('Course', secondary=course_managers, back_populates='managers') contributes = db.relationship('Version', backref='contributor') def __init__(self, email, password): self.nickname = f'学霸 {email}' self.email = email self.password_hash = generate_password_hash(password) self.avatar_url = generate_avatar() self.gender = False self.reputation = 0.0 self.school = None self.status = 'NORMAL' self.created_time = datetime.now() @property def password(self): raise AttributeError('Password can not access') @password.setter def password(self, value): self.password_hash = generate_password_hash(value) def check_password(self, password): return check_password_hash(self.password_hash, password) def is_active(self): return self.is_active # def is_anonymous(self): # return False # def is_authenticated(self): # return True def get_id(self): return self.id def __repr__(self) -> str: return f'User< {self.id} {self.nickname}>'
class File(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) file_type = db.Column(db.String, ) contributes = db.relationship('User', secondary=contributes, lazy='subquery', backref=db.backref('contributes', lazy=True)) file_url = db.Column(db.String, nullable=False) created_time = db.Column(db.DateTime) updated_time = db.Column(db.DateTime) file_size = db.Column(db.Float) # KB file_info = db.Column(db.String) # i.e. git commit message course_id = db.Column(db.Integer, db.ForeignKey('course.id'), nullable=False)
class Version(db.Model): id = db.Column(db.Integer, primary_key=True) contributor_id = db.Column(db.Integer, db.ForeignKey('user.id')) description = db.Column(db.String) created_time = db.Column(db.DateTime) status = db.Column(db.String) # 'OPEN', 'ACCEPTED', 'REJECTED' # 'OPEN': when a new changed is created # 'ACCEPTED': change is accepted by course manager # 'REJECTED': change is rejected by course manager content = db.Column(db.Text) previous_version_id = db.Column(db.Integer, db.ForeignKey('version.id')) chapter_id = db.Column(db.Integer, db.ForeignKey('chapter.id')) course_id = db.Column(db.Integer, db.ForeignKey('course.id')) def __repr__(self) -> str: return f'<Version {self.created_time}>'
class User(db.Model): id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(32), index=True, unique=True, nullable=False) avatar_url = db.Column(db.String(128), index=False, nullable=True) school = db.Column(db.String(64), index=False, unique=False, nullable=True) phone = db.Column(db.String(11), index=True, unique=True, nullable=False) token = db.Column(db.String(128))
class Chapter(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) notes = db.Column(db.String) created_time = db.Column(db.DateTime) updated_time = db.Column(db.DateTime) latest_version_id = db.Column(db.Integer, db.ForeignKey('version.id')) # The following two fields defines the chapter order # see https://docs.sqlalchemy.org/en/14/orm/self_referential.html previous_chapter_id = db.Column(db.Integer, db.ForeignKey('chapter.id')) next_chapter = db.relationship('Chapter', uselist=False) course_id = db.Column(db.Integer, db.ForeignKey('course.id')) course = db.relationship('Course', back_populates='first_chapter') # dumbfl def __repr__(self) -> str: return f'<Chapter {self.name}>'
class Course(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) # managers = db.relationship('User', secondary=course_managers, back_populates='managed_courses') manager_id = db.Column(db.Integer, db.ForeignKey('user.id')) description = db.Column(db.String) cover_url = db.Column(db.String) created_time = db.Column(db.DateTime) first_chapter = db.relationship('Chapter', back_populates='course', uselist=False) def __repr__(self) -> str: return f'<Course {self.name}>'
from plugins import db contributes = db.Table( 'contribute', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True), db.Column('file_id', db.Integer, db.ForeignKey('file.id'), primary_key=True), db.Column('update_time', db.DateTime)) class File(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, unique=True, nullable=False) file_type = db.Column(db.String, ) contributes = db.relationship('User', secondary=contributes, lazy='subquery', backref=db.backref('contributes', lazy=True)) file_url = db.Column(db.String, nullable=False) created_time = db.Column(db.DateTime) updated_time = db.Column(db.DateTime) file_size = db.Column(db.Float) # KB file_info = db.Column(db.String) # i.e. git commit message course_id = db.Column(db.Integer, db.ForeignKey('course.id'), nullable=False)