class Role(db.Model, UserMixin): __tablename__ = 'SYROLE' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(100)) DESCRIPTION = db.Column(db.String(200)) ICONCLS = db.Column(db.String(100)) SEQ = db.Column(db.Integer) # 包含资源 resources = db.relationship('Resource', secondary=role_resource_table, backref=db.backref('roles', lazy='dynamic')) # 资源所属角色 def get_id(self): return str(self.ID) def to_dict(self): return dict([(k, getattr(self, k)) for k in self.__dict__.keys() if not k.startswith("_")]) def __repr__(self): return '<Role name:%r description:%r iconCls:%r seq:%r>\n' \ % (self.NAME, self.DESCRIPTION, self.ICONCLS, self.SEQ) def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME.strftime('%Y-%m-%d %H:%M:%S'), 'updatedatetime': self.UPDATEDATETIME.strftime('%Y-%m-%d %H:%M:%S'), 'name': self.NAME, 'description': self.DESCRIPTION, 'iconCls': self.ICONCLS, 'seq': self.SEQ, }
class ResourceType(db.Model, UserMixin): __tablename__ = 'SYRESOURCETYPE' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(100)) DESCRIPTION = db.Column(db.String(200)) resources = db.relationship('Resource', backref='type', lazy='dynamic') def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME, 'updatedatetime': self.UPDATEDATETIME, 'name': self.NAME, 'description': self.DESCRIPTION } def __repr__(self): return '<ResourceType %r>\n' % (self.NAME)
class OnLine(db.Model): __tablename__ = 'SYONLINE' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) LOGINNAME = db.Column(db.String(100)) IP = db.Column(db.String(100)) TYPE = db.Column(db.String(1)) def get_id(self): return str(self.ID) def __repr__(self): return '<Oneline %r>\n' % (self.LOGINNAME) def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME.strftime('%Y-%m-%d %H:%M:%S'), 'loginname': self.LOGINNAME, 'ip': self.IP, 'type': self.TYPE }
class User(db.Model, UserMixin): __tablename__ = 'SYUSER' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) LOGINNAME = db.Column(db.String(100), unique=True, index=True) PWD = db.Column(db.String(100)) NAME = db.Column(db.String(100)) SEX = db.Column(db.String(1)) AGE = db.Column(db.Integer) PHOTO = db.Column(db.String(200)) EMPLOYDATE = db.Column(db.DATETIME, default=datetime.now) organizations = db.relationship('Organization', secondary=user_organization_table, backref=db.backref('users', lazy='dynamic'), ) roles = db.relationship('Role', secondary=user_role_table, backref=db.backref('users', lazy='dynamic'), lazy="dynamic") def get_id(self): return str(self.ID) def have_permission(self, url): permissions = [] for role in self.roles: permissions.extend([resource for resource in role.resources]) if filter(lambda x: x.URL == url, permissions): return True permissions = [] for organization in self.organizations: permissions.extend( [resource for resource in organization.resources]) return filter(lambda x: x.NAME == url, permissions) def __repr__(self): return '<User %r-%r-%r>\n' % (self.ID, self.LOGINNAME, self.NAME) def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME.strftime('%Y-%m-%d %H:%M:%S'), 'updatedatetime': self.UPDATEDATETIME.strftime('%Y-%m-%d %H:%M:%S'), 'loginname': self.LOGINNAME, 'name': self.NAME, 'sex': self.SEX, 'age': self.AGE, 'photo': self.PHOTO, }
class Organization(db.Model, UserMixin): __tablename__ = 'SYORGANIZATION' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(200)) ADDRESS = db.Column(db.String(200)) CODE = db.Column(db.String(200)) ICONCLS = db.Column(db.String(100)) SEQ = db.Column(db.Integer) resources = db.relationship('Resource', secondary=organization_resource_table, backref=db.backref('organizations', lazy='dynamic')) SYORGANIZATION_ID = db.Column( db.String(36), db.ForeignKey('SYORGANIZATION.ID')) parent = db.relationship('Organization', remote_side=[ ID], backref='organization', uselist=False) def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME, 'updatedatetime': self.UPDATEDATETIME, 'name': self.NAME, 'address': self.ADDRESS, 'code': self.CODE, 'iconCls': self.ICONCLS, 'seq': self.SEQ, 'pid': self.get_pid(), } def get_pid(self): if self.parent: return self.parent.ID return '' def get_id(self): return str(self.ID) def __repr__(self): return '<Organization %r>\n' % (self.NAME)
from app.global_.exts import db from flask_login import UserMixin from datetime import datetime organization_resource_table = db.Table('SYORGANIZATION_SYRESOURCE', db.metadata, db.Column('SYRESOURCE_ID', db.String( 36), db.ForeignKey('SYRESOURCE.ID')), db.Column('SYORGANIZATION_ID', db.String(36), db.ForeignKey('SYORGANIZATION.ID'))) class Organization(db.Model, UserMixin): __tablename__ = 'SYORGANIZATION' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(200)) ADDRESS = db.Column(db.String(200)) CODE = db.Column(db.String(200)) ICONCLS = db.Column(db.String(100)) SEQ = db.Column(db.Integer) resources = db.relationship('Resource', secondary=organization_resource_table, backref=db.backref('organizations', lazy='dynamic')) SYORGANIZATION_ID = db.Column( db.String(36), db.ForeignKey('SYORGANIZATION.ID')) parent = db.relationship('Organization', remote_side=[ ID], backref='organization', uselist=False)
class Resource(db.Model, UserMixin): __tablename__ = 'SYRESOURCE' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(100)) URL = db.Column(db.String(200)) DESCRIPTION = db.Column(db.String(200)) ICONCLS = db.Column(db.String(100)) SEQ = db.Column(db.Integer) TARGET = db.Column(db.String(100)) SYRESOURCETYPE_ID = db.Column( db.String(36), db.ForeignKey('SYRESOURCETYPE.ID')) SYRESOURCE_ID = db.Column(db.String(36), db.ForeignKey('SYRESOURCE.ID')) parent = db.relationship('Resource', remote_side=[ ID], backref='resource', uselist=False) def get_id(self): return str(self.ID) def to_json(self): return { 'id': self.ID, 'createdatetime': self.CREATEDATETIME, 'updatedatetime': self.UPDATEDATETIME, 'name': self.NAME, 'url': self.URL, 'description': self.DESCRIPTION, 'iconCls': self.ICONCLS, 'seq': self.SEQ, 'target': self.TARGET, 'pid': self.get_pid(), 'syresourcetype': self.get_type_json() } def to_menu_json(self): return { 'id': self.ID, 'iconCls': self.ICONCLS, 'pid': self.get_pid(), 'state': 'open', 'checked': False, 'attributes': { 'target': self.TARGET, 'url': self.URL }, 'text': self.NAME } def get_pid(self): if self.parent: return self.parent.ID return '' def get_type_json(self): if self.type: return self.type.to_json() return {} def __repr__(self): return '<Resource name:%r url:%r>\n' % (self.NAME, self.URL)
from app.global_.exts import db from flask_login import UserMixin from datetime import datetime # 角色资源关联表 role_resource_table = db.Table('SYROLE_SYRESOURCE', db.metadata, db.Column('SYROLE_ID', db.String(36), db.ForeignKey('SYROLE.ID')), db.Column('SYRESOURCE_ID', db.String(36), db.ForeignKey('SYRESOURCE.ID'))) class Role(db.Model, UserMixin): __tablename__ = 'SYROLE' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) NAME = db.Column(db.String(100)) DESCRIPTION = db.Column(db.String(200)) ICONCLS = db.Column(db.String(100)) SEQ = db.Column(db.Integer) # 包含资源 resources = db.relationship('Resource', secondary=role_resource_table, backref=db.backref('roles', lazy='dynamic')) # 资源所属角色 def get_id(self): return str(self.ID) def to_dict(self): return dict([(k, getattr(self, k)) for k in self.__dict__.keys() if not k.startswith("_")])
from datetime import datetime from flask_login import UserMixin from app.global_.exts import loginmanager, db @loginmanager.user_loader def load_user(user_id): return User.query.filter(User.ID == user_id).first() user_organization_table = db.Table('SYUSER_SYORGANIZATION', db.Model.metadata, db.Column('SYUSER_ID', db.String( 36), db.ForeignKey('SYUSER.ID')), db.Column('SYORGANIZATION_ID', db.String(36), db.ForeignKey('SYORGANIZATION.ID'))) user_role_table = db.Table('SYUSER_SYROLE', db.Model.metadata, db.Column('SYUSER_ID', db.String( 36), db.ForeignKey('SYUSER.ID')), db.Column('SYROLE_ID', db.String(36), db.ForeignKey('SYROLE.ID'))) class User(db.Model, UserMixin): __tablename__ = 'SYUSER' ID = db.Column(db.String(36), primary_key=True) CREATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) UPDATEDATETIME = db.Column(db.DateTime, index=True, default=datetime.now) LOGINNAME = db.Column(db.String(100), unique=True, index=True) PWD = db.Column(db.String(100)) NAME = db.Column(db.String(100)) SEX = db.Column(db.String(1)) AGE = db.Column(db.Integer) PHOTO = db.Column(db.String(200)) EMPLOYDATE = db.Column(db.DATETIME, default=datetime.now)