class PermissionModel(db.Model): __tablename__ = 'permissions' id = db.Column(db.Integer, primary_key=True, nullable=False) #: 名称 name = db.Column(db.String(50), unique=True, nullable=False) #: 标识 tag = db.Column(db.String(40), unique=True, nullable=True) group = db.relationship('GroupPermissionModel', backref='permission') def __repr__(self): return '<Perm %s>' % self.tag
class GroupModel(db.Model): __tablename__ = 'groups' id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) #: 组名称 name = db.Column(db.String(80), unique=True, nullable=False) users = db.relationship('UserGroupModel', backref='group') permissions = db.relationship('GroupPermissionModel', backref='group') def __repr__(self): return '<Group %r>' % self.id def get_permissions(self): permissions = set() for assoc in self.permissions: p = assoc.permission permissions.add(p) return permissions
class UserModel(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, nullable=False, autoincrement=True) #: 用户名 username = db.Column(db.String(30), unique=True, nullable=False) #: 名字 first_name = db.Column(db.String(30), nullable=False) #: 姓氏 last_name = db.Column(db.String(30), nullable=False) #: 邮箱 email = db.Column(db.String(75), unique=True, nullable=False) #: 密码 password = db.Column(db.String(128), nullable=False) #: 是否为职员(0表示否,1表示是) is_staff = db.Column(db.Boolean, nullable=False) #: active is_active = db.Column(db.Boolean, nullable=False) #: 是否为超级管理员 is_superuser = db.Column(db.Boolean, nullable=False) #: 上次登陆时间 last_login = db.Column(db.DateTime, nullable=False) #: 注册日期 date_joined = db.Column(db.DateTime, nullable=False) groups = db.relationship('UserGroupModel', backref='user') def __repr__(self): return '<User %r>' % self.username def get_groups(self): groups = set() for assoc in self.groups: g = assoc.group groups.add(g) return groups def get_permissions(self): permissions = set() groups = self.get_groups() for group in groups: for assoc in group.permissions: p = assoc.permission permissions.add(p) return permissions
class UserGroupModel(db.Model): __tablename__ = 'user_groups' id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) group_id = db.Column(db.Integer, db.ForeignKey('groups.id'))
class GroupPermissionModel(db.Model): __tablename__ = 'group_permissions' id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_id = db.Column(db.Integer, db.ForeignKey('groups.id')) permission_id = db.Column(db.Integer, db.ForeignKey('permissions.id'))