def create_app(env='DEV'): """创建app""" app = Flask('OPERA_API', static_folder=DevConfig.STATIC_URL) if env == 'DEV': CORS(app) # 跨域支持 app.config.from_object(DevConfig) elif env == 'PROD': app.config.from_object(ProdConfig) elif env == 'TEST': app.config.from_object(TestConfig) jwt = JWTManager(app) BindJwt.init(jwt) # 初始化jwt-auth db.init_app(app) # 初始化MYSQL数据库 mongo.init_app(app) # 初始化MONGO数据库 api.init_app(app) # 绑定restful api return app
class BatchImage(db.Model): """批次-图像 (批次中包含的图像进行关联)""" __tablename__ = 'batch_image' ID = db.Column(db.Integer, primary_key=True, nullable=False) ImageID = db.Column(db.String(45), nullable=False) BatchID = db.Column(db.String(45), nullable=False) BatchType = db.Column(db.String(10), default='all') def __repr__(self): return '<BatchImage `{}`>'.format(self.BatchID)
def create_app(): """创建App""" app = Flask(__name__) app.config.from_object(DevConfig) # 初始化配置项 jwt = JWTManager(app) @jwt.expired_token_loader def my_expired_token_callback(): return jsonify({ 'status': 401, 'sub_status': 42, 'msg': 'The token has expired' }), 401 @jwt.user_claims_loader def add_claims_to_access_token(user): return user.usertype @jwt.user_identity_loader def user_identity_lookup(user): data = [user.userid, user.username] return data db.init_app(app) # 初始化MYSQL数据库 mongo.init_app(app) # 初始化MONGODB数据库 CORS(app) # 跨域支持 api_bp = Blueprint('api', __name__) api = swagger.docs(Api(api_bp), apiVersion='0.1', resourcePath='/', description='EMDP_API', api_spec_url='/swagger') # swagger支持 bind_resources(api) # restful 的逻辑 app.register_blueprint(api_bp, url_prefix='/api') # 蓝图注册 bind_views(app) return app
class Batch(db.Model): """批次模型""" __tablename__ = 'batch' ID = db.Column(db.Integer, primary_key=True, nullable=False) Name = db.Column(db.String(45)) BatchID = db.Column(db.String(100), nullable=False) BatchType = db.Column(db.String(10), nullable=False) Description = db.Column(db.String(300)) CreateTime = db.Column(db.DateTime, default=datetime.now) ModifyTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) TenementID = db.Column(db.Integer, db.ForeignKey('tenement.ID')) tenements = db.relationship('Tenement', backref='batch', lazy='select') def __repr__(self): return '<Batch `{}`>'.format(self.BatchID)
class Tenement(db.Model): """租户模型""" __tablename__ = 'tenement' ID = db.Column(db.Integer, primary_key=True, nullable=False) Name = db.Column(db.String(100), nullable=False) CompanyID = db.Column(db.String(100)) # 企业编码 Industry = db.Column(db.String(100)) # 所属行业 Remark = db.Column(db.String(300)) CreateTime = db.Column(db.DateTime, default=datetime.now) ModifyTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) def __repr__(self): return '<Tenement: {}>'.format(self.Name)
class TbUser(db.Model): """租户子用户模型""" __bind_key__ = 'tenement' # -连接到指定的数据库 __tablename__ = 'TbUser' ID = db.Column(db.Integer, primary_key=True, nullable=False) AccountNo = db.Column(db.String(20), nullable=False) Avatar = db.Column(db.String(50)) BirthDay = db.Column(db.DateTime) CreateTime = db.Column(db.DateTime, nullable=False) ModifyTime = db.Column(db.DateTime, nullable=False) Education = db.Column(db.Integer) IDCard = db.Column(db.String(18)) IsDelete = db.Column(db.Integer) JobNo = db.Column(db.String(50)) Mobile = db.Column(db.String(50)) Name = db.Column(db.String(100), nullable=False) Password = db.Column(db.String(200), nullable=False) Sex = db.Column(db.Integer) Theme = db.Column(db.Integer) Timestamp = db.Column(db.String(50)) Type = db.Column(db.Integer) FaceID = db.Column(db.String(30)) WeChatOpenID = db.Column(db.String(30)) # 外键 Grade = db.Column(db.Integer) OrgID = db.Column(db.Integer) Post = db.Column(db.Integer) def __repr__(self): return '<TBUser: {}>'.format(self.Name)
class User(db.Model): """用户""" __tablename__ = 'user' ID = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) UserName = db.Column(db.String(32), unique=True, nullable=False) Password = db.Column(db.String(128), nullable=False) Name = db.Column(db.String(32)) UserType = db.Column(db.Enum(EnumUserType)) # Admin,Normal,Visitor CreatorId = db.Column(db.String(128)) AvatarUrl = db.Column(db.String(128)) CreateTime = db.Column(db.DateTime, nullable=False, default=datetime.now) ModifyTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) Remark = db.Column(db.String(256)) def __repr__(self): return '<User `{}`>'.format(self.UserName) @property def password(self): """获取密码""" raise AttributeError("不可直接访问") @password.setter def password(self, passwd): """设置密码,之前先哈希加密处理""" self.Password = generate_password_hash(passwd) def check_password(self, passwd): """检测密码是否正确,和原有密码进行哈希对比""" return check_password_hash(self.Password, passwd) def generateAvatar(self, size=100): """生成头像 使用用户名进行加密,可以更换 :size 头像的大小;默认是 100*100 """ digest = md5(self.UserName.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(digest, size)
# -*- coding: utf-8 -*- """socketIO文件""" from flask import Flask, request from flask_socketio import SocketIO, emit, disconnect from flask_cors import CORS from threading import Lock from app.conf.config import DevConfig from app.db import MYSQL_DB as db, MONGO_DB as mongo from app.util.logger import create_logger app = Flask('FLASK_WS') app.config.from_object(DevConfig) db.init_app(app) mongo.init_app(app) CORS(app) # 支持跨域 socketio = SocketIO(app) ###################### WS_TEST ############################ @socketio.on('connect', namespace='/test') def connect(): """test 连接""" from ws.ws_test import test_connect test_connect() @socketio.on('disconnect', namespace='/test') def disconnect(): """test 断开连接"""
class Equipment(db.Model): """设备信息""" __tablename__ = 'equipment' # 表的结构 ID = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) EQP_ID = db.Column(db.String(45), nullable=False) Location = db.Column(db.String(45)) Longitude = db.Column(db.Numeric(10, 6)) Latitude = db.Column(db.Numeric(10, 6)) Type = db.Column(db.String(45)) IP = db.Column(db.String(45)) Status = db.Column(db.Integer, nullable=False, default=1) CreateTime = db.Column(db.DateTime, nullable=False, default=datetime.now) ModifyTime = db.Column(db.DateTime)
class User(db.Model): """用户""" __tablename__ = 'user' ID = db.Column(db.Integer, primary_key=True, unique=True, nullable=False) UserName = db.Column(db.String(32), unique=True, nullable=False) Password = db.Column(db.String(128), nullable=False) Name = db.Column(db.String(32)) UserType = db.Column(db.Enum(EnumUserType)) CreatorId = db.Column(db.String(128)) AvatarUrl = db.Column(db.String(128)) CreateTime = db.Column(db.DateTime, nullable=False, default=datetime.now) ModifyTime = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now) @property def password(self): """获取密码""" raise AttributeError("不可直接访问") @password.setter def password(self, passwd): """设置密码,之前先哈希加密处理""" self.Password = generate_password_hash(passwd) def check_password(self, passwd): """检测密码是否正确,和原有密码进行哈希对比""" return check_password_hash(self.Password, passwd)