统一table,成功,失败返回响应,封装模型序列化函数
This commit is contained in:
@@ -9,6 +9,9 @@ from applications.models.admin_dict import DictType, DictData, DictTypeSchema, D
|
||||
# 通过type_code获取字典dict
|
||||
# 例:get_dict('user_sex')
|
||||
# [{'key': '男', 'value': 'boy'}, {'key': '女', 'value': 'girl'}]
|
||||
from applications.service.common.curd import model_to_dicts
|
||||
|
||||
|
||||
def get_dict(typecode: str):
|
||||
Dict_list = []
|
||||
if DictType.query.filter_by(type_code=typecode, enable=1).first():
|
||||
@@ -24,15 +27,13 @@ def get_dict(typecode: str):
|
||||
def get_dict_type(page, limit, type_name):
|
||||
dict_all = DictType.query
|
||||
if type_name:
|
||||
print('收到了')
|
||||
dict_all = dict_all.filter(DictType.type_name.like('%' + type_name + '%'))
|
||||
dict_all = dict_all.paginate(page=page,
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
count = DictType.query.count()
|
||||
dict_schema = DictTypeSchema(many=True)
|
||||
dict_dict = dict_schema.dump(dict_all.items)
|
||||
return dict_dict, count
|
||||
data = model_to_dicts(Schema=DictTypeSchema,model=dict_all.items)
|
||||
return data, count
|
||||
|
||||
|
||||
def get_dict_data(page, limit, type_code):
|
||||
@@ -40,8 +41,7 @@ def get_dict_data(page, limit, type_code):
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
count = DictType.query.count()
|
||||
dict_schema = DictDataSchema(many=True)
|
||||
dict_dict = dict_schema.dump(dict_all.items)
|
||||
dict_dict = model_to_dicts(Schema=DictDataSchema,model=dict_all.items)
|
||||
return dict_dict, count
|
||||
|
||||
|
||||
|
||||
@@ -3,15 +3,15 @@ from flask import current_app
|
||||
from sqlalchemy import desc
|
||||
from applications.models import db
|
||||
from applications.models.admin_photo import Photo, PhotoSchema
|
||||
from applications.service.common.curd import model_to_dicts
|
||||
from applications.service.upload import photos
|
||||
|
||||
|
||||
def get_photo(page, limit):
|
||||
photo = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
|
||||
count = Photo.query.count()
|
||||
role_schema = PhotoSchema(many=True)
|
||||
output = role_schema.dump(photo.items)
|
||||
return output, count
|
||||
data = model_to_dicts(Schema=PhotoSchema,model=photo.items)
|
||||
return data, count
|
||||
|
||||
|
||||
def upload_one(photo, mime):
|
||||
|
||||
@@ -4,8 +4,10 @@ from applications.models.admin_role import Role, RoleSchema
|
||||
from applications.models.admin_power import Power, PowerSchema2
|
||||
from applications.models.admin_user import User
|
||||
|
||||
|
||||
# 获取角色对象
|
||||
from applications.service.common.curd import model_to_dicts
|
||||
|
||||
|
||||
def get_role_data(page, limit, filters):
|
||||
role = Role.query.filter(and_(*[getattr(Role, k).like(v) for k, v in filters.items()])).paginate(page=page,
|
||||
per_page=limit,
|
||||
@@ -17,9 +19,8 @@ def get_role_data(page, limit, filters):
|
||||
# 获取角色dict
|
||||
def get_role_data_dict(page, limit, filters):
|
||||
role, count = get_role_data(page, limit, filters)
|
||||
role_schema = RoleSchema(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = role_schema.dump(role.items) # 生成可序列化对象
|
||||
return output, count
|
||||
data = model_to_dicts(Schema=RoleSchema, model=role.items)
|
||||
return data, count
|
||||
|
||||
|
||||
# 增加角色
|
||||
|
||||
@@ -8,6 +8,9 @@ from applications.models.admin_log import AdminLog
|
||||
|
||||
|
||||
# 获取用户的sqlalchemy对象分页器
|
||||
from applications.service.common.curd import model_to_dicts
|
||||
|
||||
|
||||
def get_user_data(page, limit, filters):
|
||||
user = User.query.filter(and_(*[getattr(User, k).like(v) for k, v in filters.items()])).paginate(page=page,
|
||||
per_page=limit,
|
||||
@@ -19,9 +22,8 @@ def get_user_data(page, limit, filters):
|
||||
# 获取用户的dict数据分页器
|
||||
def get_user_data_dict(page, limit, filters):
|
||||
user, count = get_user_data(page, limit, filters)
|
||||
user_schema = UserSchema(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = user_schema.dump(user.items) # 生成可序列化对象
|
||||
return output, count
|
||||
data = model_to_dicts(Schema=UserSchema,model=user.items)
|
||||
return data, count
|
||||
|
||||
|
||||
# 通过名称获取用户
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
def model_to_dicts(Schema, model):
|
||||
# 如果是分页器返回,需要传入model.items
|
||||
common_schema = Schema(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = common_schema.dump(model) # 生成可序列化对象
|
||||
return output
|
||||
@@ -0,0 +1,42 @@
|
||||
from flask import jsonify
|
||||
|
||||
'''
|
||||
成功响应
|
||||
|
||||
默认值”成功“
|
||||
|
||||
'''
|
||||
|
||||
|
||||
def success_api(msg: str = "成功"):
|
||||
return jsonify(success=True, msg=msg)
|
||||
|
||||
|
||||
'''
|
||||
|
||||
失败响应
|
||||
默认值“失败”
|
||||
|
||||
'''
|
||||
|
||||
|
||||
def fail_api(msg: str = "失败"):
|
||||
return jsonify(success=False, msg=msg)
|
||||
|
||||
'''
|
||||
|
||||
动态表格渲染响应
|
||||
|
||||
'''
|
||||
|
||||
|
||||
def table_api(msg: str = "", count=0, data=None, limit=10):
|
||||
res = {
|
||||
'msg': msg,
|
||||
'code': 0,
|
||||
'data': data,
|
||||
'count': count,
|
||||
'limit': limit
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
Reference in New Issue
Block a user