删除curd层
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
from marshmallow import INCLUDE
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.utils.http import success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import check_data
|
||||
from applications.common.utils import validate
|
||||
from applications.extensions import db
|
||||
from applications.models import Dept, User
|
||||
from applications.schemas import DeptSchema
|
||||
from applications.common.admin import dept_curd as dept_curd
|
||||
|
||||
dept_bp = Blueprint('dept', __name__, url_prefix='/dept')
|
||||
|
||||
@@ -23,7 +25,8 @@ def main():
|
||||
@dept_bp.get('/data')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def data():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
power_data = curd.model_to_dicts(schema=DeptSchema, data=dept)
|
||||
res = {
|
||||
"data": power_data
|
||||
}
|
||||
@@ -39,7 +42,8 @@ def add():
|
||||
@dept_bp.get('/tree')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def tree():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
power_data = curd.model_to_dicts(schema=DeptSchema, data=dept)
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
@@ -52,8 +56,27 @@ def tree():
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
check_data(DeptSchema(unknown=INCLUDE), req)
|
||||
dept_curd.save_dept(req)
|
||||
validate.check_data(DeptSchema(unknown=INCLUDE), req)
|
||||
address = validate.xss_escape(req.get("address"))
|
||||
deptName = validate.xss_escape(req.get("deptName"))
|
||||
email = validate.xss_escape(req.get("email"))
|
||||
leader = validate.xss_escape(req.get("leader"))
|
||||
parentId = validate.xss_escape(req.get("parentId"))
|
||||
phone = validate.xss_escape(req.get("phone"))
|
||||
sort = validate.xss_escape(req.get("sort"))
|
||||
status = validate.xss_escape(req.get("status"))
|
||||
dept = Dept(
|
||||
parent_id=parentId,
|
||||
dept_name=deptName,
|
||||
sort=sort,
|
||||
leader=leader,
|
||||
phone=phone,
|
||||
email=email,
|
||||
status=status,
|
||||
address=address
|
||||
)
|
||||
r = db.session.add(dept)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@@ -61,7 +84,7 @@ def save():
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def edit():
|
||||
_id = request.args.get("deptId")
|
||||
dept = dept_curd.get_dept_by_id(_id)
|
||||
dept = curd.get_one_by_id(model=Dept,id=_id)
|
||||
return render_template('admin/dept/edit.html', dept=dept)
|
||||
|
||||
|
||||
@@ -71,10 +94,12 @@ def edit():
|
||||
def enable():
|
||||
_id = request.json.get('deptId')
|
||||
if id:
|
||||
res = dept_curd.enable_status(_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启用成功")
|
||||
enable = 1
|
||||
d = Dept.query.filter_by(id=id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="出错啦")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@@ -82,30 +107,47 @@ def enable():
|
||||
@dept_bp.put('/disable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('deptId')
|
||||
id = request.json.get('deptId')
|
||||
if id:
|
||||
res = dept_curd.disable_status(_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
enable = 0
|
||||
d = Dept.query.filter_by(id=id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="出错啦")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@dept_bp.put('/update')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def update():
|
||||
req = request.json
|
||||
check_data(DeptSchema(unknown=INCLUDE), req)
|
||||
res = dept_curd.update_dept(req)
|
||||
if not res:
|
||||
json = request.json
|
||||
validate.check_data(DeptSchema(unknown=INCLUDE), json)
|
||||
id = json.get("deptId"),
|
||||
data = {
|
||||
"dept_name": validate.xss_escape(json.get("deptName")),
|
||||
"sort": validate.xss_escape(json.get("sort")),
|
||||
"leader": validate.xss_escape(json.get("leader")),
|
||||
"phone": validate.xss_escape(json.get("phone")),
|
||||
"email": validate.xss_escape(json.get("email")),
|
||||
"status": validate.xss_escape(json.get("status")),
|
||||
"address": validate.xss_escape(json.get("address"))
|
||||
}
|
||||
d = Dept.query.filter_by(id=id).update(data)
|
||||
if not d:
|
||||
return fail_api(msg="更新失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
@dept_bp.delete('/remove/<int:_id>')
|
||||
@authorize("admin:dept:remove", log=True)
|
||||
def remove(_id):
|
||||
res = dept_curd.remove_dept(_id)
|
||||
d = Dept.query.filter_by(id=_id).delete()
|
||||
if not d:
|
||||
return fail_api(msg="删除失败")
|
||||
res = User.query.filter_by(dept_id=_id).update({"dept_id": None})
|
||||
db.session.commit()
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user