增加layui表格分页器layui_paginate()
This commit is contained in:
@@ -3,7 +3,7 @@ MYSQL_HOST=127.0.0.1
|
||||
MYSQL_PORT=3306
|
||||
MYSQL_DATABASE=PearAdminFlask
|
||||
MYSQL_USERNAME=root
|
||||
MYSQL_PASSWORD=123456
|
||||
MYSQL_PASSWORD=root
|
||||
|
||||
# Redis 配置
|
||||
REDIS_HOST=127.0.0.1
|
||||
|
||||
@@ -8,7 +8,7 @@ def model_to_dicts(Schema, model):
|
||||
return output
|
||||
|
||||
|
||||
def get_one_by_id(model, id):
|
||||
def get_one_by_id(model: db.Model, id):
|
||||
"""
|
||||
:param model: 模型类
|
||||
:param id: id
|
||||
@@ -17,8 +17,19 @@ def get_one_by_id(model, id):
|
||||
return model.query.filter_by(id=id).first()
|
||||
|
||||
|
||||
def delete_one_by_id(model: db.Model, id):
|
||||
"""
|
||||
:param model: 模型类
|
||||
:param id: id
|
||||
:return: 返回单个查询结果
|
||||
"""
|
||||
r = model.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
return r
|
||||
|
||||
|
||||
# 启动状态
|
||||
def enable_status(model, id):
|
||||
def enable_status(model: db.Model, id):
|
||||
enable = 1
|
||||
role = model.query.filter_by(id=id).update({"enable": enable})
|
||||
if role:
|
||||
@@ -28,7 +39,7 @@ def enable_status(model, id):
|
||||
|
||||
|
||||
# 停用状态
|
||||
def disable_status(model, id):
|
||||
def disable_status(model: db.Model, id):
|
||||
enable = 0
|
||||
role = model.query.filter_by(id=id).update({"enable": enable})
|
||||
if role:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from flask import Flask, request
|
||||
from flask_sqlalchemy import SQLAlchemy, BaseQuery
|
||||
from flask_marshmallow import Marshmallow
|
||||
from marshmallow import fields
|
||||
from marshmallow.validate import (
|
||||
@@ -45,7 +45,20 @@ fields.Boolean.default_error_messages = {
|
||||
"invalid": "不是合法布尔值"
|
||||
}
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
class Query(BaseQuery):
|
||||
def layui_paginate(self):
|
||||
"""
|
||||
layui表格分页
|
||||
page
|
||||
limit
|
||||
"""
|
||||
return self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
|
||||
|
||||
db = SQLAlchemy(query_class=Query)
|
||||
ma = Marshmallow()
|
||||
|
||||
|
||||
|
||||
@@ -6,4 +6,4 @@ from .admin_power import Power
|
||||
from .admin_role import Role
|
||||
from .admin_role_power import role_power
|
||||
from .admin_user import User
|
||||
from .admin_user_role import user_role
|
||||
from .admin_user_role import user_role
|
||||
@@ -28,11 +28,7 @@ def index():
|
||||
@admin_log.get('/loginLog')
|
||||
@authorize("admin:log:main")
|
||||
def login_log():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
log = AdminLog.query.filter_by(url='/admin/login').order_by(desc(AdminLog.create_time)).paginate(page=page,
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
log = AdminLog.query.filter_by(url='/admin/login').order_by(desc(AdminLog.create_time)).layui_paginate()
|
||||
count = AdminLog.query.filter_by(url='/admin/login').count()
|
||||
data = model_to_dicts(Schema=LogSchema, model=log.items)
|
||||
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.utils.http import table_api, success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import DictType, DictData
|
||||
from applications.common.admin import dict_curd
|
||||
from applications.schemas import DictTypeSchema, DictDataSchema
|
||||
|
||||
admin_dict = Blueprint('adminDict', __name__, url_prefix='/admin/dict')
|
||||
|
||||
@@ -18,11 +22,14 @@ def main():
|
||||
@admin_dict.get('/dictType/data')
|
||||
@authorize("admin:dict:main", log=True)
|
||||
def dict_type_data():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
type_name = xss_escape(request.args.get('typeName', type=str))
|
||||
data, count = dict_curd.get_dict_type(page=page, limit=limit, type_name=type_name)
|
||||
return table_api(data=data,count=count)
|
||||
dict_all = DictType.query
|
||||
if type_name:
|
||||
dict_all = dict_all.filter(DictType.type_name.like('%' + type_name + '%'))
|
||||
dict_all = dict_all.layui_paginate()
|
||||
count = DictType.query.count()
|
||||
data = curd.model_to_dicts(Schema=DictTypeSchema, model=dict_all.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
@admin_dict.get('/dictType/add')
|
||||
@@ -35,8 +42,14 @@ def dict_type_add():
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_type_save():
|
||||
req_json = request.json
|
||||
res = dict_curd.save_dict_type(req_json=req_json)
|
||||
if res is None:
|
||||
description = xss_escape(req_json.get("description"))
|
||||
enable = xss_escape(req_json.get("enable"))
|
||||
type_code = xss_escape(req_json.get("typeCode"))
|
||||
type_name = xss_escape(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()
|
||||
if d.id is None:
|
||||
return fail_api(msg="增加失败")
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
@@ -98,11 +111,11 @@ def dict_type_delete(_id):
|
||||
@admin_dict.get('/dictData/data')
|
||||
@authorize("admin:dict:main", log=True)
|
||||
def dict_code_data():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
type_code = xss_escape(request.args.get('typeCode', type=str))
|
||||
data, count = dict_curd.get_dict_data(page=page, limit=limit, type_code=type_code)
|
||||
return table_api(data=data,count=count)
|
||||
dict_data = DictData.query.filter_by(type_code=type_code).layui_paginate()
|
||||
count = DictType.query.count()
|
||||
data = curd.model_to_dicts(Schema=DictDataSchema, model=dict_data.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
# 增加字典数据
|
||||
@@ -118,8 +131,15 @@ def dict_data_add():
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_data_save():
|
||||
req_json = request.json
|
||||
res = dict_curd.save_dict_data(req_json=req_json)
|
||||
if not res:
|
||||
data_label = xss_escape(req_json.get("dataLabel"))
|
||||
data_value = xss_escape(req_json.get("dataValue"))
|
||||
enable = xss_escape(req_json.get("enable"))
|
||||
remark = xss_escape(req_json.get("remark"))
|
||||
type_code = xss_escape(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()
|
||||
if not d.id:
|
||||
return jsonify(success=False, msg="增加失败")
|
||||
return jsonify(success=True, msg="增加成功")
|
||||
|
||||
@@ -129,7 +149,7 @@ def dict_data_save():
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_edit():
|
||||
_id = request.args.get('dataId', type=str)
|
||||
dict_data = DictData.query.filter_by(id=_id).first()
|
||||
dict_data = curd.get_one_by_id(DictData, _id)
|
||||
return render_template('admin/dict/data/edit.html', dict_data=dict_data)
|
||||
|
||||
|
||||
@@ -138,7 +158,15 @@ def dict_data_edit():
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_update():
|
||||
req_json = request.json
|
||||
dict_curd.update_dict_data(req_json)
|
||||
id = req_json.get("dataId")
|
||||
DictData.query.filter_by(id=id).update({
|
||||
"data_label": xss_escape(req_json.get("dataLabel")),
|
||||
"data_value": xss_escape(req_json.get("dataValue")),
|
||||
"enable": xss_escape(req_json.get("enable")),
|
||||
"remark": xss_escape(req_json.get("remark")),
|
||||
"type_code": xss_escape(req_json.get("typeCode"))
|
||||
})
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
@@ -148,7 +176,7 @@ def dict_data_update():
|
||||
def dict_data_enable():
|
||||
_id = request.json.get('dataId')
|
||||
if _id:
|
||||
res = dict_curd.enable_dict_data_status(_id)
|
||||
res = curd.enable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
@@ -161,7 +189,7 @@ def dict_data_enable():
|
||||
def dict_data_disenable():
|
||||
_id = request.json.get('dataId')
|
||||
if _id:
|
||||
res = dict_curd.disable_dict_data_status(_id)
|
||||
res = curd.disable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
@@ -172,7 +200,7 @@ def dict_data_disenable():
|
||||
@admin_dict.delete('dictData/remove/<int:id>')
|
||||
@authorize("admin:dict:remove", log=True)
|
||||
def dict_data_delete(id):
|
||||
res = dict_curd.delete_data_by_id(id)
|
||||
res = curd.delete_one_by_id(model=DictData, id=id)
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.admin import power_curd
|
||||
from applications.common.utils.http import success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Power
|
||||
from applications.schemas import PowerSchema2
|
||||
|
||||
admin_power = Blueprint('adminPower', __name__, url_prefix='/admin/power')
|
||||
|
||||
@@ -15,9 +21,10 @@ def index():
|
||||
@admin_power.get('/data')
|
||||
@authorize("admin:power:main", log=True)
|
||||
def data():
|
||||
power_data = power_curd.get_power_dict()
|
||||
power = Power.query.all()
|
||||
res = curd.model_to_dicts(Schema=PowerSchema2, model=power)
|
||||
res = {
|
||||
"data": power_data
|
||||
"data": res
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
@@ -31,10 +38,12 @@ def add():
|
||||
@admin_power.get('/selectParent')
|
||||
@authorize("admin:power:main", log=True)
|
||||
def select_parent():
|
||||
power_data = power_curd.select_parent()
|
||||
power = Power.query.all()
|
||||
res = curd.model_to_dicts(Schema=PowerSchema2, model=power)
|
||||
res.append({"powerId": 0, "powerName": "顶级权限", "parentId": -1})
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
"data": res
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
@@ -45,7 +54,27 @@ def select_parent():
|
||||
@authorize("admin:power:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
power_curd.save_power(req)
|
||||
icon = xss_escape(req.get("icon"))
|
||||
openType = xss_escape(req.get("openType"))
|
||||
parentId = xss_escape(req.get("parentId"))
|
||||
powerCode = xss_escape(req.get("powerCode"))
|
||||
powerName = xss_escape(req.get("powerName"))
|
||||
powerType = xss_escape(req.get("powerType"))
|
||||
powerUrl = xss_escape(req.get("powerUrl"))
|
||||
sort = xss_escape(req.get("sort"))
|
||||
power = Power(
|
||||
icon=icon,
|
||||
open_type=openType,
|
||||
parent_id=parentId,
|
||||
code=powerCode,
|
||||
name=powerName,
|
||||
type=powerType,
|
||||
url=powerUrl,
|
||||
sort=sort,
|
||||
enable=1
|
||||
)
|
||||
r = db.session.add(power)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@@ -53,7 +82,7 @@ def save():
|
||||
@admin_power.get('/edit/<int:_id>')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def edit(_id):
|
||||
power = power_curd.get_power_by_id(_id)
|
||||
power = curd.get_one_by_id(Power, _id)
|
||||
icon = str(power.icon).split()
|
||||
if len(icon) == 2:
|
||||
icon = icon[1]
|
||||
@@ -66,7 +95,20 @@ def edit(_id):
|
||||
@admin_power.put('/update')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def update():
|
||||
res = power_curd.update_power(request.json)
|
||||
req_json = request.json
|
||||
id = request.json.get("powerId")
|
||||
data = {
|
||||
"icon": xss_escape(req_json.get("icon")),
|
||||
"open_type": xss_escape(req_json.get("openType")),
|
||||
"parent_id": xss_escape(req_json.get("parentId")),
|
||||
"code": xss_escape(req_json.get("powerCode")),
|
||||
"name": xss_escape(req_json.get("powerName")),
|
||||
"type": xss_escape(req_json.get("powerType")),
|
||||
"url": xss_escape(req_json.get("powerUrl")),
|
||||
"sort": xss_escape(req_json.get("sort"))
|
||||
}
|
||||
res = Power.query.filter_by(id=id).update(data)
|
||||
db.session.commit()
|
||||
if not res:
|
||||
return fail_api(msg="更新权限失败")
|
||||
return success_api(msg="更新权限成功")
|
||||
@@ -78,7 +120,7 @@ def update():
|
||||
def enable():
|
||||
_id = request.json.get('powerId')
|
||||
if id:
|
||||
res = power_curd.enable_status(_id)
|
||||
res = curd.enable_status(Power,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启用成功")
|
||||
@@ -91,7 +133,7 @@ def enable():
|
||||
def dis_enable():
|
||||
_id = request.json.get('powerId')
|
||||
if id:
|
||||
res = power_curd.disable_status(_id)
|
||||
res = curd.disable_status(Power,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
|
||||
@@ -23,8 +23,6 @@ def main():
|
||||
@admin_role.get('/data')
|
||||
@authorize("admin:role:main", log=True)
|
||||
def table():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
role_name = xss_escape(request.args.get('roleName', type=str))
|
||||
role_code = xss_escape(request.args.get('roleCode', type=str))
|
||||
mf = ModelFilter()
|
||||
@@ -32,7 +30,7 @@ def table():
|
||||
mf.vague(field_name="name", value=role_name)
|
||||
if role_code:
|
||||
mf.vague(field_name="code", value=role_code)
|
||||
role = Role.query.filter(mf.get_filter(Role)).paginate(page=page, per_page=limit, error_out=False)
|
||||
role = Role.query.filter(mf.get_filter(Role)).layui_paginate()
|
||||
count = Role.query.count()
|
||||
data = model_to_dicts(Schema=RoleSchema, model=role.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
@@ -2,6 +2,7 @@ from flask import Blueprint, render_template, request
|
||||
from flask_login import login_required, current_user
|
||||
from sqlalchemy import and_, desc
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.curd import model_to_dicts, enable_status, disable_status
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.common.utils.http import table_api, fail_api, success_api
|
||||
@@ -26,8 +27,6 @@ def main():
|
||||
@admin_user.get('/data')
|
||||
@authorize("admin:user:main", log=True)
|
||||
def data():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
real_name = xss_escape(request.args.get('realName', type=str))
|
||||
username = xss_escape(request.args.get('username', type=str))
|
||||
dept_id = request.args.get('deptId', type=int)
|
||||
@@ -38,9 +37,7 @@ def data():
|
||||
mf.vague(field_name="username", value=username)
|
||||
if dept_id:
|
||||
mf.exact(field_name="dept_id", value=dept_id)
|
||||
user = User.query.filter(mf.get_filter(model=User)).paginate(page=page,
|
||||
per_page=limit,
|
||||
error_out=False)
|
||||
user = User.query.filter(mf.get_filter(model=User)).layui_paginate()
|
||||
count = User.query.count()
|
||||
data = model_to_dicts(Schema=UserSchema, model=user.items)
|
||||
return table_api(data=data, count=count)
|
||||
@@ -102,7 +99,7 @@ def delete(id):
|
||||
@admin_user.get('/edit/<int:id>')
|
||||
@authorize("admin:user:edit", log=True)
|
||||
def edit(id):
|
||||
user = User.query.filter_by(id=id).first()
|
||||
user = curd.get_one_by_id(User,id)
|
||||
roles = Role.query.all()
|
||||
checked_roles = []
|
||||
for r in user.role:
|
||||
|
||||
Reference in New Issue
Block a user