完成部门管理,增加.flaskenv配置,抽基本js和css作为引入模板
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import datetime
|
||||
from applications.models import db, ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
class Dept(db.Model):
|
||||
__tablename__ = 'admin_dept'
|
||||
id = db.Column(db.Integer, primary_key=True, comment="部门ID")
|
||||
parent_id = db.Column(db.Integer, comment="父级编号")
|
||||
dept_name = db.Column(db.String(50), comment="部门名称")
|
||||
sort = db.Column(db.Integer, comment="排序")
|
||||
leader = db.Column(db.String(50), comment="负责人")
|
||||
phone = db.Column(db.String(20), comment="联系方式")
|
||||
email = db.Column(db.String(50), comment="邮箱")
|
||||
status = db.Column(db.Integer, comment='状态(1开启,0关闭)')
|
||||
remark = db.Column(db.Text, comment="备注")
|
||||
address = db.Column(db.String(255), comment="详细地址")
|
||||
create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='创建时间')
|
||||
|
||||
|
||||
class DeptSchema(ma.Schema): # 序列化类
|
||||
deptId = fields.Integer(attribute="id")
|
||||
parentId = fields.Integer(attribute="parent_id")
|
||||
deptName = fields.Str(attribute="dept_name")
|
||||
leader = fields.Str()
|
||||
phone = fields.Str()
|
||||
email = fields.Str()
|
||||
address = fields.Str()
|
||||
status = fields.Str()
|
||||
sort = fields.Str()
|
||||
@@ -4,6 +4,7 @@ from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from applications.models import db, ma
|
||||
from marshmallow import fields
|
||||
from applications.models import admin_user_role
|
||||
from applications.models.admin_dept import Dept
|
||||
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
@@ -15,6 +16,7 @@ class User(db.Model, UserMixin):
|
||||
remark = db.Column(db.String(255), comment='备注')
|
||||
password_hash = db.Column(db.String(128), comment='哈希密码')
|
||||
enable = db.Column(db.Integer, default=0, comment='启用')
|
||||
dept_id = db.Column(db.Integer, comment='部门id')
|
||||
create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_at = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='创建时间')
|
||||
role = db.relationship('Role', secondary="admin_user_role", backref=db.backref('user'), lazy='dynamic')
|
||||
@@ -34,3 +36,11 @@ class UserSchema(ma.Schema):
|
||||
enable = fields.Integer()
|
||||
create_at = fields.DateTime()
|
||||
update_at = fields.DateTime()
|
||||
dept = fields.Method("get_dept")
|
||||
|
||||
def get_dept(self, obj):
|
||||
if obj.dept_id != None:
|
||||
return Dept.query.filter_by(id=obj.dept_id).first().dept_name
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
from markupsafe import escape, Markup
|
||||
|
||||
from applications.models import db
|
||||
from applications.models.admin_dept import Dept, DeptSchema
|
||||
from applications.models.admin_user import User
|
||||
from applications.service.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")
|
||||
parentName = req.get("parentName")
|
||||
phone = req.get("phone")
|
||||
selectParent_select_input = req.get("selectParent_select_input")
|
||||
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):
|
||||
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
|
||||
print('0')
|
||||
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"),
|
||||
print(str(Markup(json.get("leader"))))
|
||||
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
|
||||
@@ -1,22 +1,20 @@
|
||||
from applications.models import db
|
||||
from applications.models.admin_power import Power, PowerSchema2
|
||||
from applications.models.admin_role import Role
|
||||
from applications.service.common.curd import model_to_dicts
|
||||
|
||||
|
||||
def get_power_dict():
|
||||
power = Power.query.all()
|
||||
power_schema = PowerSchema2(many=True)
|
||||
power_dict = power_schema.dump(power)
|
||||
return power_dict
|
||||
|
||||
res = model_to_dicts(Schema=PowerSchema2, model=power)
|
||||
return res
|
||||
|
||||
# 选择父节点
|
||||
def select_parent():
|
||||
power = Power.query.all()
|
||||
power_schema = PowerSchema2(many=True)
|
||||
power_dict = power_schema.dump(power)
|
||||
power_dict.append({"powerId": 0, "powerName": "顶级权限", "parentId": -1})
|
||||
return power_dict
|
||||
res = model_to_dicts(Schema=PowerSchema2, model=power)
|
||||
res.append({"powerId": 0, "powerName": "顶级权限", "parentId": -1})
|
||||
return res
|
||||
|
||||
|
||||
# 增加权限
|
||||
|
||||
@@ -11,8 +11,13 @@ from applications.models.admin_log import AdminLog
|
||||
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,
|
||||
def get_user_data(page, limit, filters,deptId):
|
||||
if deptId:
|
||||
user = User.query.filter_by(dept_id=deptId).filter(and_(*[getattr(User, k).like(v) for k, v in filters.items()])).paginate(page=page,
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
else:
|
||||
user = User.query.filter(and_(*[getattr(User, k).like(v) for k, v in filters.items()])).paginate(page=page,
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
count = User.query.count()
|
||||
@@ -20,8 +25,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)
|
||||
def get_user_data_dict(page, limit, filters,deptId):
|
||||
user, count = get_user_data(page, limit, filters,deptId)
|
||||
data = model_to_dicts(Schema=UserSchema,model=user.items)
|
||||
return data, count
|
||||
|
||||
@@ -70,8 +75,8 @@ def update_avatar(url):
|
||||
|
||||
|
||||
# 更新用户信息
|
||||
def update_user(id, username, realname):
|
||||
user = User.query.filter_by(id=id).update({'username': username, 'realname': realname})
|
||||
def update_user(id, username, realname, deptId):
|
||||
user = User.query.filter_by(id=id).update({'username': username, 'realname': realname,'dept_id':deptId})
|
||||
db.session.commit()
|
||||
return user
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from applications.views.admin.dept import admin_dept
|
||||
from applications.views.admin.index import admin_index
|
||||
from applications.views.admin.user import admin_user
|
||||
from applications.views.admin.file import admin_file
|
||||
@@ -6,14 +7,10 @@ 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
|
||||
"""
|
||||
初始化蓝图
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# 初始化蓝图
|
||||
def init_adminViews(app):
|
||||
|
||||
app.register_blueprint(admin_index)
|
||||
app.register_blueprint(admin_user)
|
||||
app.register_blueprint(admin_file)
|
||||
@@ -22,4 +19,4 @@ def init_adminViews(app):
|
||||
app.register_blueprint(admin_power)
|
||||
app.register_blueprint(admin_role)
|
||||
app.register_blueprint(admin_dict)
|
||||
|
||||
app.register_blueprint(admin_dept)
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
from applications.service.admin import dept as dept_curd
|
||||
from applications.service.common.response import table_api, success_api, fail_api
|
||||
from applications.service.route_auth import authorize_and_log, authorize
|
||||
|
||||
admin_dept = Blueprint('adminDept', __name__, url_prefix='/admin/dept')
|
||||
|
||||
|
||||
@admin_dept.route('/')
|
||||
@authorize_and_log("admin:dept:main")
|
||||
def main():
|
||||
return render_template('admin/dept/main.html')
|
||||
|
||||
|
||||
@admin_dept.route('/data')
|
||||
@authorize_and_log("admin:dept:main")
|
||||
def data():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
res = {
|
||||
"data": power_data
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@admin_dept.route('/add')
|
||||
@authorize_and_log("admin:dept:add")
|
||||
def add():
|
||||
return render_template('admin/dept/add.html')
|
||||
|
||||
|
||||
@admin_dept.route('/tree')
|
||||
@authorize_and_log("admin:dept:main")
|
||||
def tree():
|
||||
power_data = dept_curd.get_dept_dict()
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@admin_dept.route('/save', methods=['POST'])
|
||||
@authorize_and_log("admin:dept:edit")
|
||||
def save():
|
||||
req = request.json
|
||||
dept_curd.save_dept(req)
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@admin_dept.route('/edit', methods=['GET', 'POST'])
|
||||
@authorize_and_log("admin:dept:edit")
|
||||
def edit():
|
||||
id = request.args.get("deptId")
|
||||
dept = dept_curd.get_dept_by_id(id)
|
||||
return render_template('admin/dept/edit.html', dept=dept)
|
||||
|
||||
|
||||
# 启用
|
||||
@admin_dept.route('/enable', methods=['PUT'])
|
||||
@authorize_and_log("admin:dept: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="启用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用
|
||||
@admin_dept.route('/disable', methods=['PUT'])
|
||||
@authorize_and_log("admin:dept:edit")
|
||||
def disenable():
|
||||
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="数据错误")
|
||||
|
||||
|
||||
@admin_dept.route('/update', methods=['PUT'])
|
||||
@authorize_and_log("admin:dept:edit")
|
||||
def update():
|
||||
res = dept_curd.update_dept(request.json)
|
||||
if not res:
|
||||
return fail_api(msg="更新失败")
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
@admin_dept.route('/remove/<int:id>', methods=['DELETE'])
|
||||
@authorize_and_log("admin:dept:remove")
|
||||
def remove(id):
|
||||
res = dept_curd.remove_dept(id)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -73,7 +73,7 @@ def update():
|
||||
return success_api(msg="更新权限成功")
|
||||
|
||||
|
||||
# 启用用户
|
||||
# 启用权限
|
||||
@admin_power.route('/enable', methods=['PUT'])
|
||||
@authorize_and_log("admin:power:edit")
|
||||
def enable():
|
||||
@@ -83,11 +83,11 @@ def enable():
|
||||
res = enable_status(id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用用户
|
||||
# 禁用权限
|
||||
@admin_power.route('/disable', methods=['PUT'])
|
||||
@authorize_and_log("admin:power:edit")
|
||||
def disenable():
|
||||
|
||||
@@ -26,12 +26,13 @@ def data():
|
||||
limit = request.args.get('limit', type=int)
|
||||
realName = request.args.get('realName', type=str)
|
||||
username = request.args.get('username', type=str)
|
||||
deptId = request.args.get('deptId', type=int)
|
||||
filters = {}
|
||||
if realName:
|
||||
filters["realname"] = ('%' + realName + '%')
|
||||
if username:
|
||||
filters["username"] = ('%' + username + '%')
|
||||
user_data, count = get_user_data_dict(page=page, limit=limit, filters=filters)
|
||||
user_data, count = get_user_data_dict(page=page, limit=limit, filters=filters,deptId=deptId)
|
||||
return table_api(data=user_data, count=count)
|
||||
|
||||
|
||||
@@ -96,8 +97,9 @@ def update():
|
||||
id = req_json.get("userId")
|
||||
username = req_json.get('username')
|
||||
realName = req_json.get('realName')
|
||||
deptId = req_json.get('deptId')
|
||||
role_ids = a.split(',')
|
||||
update_user(id, username, realName)
|
||||
update_user(id, username, realName,deptId)
|
||||
update_user_role(id, role_ids)
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
@@ -158,7 +160,6 @@ def edit_password_put():
|
||||
@authorize_and_log("admin:user:edit")
|
||||
def enable():
|
||||
id = request.json.get('userId')
|
||||
print(id)
|
||||
if id:
|
||||
res = enable_status(id)
|
||||
if not res:
|
||||
|
||||
Reference in New Issue
Block a user