From f6d7d7d84c87c7b8fd219eb730520f121471ff8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E8=83=9C=E8=88=9F?= <1650473152@qq.com> Date: Wed, 28 Dec 2022 00:34:08 +0800 Subject: [PATCH] edit md --- README.md | 2 +- applications/view/admin/role.py | 1 - docs/detail.md | 140 ++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 docs/detail.md diff --git a/README.md b/README.md index 60b2ba8..4582677 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 开 箱 即 用 的 Flask 快 速 开 发 平 台 - [预 览](http://flask.pearadmin.com) | [官 网](http://www.pearadmin.com/) | [社区](http://forum.pearadmin.com/) + [预 览](http://flask.pearadmin.com) | [官 网](http://www.pearadmin.com/) | [文档](docs/detail.md)
diff --git a/applications/view/admin/role.py b/applications/view/admin/role.py index 1b4a5e8..f4781dc 100644 --- a/applications/view/admin/role.py +++ b/applications/view/admin/role.py @@ -1,7 +1,6 @@ from flask import Blueprint, render_template, request, jsonify from flask_login import login_required from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id -from applications.common.helper import ModelFilter 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 str_escape diff --git a/docs/detail.md b/docs/detail.md new file mode 100644 index 0000000..5710ca2 --- /dev/null +++ b/docs/detail.md @@ -0,0 +1,140 @@ +### 权限管理 :id=authorize + +使用装饰器 @authorize时需要注意,该装饰器需要写在 @app.route 之后 + +```python +@authorize(power: str, log: bool) +``` + +第一个参数为权限 code + +第二个参数为是否生成日志 + +```python +# 例如 +@authorize("admin:power:remove", log=True) +``` + +在前端中,例如增加,删除按钮,对于没有编辑权限的用户不显示的话,可以使用 + + `{% **if** authorize("admin:user:edit") %}` + + `{% endif %}` + +例如 + +```python + {% if authorize("admin:user:edit") %} + + {% endif %} + {% if authorize("admin:user:remove") %} + + {% endif %} +``` + +## model序列化 :id=Schema + +- sqlalchemy查询的model对象转dict + + +``` + model_to_dicts(Schema, model) +``` + +Schema 是 序列化类,我把他放在了models文件里,觉得没有必要见一个文件夹叫Schema,也方便看着模型写序列化类 + +```python +# 例如 +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() +``` + +>这一部分有问题的话请看marshmallow文档 + +model写的是查询后的对象 + +```python +dept = Dept.query.order_by(Dept.sort).all() +``` + +进行序列化 + +```python +res = model_to_dicts(Schema=DeptSchema, model=dept) +``` + +## 构造查询过滤 + +```python +# 准确查询字段 +# 不等于查询字段 +# 大于查询字段 +# 小于查询字段 +# 模糊查询字段(%+xxx+%) +# 左模糊 (% + xxx) +# 右模糊查询字段(xxx+ %) +# 包含查询字段 +#范围查询字段 +# 查询 +``` + +## xss过滤 + +```python +from applications.common.utils.validate import str_escape +details = str_escape(req.get("details")) +``` + + + +## 邮件发送 + +```python +#在.flaskenv中配置邮箱 + +from applications/common/utils/mail import send_main +send_mail(subject='title', recipients=['123@qq.com'], content='body') +``` + + + +## 返回格式 + +``` +from applications/common/utils/http import success_api,fail_api,table_api + +# 这是源代码 +def success_api(msg: str = "成功"): + """ 成功响应 默认值”成功“ """ + return jsonify(success=True, msg=msg) + + +def fail_api(msg: str = "失败"): + """ 失败响应 默认值“失败” """ + return jsonify(success=False, msg=msg) + + +def table_api(msg: str = "", count=0, data=None, limit=10): + """ 动态表格渲染响应 """ + res = { + 'msg': msg, + 'code': 0, + 'data': data, + 'count': count, + 'limit': limit + + } + return jsonify(res) +``` \ No newline at end of file