增加数据字典

This commit is contained in:
mkg
2021-04-16 14:08:13 +08:00
parent a709095dc2
commit 2f0df0a700
24 changed files with 1284 additions and 431 deletions
+24
View File
@@ -93,3 +93,27 @@ class Photo(db.Model):
mime = db.Column(db.CHAR(50), nullable=False)
size = db.Column(db.CHAR(30), nullable=False)
create_time = db.Column(db.DateTime, default=datetime.datetime.now)
class DictType(db.Model):
__tablename__ = 'admin_dict_type'
id = db.Column(db.Integer, primary_key=True)
type_name = db.Column(db.String(255), comment='字典类型名称')
type_code = db.Column(db.String(255), comment='字典类型标识')
description = db.Column(db.String(255), comment='字典类型描述')
enable = db.Column(db.Integer, comment='是否开启')
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
class DictData(db.Model):
__tablename__ = 'admin_dict_data'
id = db.Column(db.Integer, primary_key=True)
data_label = db.Column(db.String(255), comment='字典类型名称')
data_value = db.Column(db.String(255), comment='字典类型标识')
type_code = db.Column(db.String(255), comment='字典类型描述')
is_default = db.Column(db.Integer, comment='是否默认')
enable = db.Column(db.Integer, comment='是否开启')
remark = db.Column(db.String(255), comment='备注')
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
+176
View File
@@ -0,0 +1,176 @@
# 字典管理
# 查询字典类型
from flask_marshmallow import Marshmallow
from marshmallow import fields
from applications.models import db
from applications.models.admin import DictType, DictData
ma = Marshmallow()
# 通过type_code获取字典dict
# 例:get_dict('user_sex')
# [{'key': '男', 'value': 'boy'}, {'key': '女', 'value': 'girl'}]
def get_dict(typecode: str):
Dict_list = []
if DictType.query.filter_by(type_code=typecode, enable=1).first():
Dicts = DictData.query.filter_by(type_code=typecode, enable=1).all()
for d in Dicts:
Dict_Dict = {"key": d.data_label, "value": d.data_value}
Dict_list.append(Dict_Dict)
else:
return None
return Dict_list
class DictTypeSchema(ma.Schema): # 序列化类
id = fields.Str(attribute="id")
typeName = fields.Str(attribute="type_name")
typeCode = fields.Str(attribute="type_code")
description = fields.Str(attribute="description")
createTime = fields.Str(attribute="create_time")
updateName = fields.Str(attribute="update_time")
remark = fields.Str()
enable = fields.Str()
class DictDataSchema(ma.Schema): # 序列化类
dataId = fields.Str(attribute="id")
dataLabel = fields.Str(attribute="data_label")
dataValue = fields.Str(attribute="data_value")
remark = fields.Str()
enable = fields.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
def get_dict_data(page, limit, type_code):
dict_all = DictData.query.filter_by(type_code=type_code).paginate(page=page,
per_page=limit,
error_out=False)
count = DictType.query.count()
dict_schema = DictDataSchema(many=True)
dict_dict = dict_schema.dump(dict_all.items)
return dict_dict, count
# 增加dicttype
def save_dict_type(req_json):
description = req_json.get("description")
enable = req_json.get("enable")
type_code = req_json.get("typeCode")
type_name = req_json.get("typeName")
d = DictType(type_name=type_name, type_code=type_code, enable=enable, description=description)
db.session.add(d)
db.session.commit()
return d.id
# 编辑字典类型
def update_dict_type(req_json):
id = req_json.get("id")
description = req_json.get("description")
enable = req_json.get("enable")
type_code = req_json.get("typeCode")
type_name = req_json.get("typeName")
DictType.query.filter_by(id=id).update({
"description": description,
"enable": enable,
"type_code": type_code,
"type_name": type_name
})
db.session.commit()
return
def enable_dict_type_status(id):
enable = 1
res = DictType.query.filter_by(id=id).update({"enable": enable})
if res:
db.session.commit()
return True
return False
def disable_dict_type_status(id):
enable = 0
res = DictType.query.filter_by(id=id).update({"enable": enable})
if res:
db.session.commit()
return True
return False
# 删除字典类型
def delete_type_by_id(id):
type_code = DictType.query.filter_by(id=id).first().type_code
DictData.query.filter_by(type_code=type_code).delete()
res = DictType.query.filter_by(id=id).delete()
db.session.commit()
return res
# 增加dictdata
def save_dict_data(req_json):
data_label = req_json.get("dataLabel")
data_value = req_json.get("dataValue")
enable = req_json.get("enable")
remark = req_json.get("remark")
type_code = req_json.get("typeCode")
d = DictData(data_label=data_label, data_value=data_value, enable=enable, remark=remark, type_code=type_code)
db.session.add(d)
db.session.commit()
return d.id
# 编辑字典数据
def update_dict_data(req_json):
id = req_json.get("dataId")
DictData.query.filter_by(id=id).update({
"data_label": req_json.get("dataLabel"),
"data_value": req_json.get("dataValue"),
"enable": req_json.get("enable"),
"remark": req_json.get("remark"),
"type_code": req_json.get("typeCode")
})
db.session.commit()
return
def enable_dict_data_status(id):
enable = 1
res = DictData.query.filter_by(id=id).update({"enable": enable})
if res:
db.session.commit()
return True
return False
def disable_dict_data_status(id):
enable = 0
res = DictData.query.filter_by(id=id).update({"enable": enable})
if res:
db.session.commit()
return True
return False
# 删除dictdata
def delete_data_by_id(id):
res = DictData.query.filter_by(id=id).delete()
db.session.commit()
return res
+3
View File
@@ -5,6 +5,7 @@ from applications.views.admin.monitor import admin_Monitor
from applications.views.admin.admin_log import admin_log
from applications.views.admin.power import admin_power
from applications.views.admin.role import admin_role
from applications.views.admin.dict import admin_dict
"""
初始化蓝图
@@ -20,3 +21,5 @@ def init_adminViews(app):
app.register_blueprint(admin_log)
app.register_blueprint(admin_power)
app.register_blueprint(admin_role)
app.register_blueprint(admin_dict)
+192
View File
@@ -0,0 +1,192 @@
from flask import Blueprint, render_template, request, jsonify
from applications.models.admin import DictType, DictData
from applications.service.admin.dict import get_dict_type, get_dict_data, save_dict_type, save_dict_data, \
delete_type_by_id, delete_data_by_id, update_dict_type, enable_dict_type_status, disable_dict_type_status, \
enable_dict_data_status, disable_dict_data_status, update_dict_data
from applications.service.route_auth import authorize_and_log
admin_dict = Blueprint('adminDict', __name__, url_prefix='/admin/dict')
# 数据字典
@admin_dict.route('/')
@authorize_and_log("admin:dict:main")
def main():
return render_template('admin/dict/main.html')
@admin_dict.route('/dictType/data')
@authorize_and_log("admin:dict:main")
def dictType_data():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
type_name = request.args.get('typeName', type=str)
dict_data, count = get_dict_type(page=page, limit=limit, type_name=type_name)
res = {
"data": dict_data,
"count": count,
"code": 0,
}
return jsonify(res)
@admin_dict.route('/dictType/add')
@authorize_and_log("admin:dict:add")
def dictType_add():
return render_template('admin/dict/add.html')
@admin_dict.route('/dictType/save', methods=['POST'])
@authorize_and_log("admin:power:add")
def dictType_save():
req_json = request.json
res = save_dict_type(req_json=req_json)
if res == None:
return jsonify(success=False, msg="增加失败")
return jsonify(success=True, msg="增加成功")
# 编辑字典类型
@admin_dict.route('/dictType/edit', methods=['GET', 'POST'])
@authorize_and_log("admin:dict:edit")
def dictType_edit():
id = request.args.get('dictTypeId', type=str)
dict_type = DictType.query.filter_by(id=id).first()
return render_template('admin/dict/edit.html', dict_type=dict_type)
# 编辑字典类型
@admin_dict.route('/dictType/update', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictType_update():
req_json = request.json
update_dict_type(req_json)
return jsonify(success=True, msg="更新成功")
# 启用字典
@admin_dict.route('/dictType/enable', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictType_enable():
id = request.json.get('id')
print(id)
if id:
res = enable_dict_type_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
# 禁用字典
@admin_dict.route('/dictType/disable', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictType_disenable():
id = request.json.get('id')
print(id)
if id:
res = disable_dict_type_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
# 删除字典类型
@admin_dict.route('/dictType/remove/<int:id>', methods=['DELETE'])
@authorize_and_log("admin:dict:remove")
def dictType_delete(id):
res = delete_type_by_id(id)
if not res:
return jsonify(msg="删除失败", success=False)
return jsonify(msg="删除成功", success=True)
@admin_dict.route('/dictData/data')
@authorize_and_log("admin:dict:main")
def dictCode_data():
page = request.args.get('page', type=int)
limit = request.args.get('limit', type=int)
type_code = request.args.get('typeCode', type=str)
dict_data, count = get_dict_data(page=page, limit=limit, type_code=type_code)
res = {
"data": dict_data,
"count": count,
"code": 0,
}
return jsonify(res)
# 增加字典数据
@admin_dict.route('/dictData/add')
# @authorize_and_log("admin:power:add")
def dictData_add():
type_code = request.args.get('typeCode', type=str)
return render_template('admin/dict/data/add.html', type_code=type_code)
# 增加字典数据
@admin_dict.route('/dictData/save', methods=['POST'])
# @authorize_and_log("admin:power:main")
def dictData_save():
req_json = request.json
res = save_dict_data(req_json=req_json)
if res == None:
return jsonify(success=False, msg="增加失败")
return jsonify(success=True, msg="增加成功")
# 编辑字典数据
@admin_dict.route('/dictData/edit', methods=['GET', 'POST'])
@authorize_and_log("admin:dict:edit")
def dictData_edit():
id = request.args.get('dataId', type=str)
dict_data = DictData.query.filter_by(id=id).first()
return render_template('admin/dict/data/edit.html', dict_data=dict_data)
# 编辑字典数据
@admin_dict.route('/dictData/update', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictData_update():
req_json = request.json
update_dict_data(req_json)
return jsonify(success=True, msg="更新成功")
# 启用字典数据
@admin_dict.route('/dictData/enable', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictData_enable():
id = request.json.get('dataId')
print(id)
if id:
res = enable_dict_data_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="启动成功", success=True)
return jsonify(msg="数据错误", success=False)
# 禁用字典数据
@admin_dict.route('/dictData/disable', methods=['PUT'])
@authorize_and_log("admin:dict:edit")
def dictData_disenable():
id = request.json.get('dataId')
if id:
res = disable_dict_data_status(id)
if not res:
return jsonify(msg="出错啦", success=False)
return jsonify(msg="禁用成功", success=True)
return jsonify(msg="数据错误", success=False)
# 删除字典类型
@admin_dict.route('dictData/remove/<int:id>', methods=['DELETE'])
@authorize_and_log("admin:dict:remove")
def dictData_delete(id):
res = delete_data_by_id(id)
if not res:
return jsonify(msg="删除失败", success=False)
return jsonify(msg="删除成功", success=True)