flask-restful依赖替换完成
This commit is contained in:
@@ -1,18 +1,8 @@
|
||||
import os
|
||||
from flask import current_app
|
||||
from sqlalchemy import desc
|
||||
|
||||
from applications.common.utils.upload import photos
|
||||
from applications.extensions import db
|
||||
from applications.models import Photo, PhotoSchema
|
||||
from applications.common.curd import model_to_dicts
|
||||
|
||||
|
||||
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()
|
||||
data = model_to_dicts(Schema=PhotoSchema, model=photo.items)
|
||||
return data, count
|
||||
from applications.models import Photo
|
||||
|
||||
|
||||
def upload_one(photo, mime):
|
||||
@@ -1,11 +1,15 @@
|
||||
import os
|
||||
from flask import Blueprint, request, render_template, jsonify, current_app, make_response
|
||||
from flask_restful import Api, Resource
|
||||
from flask_restful import Api, Resource, marshal
|
||||
from sqlalchemy import desc
|
||||
|
||||
from applications.common.serialization import photo_fields
|
||||
from applications.common.utils.http import fail_api, success_api, table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.extensions import db
|
||||
from applications.models import Photo
|
||||
from applications.view.admin import file_curd
|
||||
|
||||
from ._utils import delete_photo_by_id, upload_one
|
||||
|
||||
file_bp = Blueprint('file', __name__, url_prefix='/file')
|
||||
file_api = Api(file_bp)
|
||||
@@ -24,8 +28,9 @@ def index():
|
||||
def table():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
data, count = file_curd.get_photo(page=page, limit=limit)
|
||||
return table_api(data=data, count=count)
|
||||
photo_paginate = Photo.query.order_by(desc(Photo.create_time)).paginate(page=page, per_page=limit, error_out=False)
|
||||
data = marshal(photo_paginate.items, photo_fields)
|
||||
return table_api(data=data, count=photo_paginate.total)
|
||||
|
||||
|
||||
@file_api.resource('/upload')
|
||||
@@ -39,7 +44,8 @@ class Upload(Resource):
|
||||
if 'file' in request.files:
|
||||
photo = request.files['file']
|
||||
mime = request.files['file'].content_type
|
||||
file_url = file_curd.upload_one(photo=photo, mime=mime)
|
||||
file_url = upload_one(photo=photo, mime=mime)
|
||||
|
||||
res = {
|
||||
"msg": "上传成功",
|
||||
"code": 0,
|
||||
@@ -53,7 +59,7 @@ class Upload(Resource):
|
||||
@authorize("admin:file:delete", log=True)
|
||||
def delete(self):
|
||||
_id = request.form.get('id')
|
||||
res = file_curd.delete_photo_by_id(_id)
|
||||
res = delete_photo_by_id(_id)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
from flask import Blueprint, request, render_template
|
||||
from sqlalchemy import desc
|
||||
from flask_restful import marshal
|
||||
from applications.common.serialization import log_fields
|
||||
|
||||
from applications.common.utils.http import table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.models import AdminLog, LogSchema
|
||||
from applications.common.curd import model_to_dicts
|
||||
from applications.models import AdminLog
|
||||
|
||||
admin_log = Blueprint('logs', __name__, url_prefix='/logs')
|
||||
|
||||
@@ -24,7 +25,7 @@ def login_log():
|
||||
url='/passport/login').order_by(
|
||||
desc(AdminLog.create_time)).paginate(
|
||||
page=page, per_page=limit, error_out=False)
|
||||
data = model_to_dicts(Schema=LogSchema, model=log_paginate.items)
|
||||
data = marshal(log_paginate.items, log_fields)
|
||||
|
||||
return table_api(data=data, count=log_paginate.total)
|
||||
|
||||
@@ -34,10 +35,9 @@ def login_log():
|
||||
def operate_log():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
log = AdminLog.query.filter(
|
||||
log_paginate = AdminLog.query.filter(
|
||||
AdminLog.url != '/passport/login').order_by(
|
||||
desc(AdminLog.create_time)).paginate(
|
||||
page=page, per_page=limit, error_out=False)
|
||||
count = AdminLog.query.filter(AdminLog.url != '/admin/login').count()
|
||||
data = model_to_dicts(Schema=LogSchema, model=log.items)
|
||||
return table_api(data=data, count=count)
|
||||
data = marshal(log_paginate.items, log_fields)
|
||||
return table_api(data=data, count=log_paginate.total)
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
from marshmallow import INCLUDE
|
||||
from flask import Blueprint, render_template, jsonify, make_response
|
||||
from flask_restful import marshal, Api, Resource, reqparse
|
||||
|
||||
from applications.common.serialization import dept_fields
|
||||
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.models import DeptSchema
|
||||
from applications.view.company.department import dept_curd as dept_curd
|
||||
from applications.extensions import db
|
||||
from applications.models import Dept, User
|
||||
|
||||
# from applications.view.company.department import dept_curd
|
||||
|
||||
dept_bp = Blueprint('dept', __name__, url_prefix='/dept')
|
||||
dept_api = Api(dept_bp)
|
||||
|
||||
|
||||
def register_dept_views(app):
|
||||
@@ -23,90 +26,118 @@ def main():
|
||||
@dept_bp.get('/data')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def data():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
dept_data = Dept.query.order_by(Dept.sort).all()
|
||||
|
||||
res = {
|
||||
"data": power_data
|
||||
"data": marshal(dept_data, dept_fields)
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@dept_bp.get('/add')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/dept/add.html')
|
||||
@dept_api.resource('/add')
|
||||
class AddDepartment(Resource):
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def get(self):
|
||||
return make_response(render_template('admin/dept/add.html'))
|
||||
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def post(self):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('address', type=str)
|
||||
parser.add_argument('deptName', type=str, dest='dept_name')
|
||||
parser.add_argument('email', type=str)
|
||||
parser.add_argument('leader', type=str)
|
||||
parser.add_argument('parentId', type=int, dest='parent_id')
|
||||
parser.add_argument('phone', type=str)
|
||||
parser.add_argument('sort', type=int)
|
||||
parser.add_argument('status', type=int)
|
||||
|
||||
res = parser.parse_args()
|
||||
|
||||
dept = Dept(
|
||||
parent_id=res.parent_id,
|
||||
dept_name=res.dept_name,
|
||||
sort=res.sort,
|
||||
leader=res.leader,
|
||||
phone=res.phone,
|
||||
email=res.email,
|
||||
status=res.status,
|
||||
address=res.address
|
||||
)
|
||||
db.session.add(dept)
|
||||
db.session.commit()
|
||||
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@dept_bp.get('/tree')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def tree():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
dept_data = Dept.query.order_by(Dept.sort).all()
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
"data": marshal(dept_data, dept_fields)
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@dept_bp.post('/save')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
check_data(DeptSchema(unknown=INCLUDE), req)
|
||||
dept_curd.save_dept(req)
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@dept_bp.get('/edit')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def edit():
|
||||
_id = request.args.get("deptId")
|
||||
dept = dept_curd.get_dept_by_id(_id)
|
||||
return render_template('admin/dept/edit.html', dept=dept)
|
||||
|
||||
|
||||
# 启用
|
||||
@dept_bp.put('/enable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def enable():
|
||||
_id = request.json.get('deptId')
|
||||
if id:
|
||||
res = dept_curd.enable_status(_id)
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('deptId', type=int, dest='dept_id', required=True)
|
||||
parser.add_argument('operate', type=int, choices=[0, 1], required=True)
|
||||
|
||||
res = parser.parse_args()
|
||||
d = Dept.query.filter_by(id=res.dept_id).update({"status": res.operate})
|
||||
if d:
|
||||
db.session.commit()
|
||||
message = '启用成功' if res.operate else '禁用成功'
|
||||
return success_api(msg=message)
|
||||
return fail_api(msg="出错啦")
|
||||
|
||||
|
||||
@dept_api.resource('/edit/<int:dept_id>')
|
||||
class DeptURD(Resource):
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def get(self, dept_id):
|
||||
dept = Dept.query.filter_by(id=dept_id).first()
|
||||
return make_response(render_template('admin/dept/edit.html', dept=dept))
|
||||
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def put(self, dept_id):
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('address', type=str)
|
||||
parser.add_argument('deptName', type=str, dest='dept_name')
|
||||
parser.add_argument('email', type=str)
|
||||
parser.add_argument('leader', type=str)
|
||||
parser.add_argument('phone', type=str)
|
||||
parser.add_argument('sort', type=int)
|
||||
parser.add_argument('status', type=int)
|
||||
|
||||
res = parser.parse_args()
|
||||
data = {
|
||||
"dept_name": res.dept_name,
|
||||
"sort": res.sort,
|
||||
"leader": res.leader,
|
||||
"phone": res.phone,
|
||||
"email": res.email,
|
||||
"status": res.status,
|
||||
"address": res.address
|
||||
}
|
||||
res = Dept.query.filter_by(id=dept_id).update(data)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
return fail_api(msg="更新失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 禁用
|
||||
@dept_bp.put('/disable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('deptId')
|
||||
if id:
|
||||
res = dept_curd.disable_status(_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_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:
|
||||
return fail_api(msg="更新失败")
|
||||
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)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
@authorize("admin:dept:remove", log=True)
|
||||
def delete(self, dept_id):
|
||||
ret = Dept.query.filter_by(id=dept_id).delete()
|
||||
User.query.filter_by(dept_id=dept_id).update({"dept_id": None})
|
||||
db.session.commit()
|
||||
if ret:
|
||||
return success_api(msg="删除成功")
|
||||
return fail_api(msg="删除失败")
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
from applications.extensions import db
|
||||
from applications.models import Dept, DeptSchema
|
||||
from applications.models import User
|
||||
from applications.common.curd import model_to_dicts
|
||||
|
||||
|
||||
def get_dept_dict():
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
res = model_to_dicts(Schema=DeptSchema, model=dept)
|
||||
return res
|
||||
|
||||
|
||||
def save_dept(req):
|
||||
address = req.get("address")
|
||||
deptName = req.get("deptName")
|
||||
email = req.get("email")
|
||||
leader = req.get("leader")
|
||||
parentId = req.get("parentId")
|
||||
phone = req.get("phone")
|
||||
sort = req.get("sort")
|
||||
status = 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 r
|
||||
|
||||
|
||||
def get_dept_by_id(_id):
|
||||
"""根据 id 获取部门"""
|
||||
d = Dept.query.filter_by(id=_id).first()
|
||||
return d
|
||||
|
||||
|
||||
def enable_status(_id):
|
||||
""" 启用权限 """
|
||||
enable = 1
|
||||
d = Dept.query.filter_by(id=_id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def disable_status(_id):
|
||||
""" 停用权限 """
|
||||
enable = 0
|
||||
d = Dept.query.filter_by(id=_id).update({"status": enable})
|
||||
if d:
|
||||
db.session.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def update_dept(json):
|
||||
""" 更新部门信息 """
|
||||
_id = json.get("deptId"),
|
||||
data = {
|
||||
"dept_name": json.get("deptName"),
|
||||
"sort": json.get("sort"),
|
||||
"leader": json.get("leader"),
|
||||
"phone": json.get("phone"),
|
||||
"email": json.get("email"),
|
||||
"status": json.get("status"),
|
||||
"address": json.get("address")
|
||||
}
|
||||
d = Dept.query.filter_by(id=_id).update(data)
|
||||
if not d:
|
||||
return False
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
|
||||
def remove_dept(_id):
|
||||
d = Dept.query.filter_by(id=_id).delete()
|
||||
if not d:
|
||||
return False
|
||||
User.query.filter_by(dept_id=_id).update({"dept_id": None})
|
||||
db.session.commit()
|
||||
return True
|
||||
@@ -7,7 +7,9 @@ from flask_restful import marshal
|
||||
|
||||
from applications.common.serialization import power_fields
|
||||
from applications.extensions import db
|
||||
from applications.models import PowerSchema, Power, Role, User
|
||||
from applications.models import Power, Role, User
|
||||
|
||||
from applications.common.serialization import power2_fields
|
||||
|
||||
|
||||
def get_render_config():
|
||||
@@ -99,8 +101,7 @@ def make_menu_tree():
|
||||
if p.type == 0 or p.type == 1:
|
||||
powers.append(p)
|
||||
|
||||
power_schema = PowerSchema(many=True) # 用已继承 ma.ModelSchema 类的自定制类生成序列化类
|
||||
power_dict = power_schema.dump(powers) # 生成可序列化对象
|
||||
power_dict = marshal(powers, power2_fields) # 生成可序列化对象
|
||||
power_dict.sort(key=lambda x: x['id'], reverse=True)
|
||||
|
||||
menu_dict = OrderedDict()
|
||||
@@ -115,7 +116,6 @@ def make_menu_tree():
|
||||
menu_dict[_dict['parent_id']] = [_dict]
|
||||
else:
|
||||
menu_dict[_dict['parent_id']].append(_dict)
|
||||
|
||||
return menu_dict.get(0)
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from flask import Blueprint, render_template, request, jsonify, make_response
|
||||
from flask import Blueprint, render_template, jsonify, make_response
|
||||
from flask_login import login_required
|
||||
|
||||
from flask_restful import Resource, Api, reqparse
|
||||
|
||||
from applications.extensions import db
|
||||
from applications.models import Role, Power
|
||||
# from applications.view.rights import role_curd
|
||||
from applications.common.utils.http import table_api, success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from flask_restful import reqparse, marshal
|
||||
|
||||
Reference in New Issue
Block a user