class Role(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='role') permissions = db.relationship('Permission', secondary=roles_permissions, back_populates='roles') @staticmethod def init_role(): roles_permissions_map = { 'User': ['UPLOAD'], 'Moderator': ['UPLOAD', 'MODERATE'], 'Administrator': ['UPLOAD', 'MODERATE', 'ADMINISTER'] } for role_name in roles_permissions_map: role = Role.query.filter_by(name=role_name).first() if role is None: role = Role(name=role_name) db.session.add(role) role.permissions = [] for permission_name in roles_permissions_map[role_name]: permission = Permission.query.filter_by( name=permission_name).first() if permission is None: permission = Permission(name=permission_name) db.session.add(permission) role.permissions.append(permission) db.session.commit()
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, index=True) email = db.Column(db.String(254), unique=True, index=True) password_hash = db.Column(db.String(128)) name = db.Column(db.String(30)) location = db.Column(db.String(64)) about_me = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.datetime.utcnow) last_seen = db.Column(db.DateTime(), default=datetime.datetime.utcnow) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) role = db.relationship('Role', back_populates='users') depart_id = db.Column(db.Integer, db.ForeignKey('departs.id')) depart = db.relationship('Depart', back_populates='users') photos = db.relationship('Photo', back_populates='author', cascade='all') advice = db.relationship('Advice', back_populates='author', cascade='all') comments = db.relationship('Comment', back_populates='author', cascade='all') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.set_role_by_role_name() self.set_depart_by_depart_name() def __repr__(self): return f'<User {self.username}>' # @property # def password(self): # raise AttributeError('密码不具有可读属性') def set_password(self, password): self.password_hash = generate_password_hash(password) def set_role_by_role_name(self, role_name='User'): self.role = Role.query.filter_by(name=role_name).first() db.session.commit() def set_depart_by_depart_name(self, depart_name='本科生第一支部'): self.depart = Depart.query.filter_by(name=depart_name).first() db.session.commit() def validate_password(self, password): return check_password_hash(self.password_hash, password) @property def is_admin(self): return self.role.name == 'Administrator' def can(self, permission_name): permission = Permission.query.filter_by(name=permission_name).first() return permission is not None and self.role is not None and permission in self.role.permissions def ping(self): self.last_seen = datetime.datetime.utcnow() db.session.commit()
class Photo(db.Model): __tablename__ = 'photos' id = db.Column(db.Integer, primary_key=True) description = db.Column(db.Text) filesize = db.Column(db.String(64)) filename = db.Column(db.String(512)) filename_m = db.Column(db.String(512)) filename_s = db.Column(db.String(512)) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) public_status = db.Column(db.Integer, default=0) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) author = db.relationship('User', back_populates='photos') task_id = db.Column(db.Integer, db.ForeignKey('tasks.id')) task = db.relationship('Task', back_populates='photos') def __repr__(self): return f'<Photo {self.id}>' def set_task_by_id(self, task_id=1): self.task = Task.query.filter_by(id=task_id).first() db.session.commit() def set_task_by_name_third(self, name_third): self.task = Task.query.filter_by(name_third=name_third).first() db.session.commit()
class Advice(db.Model): __tablename__ = 'advice' id = db.Column(db.Integer, primary_key=True) depart_id = db.Column(db.Integer) passed_count = db.Column(db.Integer) url = db.Column(db.Text) filter = db.Column(db.Text) status = db.Column(db.Integer, default=0) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) author = db.relationship('User', back_populates='advice') comments = db.relationship('Comment', back_populates='advice', cascade='all')
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, default=datetime.utcnow, index=True) advice_id = db.Column(db.Integer, db.ForeignKey('advice.id')) advice = db.relationship('Advice', back_populates='comments') author_id = db.Column(db.Integer, db.ForeignKey('users.id')) author = db.relationship('User', back_populates='comments') replied_id = db.Column(db.Integer, db.ForeignKey('comments.id')) replies = db.relationship('Comment', back_populates='replied', cascade='all') replied = db.relationship('Comment', back_populates='replies', remote_side=[id])
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='role') permissions = db.relationship('Permission', secondary=roles_permissions, back_populates='roles') @staticmethod def init_roles(): roles_permissions_map = { 'User': ['UPLOAD'], 'Inspector': ['UPLOAD', 'RIGISTER', 'WATCH_OTHERS', 'SET_PUBLIC', 'COMMENT'], 'Moderator': ['UPLOAD', 'RIGISTER', 'WATCH_OTHERS', 'ADVICE', 'COMMENT'], 'Administrator': [ 'UPLOAD', 'RIGISTER', 'WATCH_OTHERS', 'SET_PUBLIC', 'ADVICE', 'COMMENT', 'ADMINISTER' ] } # 权限分配: # 用户:上传; # 审核者一:上传、注册、浏览他人、公开; # 审核者二:上传、注册、浏览他人、修改; # 管理员:全部。 for role_name in roles_permissions_map: role = Role.query.filter_by(name=role_name).first() if role is None: role = Role(name=role_name) db.session.add(role) role.permissions = [] for permission_name in roles_permissions_map[role_name]: permission = Permission.query.filter_by( name=permission_name).first() if permission is None: permission = Permission(name=permission_name) db.session.add(permission) role.permissions.append(permission) db.session.commit() def __repr__(self): return f'<Role {self.name}>'
class Permission(db.Model): __tablename__ = 'permissions' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) roles = db.relationship('Role', secondary=roles_permissions, back_populates='permissions') def __repr__(self): return f'<Permission {self.name}>'
class Depart(db.Model): __tablename__ = 'departs' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) users = db.relationship('User', back_populates='depart') @staticmethod def init_departs(): depart_list = [ '本科生第一支部', '本科生第二支部', '研究生第一支部', '研究生第二支部', '研究生第三支部', '教工第一支部', '教工第二支部', '计算机系支部' ] # 八个部门列表硬编码于此处,初始化时自动写入数据库 for depart in depart_list: depart = Depart(name=depart) db.session.add(depart) db.session.commit() def __repr__(self): return f'<Depart {self.name}>'
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, index=True) email = db.Column(db.String(254), unique=True, index=True) password_hash = db.Column(db.String(128)) name = db.Column(db.String(30)) member_since = db.Column(db.DateTime, default=datetime.utcnow) role_id = db.Column(db.Integer, db.ForeignKey('role.id')) role = db.relationship('Role', back_populates='users') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.set_role() def set_password(self, password): self.password_hash = generate_password_hash(password) def set_role(self): if self.role is None: if self.email == current_app.config['ADMIN_EMAIL']: self.role = Role.query.filter_by(name='Administrator').first() else: self.role = Role.query.filter_by(name='User').first() db.session.commit() def validate_password(self, password): return check_password_hash(self.password_hash, password) @property def is_admin(self): return self.role.name == 'Administrator' def can(self, permission_name): permission = Permission.query.filter_by(name=permission_name).first() return permission is not None and self.role is not None and permission in self.role.permissions
class Task(db.Model): __tablename__ = 'tasks' id = db.Column(db.Integer, primary_key=True) name_first = db.Column(db.String(30)) name_second = db.Column(db.String(30)) name_third = db.Column(db.String(512), unique=True, index=True) kind = db.Column(db.String(30)) photos = db.relationship('Photo', back_populates='task') @staticmethod def init_tasks(): count = 1 for task_name_first in task_dict.keys(): for task_name_second in task_dict[task_name_first]: for task_name_third in task_dict[task_name_first][task_name_second]: task = Task(id=count, name_first=task_name_first, name_second=task_name_second, name_third=task_name_third['details'], kind=task_name_third['kind']) count += 1 db.session.add(task) db.session.commit() def __repr__(self): return f'<Task {self.id}>'
class Permission(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30), unique=True) roles = db.relationship('Role', secondary=roles_permissions, back_populates='permissions')