Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c7517be4a | ||
|
|
9b8d632995 | ||
|
|
9bfdac0953 | ||
|
|
2e6067fd31 | ||
|
|
a3c61d3291 | ||
|
|
ef8044ea22 | ||
|
|
56dcc7eff8 | ||
|
|
4eeb2cfec9 | ||
|
|
28d503eb8b | ||
|
|
0bec251f08 | ||
|
|
027481ce56 | ||
|
|
dd6cbf2694 | ||
|
|
7d179aae2e | ||
|
|
b5fbd9867b | ||
|
|
2acc3cce67 | ||
|
|
0cd0c414ee |
@@ -1,29 +1,14 @@
|
||||
# flask配置
|
||||
FLASK_APP=app.py
|
||||
FLASK_APP="entrance:create_app()"
|
||||
FLASK_ENV=development
|
||||
FLASK_DEBUG=1
|
||||
FLASK_RUN_HOST = 127.0.0.1
|
||||
FLASK_RUN_PORT = 5000
|
||||
|
||||
# pear admin flask配置
|
||||
SYSTEM_NAME = Pear Admin
|
||||
|
||||
# MySql配置信息
|
||||
MYSQL_HOST=127.0.0.1
|
||||
# MYSQL_HOST=dbserver
|
||||
MYSQL_PORT=3306
|
||||
MYSQL_DATABASE=PearAdminFlask
|
||||
MYSQL_DATABASE=next
|
||||
MYSQL_USERNAME=root
|
||||
MYSQL_PASSWORD=123456
|
||||
|
||||
# Redis 配置
|
||||
# REDIS_HOST=127.0.0.1
|
||||
# REDIS_PORT=6379
|
||||
|
||||
# 密钥配置(记得改)
|
||||
SECRET_KEY='pear-admin-flask'
|
||||
|
||||
# 邮箱配置
|
||||
MAIL_SERVER='smtp.qq.com'
|
||||
MAIL_USERNAME='123@qq.com'
|
||||
MAIL_PASSWORD='XXXXX' # 生成的授权码
|
||||
MYSQL_PASSWORD=root
|
||||
-25
@@ -1,25 +0,0 @@
|
||||
FROM python:3.7-alpine
|
||||
|
||||
|
||||
RUN apk update \
|
||||
&& apk --update add --no-cache gcc \
|
||||
&& apk --update add --no-cache g++ \
|
||||
&& apk --update add --no-cache tzdata \
|
||||
&& apk --update add --no-cache libffi-dev \
|
||||
&& apk --update add --no-cache libxslt-dev \
|
||||
&& apk --update add --no-cache jpeg-dev
|
||||
|
||||
ENV TIME_ZONE Asia/Shanghai
|
||||
ENV PIPURL "https://pypi.tuna.tsinghua.edu.cn/simple"
|
||||
|
||||
RUN echo "${TIME_ZONE}" > /etc/timezone \
|
||||
&& ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime
|
||||
|
||||
COPY . /app
|
||||
WORKDIR /app
|
||||
|
||||
RUN pip --no-cache-dir install -i ${PIPURL} --upgrade pip \
|
||||
&& pip --no-cache-dir install -i ${PIPURL} -r requirement/requirement-dev.txt \
|
||||
&& pip --no-cache-dir install -i ${PIPURL} gunicorn \
|
||||
&& chmod +x start.sh
|
||||
CMD ./start.sh
|
||||
@@ -8,7 +8,7 @@
|
||||
开 箱 即 用 的 Flask 快 速 开 发 平 台
|
||||
</h4>
|
||||
|
||||
[预 览](http://flask.pearadmin.com) | [官 网](http://www.pearadmin.com/) | [群聊](https://jq.qq.com/?_wv=1027&k=TkLAKUxC) | [社区](http://forum.pearadmin.com/)
|
||||
[预 览](http://flask.pearadmin.com) | [官 网](http://www.pearadmin.com/) | [群聊](https://jq.qq.com/?_wv=1027&k=5OdSmve) | [社区](http://forum.pearadmin.com/)
|
||||
|
||||
|
||||
<p align="center">
|
||||
@@ -29,136 +29,28 @@
|
||||
</div>
|
||||
|
||||
#### 项目简介
|
||||
>Pear Admin Flask 基于 Flask 的后台管理系统,拥抱应用广泛的python语言,通过使用本系统,即可快速构建你的功能业务
|
||||
>
|
||||
>项目旨在为python开发者提供一个后台管理系统的模板,成为您构建信息管理系统,物联网后台....等等应用时灵活,简单的工具
|
||||
>
|
||||
>众人拾柴火焰高,欢迎pythoner参与项目~
|
||||
>Pear Admin Flask 新版本开发中
|
||||
|
||||
Pear Admin Flask 有以下几个版本:
|
||||
|
||||
[master分支版本 ](https://gitee.com/pear-admin/pear-admin-flask/tree/master/)
|
||||
|
||||
> flask 2.0.1 + flask-sqlalchemy + mysql + 权限验证 + Flask-APScheduler 定时任务 + marshmallow 序列化与数据验证
|
||||
|
||||
[Mini 分支版本 ](https://gitee.com/pear-admin/pear-admin-flask/tree/mini/)
|
||||
|
||||
>flask 2.0.1 + flask-sqlalchemy + sqllite + 权限验证 + Flask-RESTful 序列化与数据验证
|
||||
|
||||
[v1 分支版本(不再更新,仅供参考) ](https://gitee.com/pear-admin/pear-admin-flask/tree/v1/)
|
||||
|
||||
>flask 1.12 + flask-sqlalchemy + mysql + 权限验证 + marshmallow 序列化与数据验证
|
||||
|
||||
[simple 分支版本(不再更新,仅供参考) ](https://gitee.com/pear-admin/pear-admin-flask/tree/simple/)
|
||||
|
||||
>flask 1.12 + flask-sqlalchemy + mysql + 极简权限 + marshmallow 序列化与数据验证
|
||||
>
|
||||
>项目最简版本
|
||||
|
||||
#### 内置功能
|
||||
|
||||
- [x] 用户管理:用户是系统操作者,该功能主要完成系统用户配置。
|
||||
- [x] 权限管理:配置系统菜单,操作权限,按钮权限标识等。
|
||||
- [x] 角色管理:角色菜单权限分配。
|
||||
- [x] 操作日志:系统正常操作日志记录和查询;系统异常信息日志记录和查询。
|
||||
- [x] 登录日志:系统登录日志记录查询包含登录异常。
|
||||
- [x] 服务监控:监视当前系统CPU、内存、磁盘、python版本,运行时长等相关信息。
|
||||
- [x] 文件上传: 图片上传示例
|
||||
- [x] 定时任务: 简单的定时任务
|
||||
- [ ] 代码生成: 构想中....
|
||||
|
||||
#### 项目结构
|
||||
|
||||
```
|
||||
Pear Admin Flask
|
||||
├─applications # 应用
|
||||
│ ├─configs # 配置文件
|
||||
│ │ ├─ common.py # 普通配置
|
||||
│ │ └─ config.py # 配置文件对象
|
||||
│ ├─extensions # 注册插件
|
||||
│ ├─models # 数据模型
|
||||
│ ├─static # 静态资源文件
|
||||
│ ├─templates # 静态模板文件
|
||||
│ └─views # 视图部分
|
||||
│ ├─admin # 后台管理视图模块
|
||||
│ └─index # 前台视图模块
|
||||
├─docs # 文档说明(占坑)
|
||||
├─migrations # 迁移文件记录
|
||||
├─requirement # 依赖文件
|
||||
├─test # 测试文件夹(占坑)
|
||||
└─.env # 项目的配置文件
|
||||
#### 安装
|
||||
|
||||
```shell
|
||||
pip install -r require/common.txt
|
||||
```
|
||||
|
||||
#### 项目安装
|
||||
#### 目录结构
|
||||
|
||||
```bash
|
||||
# 下 载
|
||||
git clone https://gitee.com/pear-admin/pear-admin-flask
|
||||
pear-admin-flask
|
||||
|
||||
# 安 装
|
||||
pip install -r requirement\requirement-dev.txt
|
||||
common -> 公共
|
||||
|
||||
# 配 置
|
||||
.env
|
||||
entrance -> 核心
|
||||
|
||||
```
|
||||
logs -> 日志
|
||||
|
||||
#### 修改配置
|
||||
modules -> 蓝图,视图,模型,服务
|
||||
|
||||
```python
|
||||
.env
|
||||
# MySql配置信息
|
||||
MYSQL_HOST=127.0.0.1
|
||||
MYSQL_PORT=3306
|
||||
MYSQL_DATABASE=PearAdminFlask
|
||||
MYSQL_USERNAME=root
|
||||
MYSQL_PASSWORD=root
|
||||
require -> 安装需要的文件
|
||||
|
||||
# Redis 配置
|
||||
REDIS_HOST=127.0.0.1
|
||||
REDIS_PORT=6379
|
||||
.flaskenv -> 配置文件
|
||||
|
||||
# 密钥配置
|
||||
SECRET_KEY='pear-admin-flask'
|
||||
|
||||
# 邮箱配置
|
||||
MAIL_SERVER='smtp.qq.com'
|
||||
MAIL_USERNAME='123@qq.com'
|
||||
MAIL_PASSWORD='XXXXX' # 生成的授权码
|
||||
```
|
||||
|
||||
#### Venv 安装
|
||||
|
||||
```bash
|
||||
python -m venv venv
|
||||
```
|
||||
|
||||
#### 运行项目
|
||||
|
||||
```bash
|
||||
# 初 始 化 数 据 库
|
||||
|
||||
flask init
|
||||
```
|
||||
|
||||
执行 flask run 命令启动项目
|
||||
|
||||
#### 命令行创建视图
|
||||
|
||||
```bash
|
||||
# 示例
|
||||
|
||||
flask new --type view --name test/a
|
||||
|
||||
# 自动注册蓝图
|
||||
# 访问http://127.0.0.1:5000/test/a/
|
||||
```
|
||||
|
||||
#### 预览项目
|
||||
|
||||
| | |
|
||||
|---------------------|---------------------|
|
||||
|  |  |
|
||||
| |  |
|
||||
|  |  |
|
||||
app.py -> 启动模块(使用flask run命令的话可以删除)
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
from applications import create_app
|
||||
from flask_migrate import Migrate
|
||||
from applications.extensions import db
|
||||
from entrance import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
migrate = Migrate(app, db)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
import os
|
||||
from flask import Flask
|
||||
|
||||
from applications.common.script import init_script
|
||||
from applications.extensions import init_plugs
|
||||
from applications.view import init_view
|
||||
from applications.configs import config
|
||||
|
||||
|
||||
def create_app(config_name=None):
|
||||
app = Flask(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
if not config_name:
|
||||
# 尝试从本地环境中读取
|
||||
config_name = os.getenv('FLASK_CONFIG', 'development')
|
||||
|
||||
# 引入数据库配置
|
||||
app.config.from_object(config[config_name])
|
||||
|
||||
# 注册各种插件
|
||||
init_plugs(app)
|
||||
|
||||
# 注册路由
|
||||
init_view(app)
|
||||
|
||||
# 注册命令
|
||||
init_script(app)
|
||||
|
||||
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
|
||||
logo()
|
||||
|
||||
return app
|
||||
|
||||
|
||||
def logo():
|
||||
print('''
|
||||
_____ _ _ ______ _ _
|
||||
| __ \ /\ | | (_) | ____| | | |
|
||||
| |__) |__ __ _ _ __ / \ __| |_ __ ___ _ _ __ | |__ | | __ _ ___| | __
|
||||
| ___/ _ \/ _` | '__| / /\ \ / _` | '_ ` _ \| | '_ \ | __| | |/ _` / __| |/ /
|
||||
| | | __/ (_| | | / ____ \ (_| | | | | | | | | | | | | | | (_| \__ \ <
|
||||
|_| \___|\__,_|_| /_/ \_\__,_|_| |_| |_|_|_| |_| |_| |_|\__,_|___/_|\_\\
|
||||
|
||||
''')
|
||||
@@ -1,145 +0,0 @@
|
||||
import copy
|
||||
from collections import OrderedDict
|
||||
from io import BytesIO
|
||||
from flask import session, make_response, current_app
|
||||
from flask_login import current_user
|
||||
|
||||
from applications.common.utils.gen_captcha import gen_captcha
|
||||
from applications.schemas import PowerOutSchema
|
||||
|
||||
|
||||
# 授权路由存入session
|
||||
def add_auth_session():
|
||||
role = current_user.role
|
||||
user_power = []
|
||||
for i in role:
|
||||
if i.enable == 0:
|
||||
continue
|
||||
for p in i.power:
|
||||
if p.enable == 0:
|
||||
continue
|
||||
user_power.append(p.code)
|
||||
session['permissions'] = user_power
|
||||
|
||||
|
||||
# 生成菜单树
|
||||
def make_menu_tree():
|
||||
role = current_user.role
|
||||
powers = []
|
||||
for i in role:
|
||||
# 如果角色没有被启用就直接跳过
|
||||
if i.enable == 0:
|
||||
continue
|
||||
# 变量角色用户的权限
|
||||
for p in i.power:
|
||||
# 如果权限关闭了就直接跳过
|
||||
if p.enable == 0:
|
||||
continue
|
||||
# 一二级菜单
|
||||
if int(p.type) in [0,1] and p not in powers:
|
||||
powers.append(p)
|
||||
|
||||
power_schema = PowerOutSchema(many=True) # 用已继承 ma.ModelSchema 类的自定制类生成序列化类
|
||||
power_dict = power_schema.dump(powers) # 生成可序列化对象
|
||||
power_dict.sort(key=lambda x: (x['parent_id'], x['id']), reverse=True)
|
||||
|
||||
menu_dict = OrderedDict()
|
||||
for _dict in power_dict:
|
||||
if _dict['id'] in menu_dict:
|
||||
# 当前节点添加子节点
|
||||
_dict['children'] = copy.deepcopy(menu_dict[_dict['id']])
|
||||
_dict['children'].sort(key=lambda item: item['sort'])
|
||||
# 删除子节点
|
||||
del menu_dict[_dict['id']]
|
||||
|
||||
if _dict['parent_id'] not in menu_dict:
|
||||
menu_dict[_dict['parent_id']] = [_dict]
|
||||
else:
|
||||
menu_dict[_dict['parent_id']].append(_dict)
|
||||
return sorted(menu_dict.get(0), key=lambda item: item['sort'])
|
||||
|
||||
|
||||
# 生成验证码
|
||||
def get_captcha():
|
||||
code, image = gen_captcha()
|
||||
out = BytesIO()
|
||||
session["code"] = code
|
||||
image.save(out, 'png')
|
||||
out.seek(0)
|
||||
resp = make_response(out.read())
|
||||
resp.content_type = 'image/png'
|
||||
return resp, code
|
||||
|
||||
|
||||
def get_render_config():
|
||||
# 网站配置
|
||||
config = dict(logo={
|
||||
# 网站名称
|
||||
"title": current_app.config.get("SYSTEM_NAME"),
|
||||
# 网站图标
|
||||
"image": "/static/admin/admin/images/logo.png"
|
||||
# 菜单配置
|
||||
}, menu={
|
||||
# 菜单数据来源
|
||||
"data": "/rights/menu",
|
||||
"collaspe": False,
|
||||
# 是否同时只打开一个菜单目录
|
||||
"accordion": True,
|
||||
"method": "GET",
|
||||
# 是否开启多系统菜单模式
|
||||
"control": False,
|
||||
# 顶部菜单宽度 PX
|
||||
"controlWidth": 500,
|
||||
# 默认选中的菜单项
|
||||
"select": "0",
|
||||
# 是否开启异步菜单,false 时 data 属性设置为菜单数据,false 时为 json 文件或后端接口
|
||||
"async": True
|
||||
}, tab={
|
||||
# 是否开启多选项卡
|
||||
"enable": True,
|
||||
# 切换选项卡时,是否刷新页面状态
|
||||
"keepState": True,
|
||||
# 是否开启 Tab 记忆
|
||||
"session": True,
|
||||
# 最大可打开的选项卡数量
|
||||
"max": 30,
|
||||
"index": {
|
||||
# 标识 ID , 建议与菜单项中的 ID 一致
|
||||
"id": "10",
|
||||
# 页面地址
|
||||
"href": "/admin/welcome",
|
||||
# 标题
|
||||
"title": "首页"
|
||||
}
|
||||
}, theme={
|
||||
# 默认主题色,对应 colors 配置中的 ID 标识
|
||||
"defaultColor": "2",
|
||||
# 默认的菜单主题 dark-theme 黑 / light-theme 白
|
||||
"defaultMenu": "dark-theme",
|
||||
# 是否允许用户切换主题,false 时关闭自定义主题面板
|
||||
"allowCustom": True
|
||||
}, colors=[{
|
||||
"id": "1",
|
||||
"color": "#2d8cf0"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"color": "#5FB878"
|
||||
},
|
||||
{
|
||||
"id": "3",
|
||||
"color": "#1E9FFF"
|
||||
}, {
|
||||
"id": "4",
|
||||
"color": "#FFB800"
|
||||
}, {
|
||||
"id": "5",
|
||||
"color": "darkgray"
|
||||
}
|
||||
], links=current_app.config.get("SYSTEM_PANEL_LINKS"), other={
|
||||
# 主页动画时长
|
||||
"keepLoad": 1200,
|
||||
# 布局顶部主题
|
||||
"autoHead": False
|
||||
}, header=False)
|
||||
return config
|
||||
@@ -1,57 +0,0 @@
|
||||
from flask_login import current_user
|
||||
|
||||
from applications.common.utils.validate import xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import AdminLog
|
||||
|
||||
|
||||
def login_log(request, uid, is_access):
|
||||
info = {
|
||||
'method': request.method,
|
||||
'url': request.path,
|
||||
'ip': request.remote_addr,
|
||||
'user_agent': xss_escape(request.headers.get('User-Agent')),
|
||||
'desc': xss_escape(request.form.get('username')),
|
||||
'uid': uid,
|
||||
'success': int(is_access)
|
||||
|
||||
}
|
||||
log = AdminLog(
|
||||
url=info.get('url'),
|
||||
ip=info.get('ip'),
|
||||
user_agent=info.get('user_agent'),
|
||||
desc=info.get('desc'),
|
||||
uid=info.get('uid'),
|
||||
method=info.get('method'),
|
||||
success=info.get('success')
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.flush()
|
||||
db.session.commit()
|
||||
return log.id
|
||||
|
||||
|
||||
def admin_log(request, is_access):
|
||||
info = {
|
||||
'method': request.method,
|
||||
'url': request.path,
|
||||
'ip': request.remote_addr,
|
||||
'user_agent': xss_escape(request.headers.get('User-Agent')),
|
||||
'desc': xss_escape(str(dict(request.values))),
|
||||
'uid': current_user.id,
|
||||
'success': int(is_access)
|
||||
|
||||
}
|
||||
log = AdminLog(
|
||||
url=info.get('url'),
|
||||
ip=info.get('ip'),
|
||||
user_agent=info.get('user_agent'),
|
||||
desc=info.get('desc'),
|
||||
uid=info.get('uid'),
|
||||
method=info.get('method'),
|
||||
success=info.get('success')
|
||||
)
|
||||
db.session.add(log)
|
||||
db.session.commit()
|
||||
|
||||
return log.id
|
||||
@@ -1,94 +0,0 @@
|
||||
import datetime
|
||||
|
||||
from marshmallow import Schema
|
||||
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
|
||||
|
||||
from applications.extensions import db, ma
|
||||
|
||||
|
||||
class LogicalDeleteMixin(object):
|
||||
"""
|
||||
class Test(db.Model,LogicalDeleteMixin):
|
||||
__tablename__ = 'admin_test'
|
||||
id = db.Column(db.Integer, primary_key=True, comment='角色ID')
|
||||
|
||||
Test.query.filter_by(id=1).soft_delete()
|
||||
Test.query.logic_all()
|
||||
"""
|
||||
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='创建时间')
|
||||
delete_at = db.Column(db.DateTime, comment='删除时间')
|
||||
|
||||
|
||||
def auto_model_jsonify(data, model: db.Model):
|
||||
"""
|
||||
不需要建立schemas,直接使用orm的定义模型进行序列化
|
||||
基本功能,待完善
|
||||
示例
|
||||
power_data = curd.auto_model_jsonify(model=Dept, data=dept)
|
||||
"""
|
||||
def get_model():
|
||||
return model
|
||||
|
||||
class AutoSchema(SQLAlchemyAutoSchema):
|
||||
class Meta(Schema):
|
||||
model = get_model()
|
||||
include_fk = True
|
||||
include_relationships = True
|
||||
load_instance = True
|
||||
|
||||
common_schema = AutoSchema(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = common_schema.dump(data)
|
||||
return output
|
||||
|
||||
|
||||
def model_to_dicts(schema: ma.Schema, data):
|
||||
"""
|
||||
:param schema: schema类
|
||||
:param model: sqlalchemy查询结果
|
||||
:return: 返回单个查询结果
|
||||
"""
|
||||
# 如果是分页器返回,需要传入model.items
|
||||
common_schema = schema(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = common_schema.dump(data) # 生成可序列化对象
|
||||
return output
|
||||
|
||||
|
||||
def get_one_by_id(model: db.Model, id):
|
||||
"""
|
||||
:param model: 模型类
|
||||
:param id: id
|
||||
:return: 返回单个查询结果
|
||||
"""
|
||||
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: db.Model, id):
|
||||
enable = 1
|
||||
role = model.query.filter_by(id=id).update({"enable": enable})
|
||||
if role:
|
||||
db.session.commit()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
# 停用状态
|
||||
def disable_status(model: db.Model, id):
|
||||
enable = 0
|
||||
role = model.query.filter_by(id=id).update({"enable": enable})
|
||||
if role:
|
||||
db.session.commit()
|
||||
return True
|
||||
return False
|
||||
@@ -1,126 +0,0 @@
|
||||
from sqlalchemy import and_
|
||||
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class ModelFilter:
|
||||
"""
|
||||
orm多参数构造器
|
||||
"""
|
||||
filter_field = {}
|
||||
filter_list = []
|
||||
|
||||
type_exact = "exact"
|
||||
type_neq = "neq"
|
||||
type_greater = "greater"
|
||||
type_less = "less"
|
||||
type_vague = "vague"
|
||||
type_contains = "contains"
|
||||
type_between = "between"
|
||||
|
||||
def __init__(self):
|
||||
self.filter_field = {}
|
||||
self.filter_list = []
|
||||
|
||||
def exact(self, field_name, value):
|
||||
"""
|
||||
准确查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": value, "type": self.type_exact}
|
||||
|
||||
def neq(self, field_name, value):
|
||||
"""
|
||||
不等于查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": value, "type": self.type_neq}
|
||||
|
||||
def greater(self, field_name, value):
|
||||
"""
|
||||
大于查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": value, "type": self.type_greater}
|
||||
|
||||
def less(self, field_name, value):
|
||||
"""
|
||||
大于查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": value, "type": self.type_less}
|
||||
|
||||
def vague(self, field_name, value: str):
|
||||
"""
|
||||
模糊查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": ('%' + value + '%'), "type": self.type_vague}
|
||||
|
||||
def left_vague(self, field_name, value: str):
|
||||
"""
|
||||
左模糊查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": ('%' + value), "type": self.type_vague}
|
||||
|
||||
def right_vague(self, field_name, value: str):
|
||||
"""
|
||||
左模糊查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": (value + '%'), "type": self.type_vague}
|
||||
|
||||
def contains(self, field_name, value: str):
|
||||
"""
|
||||
包含查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value and value != '':
|
||||
self.filter_field[field_name] = {"data": value, "type": self.type_contains}
|
||||
|
||||
def between(self, field_name, value1, value2):
|
||||
"""
|
||||
范围查询字段
|
||||
:param field_name: 模型字段名称
|
||||
:param value: 值
|
||||
"""
|
||||
if value1 and value2 and value1 != '' and value2 != '':
|
||||
self.filter_field[field_name] = {"data": [value1, value2], "type": self.type_between}
|
||||
|
||||
def get_filter(self, model: db.Model):
|
||||
"""
|
||||
获取过滤条件
|
||||
:param model: 模型字段名称
|
||||
"""
|
||||
for k, v in self.filter_field.items():
|
||||
if v.get("type") == self.type_vague:
|
||||
self.filter_list.append(getattr(model, k).like(v.get("data")))
|
||||
if v.get("type") == self.type_contains:
|
||||
self.filter_list.append(getattr(model, k).contains(v.get("data")))
|
||||
if v.get("type") == self.type_exact:
|
||||
self.filter_list.append(getattr(model, k) == v.get("data"))
|
||||
if v.get("type") == self.type_neq:
|
||||
self.filter_list.append(getattr(model, k) != v.get("data"))
|
||||
if v.get("type") == self.type_greater:
|
||||
self.filter_list.append(getattr(model, k) > v.get("data"))
|
||||
if v.get("type") == self.type_less:
|
||||
self.filter_list.append(getattr(model, k) < v.get("data"))
|
||||
if v.get("type") == self.type_between:
|
||||
self.filter_list.append(getattr(model, k).between(v.get("data")[0], v.get("data")[1]))
|
||||
return and_(*self.filter_list)
|
||||
@@ -1,26 +0,0 @@
|
||||
import os
|
||||
|
||||
import click
|
||||
|
||||
from applications.common.script.initdb import init_db
|
||||
from applications.common.script.newmodular.new import NewViewModular
|
||||
|
||||
|
||||
def init_script(app):
|
||||
@app.cli.command()
|
||||
def init():
|
||||
init_db()
|
||||
|
||||
@app.cli.command()
|
||||
@click.option('--type', prompt="请输入类型", help='新增的类型')
|
||||
@click.option('--name', prompt="请输入新增的名称", help='新增的名称')
|
||||
def new(type,name):
|
||||
if type == 'view':
|
||||
if name.count('/') > 1:
|
||||
print("目前只支持二级目录,多级目录需要蓝图嵌套,本命令暂不支持,请手动创建")
|
||||
quit()
|
||||
if type == "view" and os.path.exists(f"applications/view/{name}.py"):
|
||||
print(f'已经存在视图模块{name}.py')
|
||||
quit()
|
||||
NewViewModular(name=name).new_view()
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
from dotenv import dotenv_values
|
||||
import sqlparse
|
||||
import pymysql
|
||||
config = dotenv_values('.flaskenv')
|
||||
# MySql配置信息
|
||||
HOST = config.get('MYSQL_HOST') or '127.0.0.1'
|
||||
PORT = config.get('MYSQL_PORT') or 3306
|
||||
DATABASE = config.get('MYSQL_DATABASE') or 'PearAdminFlask'
|
||||
USERNAME = config.get('MYSQL_USERNAME') or 'root'
|
||||
PASSWORD = config.get('MYSQL_PASSWORD') or '123456'
|
||||
|
||||
|
||||
def is_exist_database():
|
||||
db = pymysql.connect(host=HOST, port=int(PORT), user=USERNAME, password=PASSWORD, charset='utf8mb4')
|
||||
cursor1 = db.cursor()
|
||||
sql = "select * from information_schema.SCHEMATA WHERE SCHEMA_NAME = '%s' ; " % DATABASE
|
||||
res = cursor1.execute(sql)
|
||||
db.close()
|
||||
return res
|
||||
|
||||
|
||||
def init_database():
|
||||
db = pymysql.connect(host=HOST, port=int(PORT), user=USERNAME, password=PASSWORD, charset='utf8mb4')
|
||||
cursor1 = db.cursor()
|
||||
sql = "CREATE DATABASE IF NOT EXISTS %s CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;" % DATABASE
|
||||
res = cursor1.execute(sql)
|
||||
db.close()
|
||||
return res
|
||||
|
||||
|
||||
def execute_fromfile(filename):
|
||||
db = pymysql.connect(host=HOST, port=int(PORT), user=USERNAME, password=PASSWORD, database=DATABASE,
|
||||
charset='utf8mb4')
|
||||
fd = open(filename, 'r', encoding='utf-8')
|
||||
cursor = db.cursor()
|
||||
sqlfile = fd.read()
|
||||
sqlfile = sqlparse.format(sqlfile, strip_comments=True).strip()
|
||||
|
||||
sqlcommamds = sqlfile.split(';')
|
||||
|
||||
for command in sqlcommamds:
|
||||
try:
|
||||
cursor.execute(command)
|
||||
db.commit()
|
||||
|
||||
except Exception as msg:
|
||||
|
||||
db.rollback()
|
||||
db.close()
|
||||
|
||||
|
||||
def init_db():
|
||||
if is_exist_database():
|
||||
print('数据库已经存在,为防止误初始化,请手动删除 %s 数据库' % str(DATABASE))
|
||||
return
|
||||
if init_database():
|
||||
print('数据库%s创建成功' % str(DATABASE))
|
||||
execute_fromfile('test/pear.sql')
|
||||
print('表创建成功')
|
||||
print('欢迎使用pear-admin-flask,请使用 flask run 命令启动程序')
|
||||
@@ -1,92 +0,0 @@
|
||||
import os
|
||||
|
||||
import jinja2
|
||||
|
||||
|
||||
class NewViewModular():
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def path_is_exists(self):
|
||||
return os.path.exists(f"applications/view/{self.name.split('/')[0]}")
|
||||
|
||||
def new_dirs(self):
|
||||
if not self.path_is_exists():
|
||||
# 如果不存在则创建目录
|
||||
|
||||
# 创建目录操作函数
|
||||
os.makedirs(f"applications/view/{self.name.split('/')[0]}")
|
||||
|
||||
print(self.name + ' 创建成功')
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def add_view(self):
|
||||
# jinja渲染
|
||||
templateLoader = jinja2.FileSystemLoader(searchpath='applications/common/script/newmodular/template/')
|
||||
templateEnv = jinja2.Environment(loader=templateLoader)
|
||||
TEMPLATE_FILE = "view"
|
||||
template = templateEnv.get_template(TEMPLATE_FILE, )
|
||||
templateVars = {"name": self.name}
|
||||
outputText = template.render(templateVars)
|
||||
with open(f"applications/view/{self.name}.py", "a", encoding='utf-8') as f:
|
||||
f.write(outputText)
|
||||
f.close()
|
||||
|
||||
def add_init(self):
|
||||
# 判断__init__.py
|
||||
if os.path.exists(f"applications/view/{self.name.split('/')[0]}/__init__.py"):
|
||||
with open(f"applications/view/{self.name.split('/')[0]}/__init__.py", "r") as file:
|
||||
lines = file.readlines()
|
||||
print(lines)
|
||||
file.close()
|
||||
import_line_num = 0
|
||||
for line in lines:
|
||||
if 'def' in line:
|
||||
import_line_num = lines.index(line) - 2
|
||||
with open(f"applications/view/{self.name.split('/')[0]}/__init__.py", "w") as file:
|
||||
lines.insert(import_line_num,
|
||||
f"from applications.view.{self.name.replace('/', '.')} import {self.name.replace('/', '_')}\n")
|
||||
print(lines)
|
||||
lines.insert(len(lines) - 1, f" app.register_blueprint({self.name.replace('/', '_')})\n")
|
||||
print(lines)
|
||||
file.writelines(lines)
|
||||
file.close()
|
||||
return True
|
||||
else:
|
||||
templateLoader = jinja2.FileSystemLoader(searchpath='applications/common/script/newmodular/template/')
|
||||
templateEnv = jinja2.Environment(loader=templateLoader)
|
||||
TEMPLATE_FILE = "__init__"
|
||||
template = templateEnv.get_template(TEMPLATE_FILE, )
|
||||
templateVars = {"name": self.name}
|
||||
output = template.render(templateVars)
|
||||
with open(f"applications/view/{self.name.split('/')[0]}/__init__.py", "a", encoding='utf-8') as f:
|
||||
f.write(output)
|
||||
f.close()
|
||||
self.add_root_init()
|
||||
return True
|
||||
|
||||
def add_root_init(self):
|
||||
with open(f"applications/view/__init__.py", "r") as file:
|
||||
lines = file.readlines()
|
||||
file.close()
|
||||
import_line_num = 0
|
||||
for line in lines:
|
||||
if 'def' in line:
|
||||
import_line_num = lines.index(line) - 2
|
||||
with open(f"applications/view/__init__.py", "w") as file:
|
||||
lines.insert(
|
||||
import_line_num,
|
||||
f"from applications.view.{self.name.split('/')[0]} import register_{self.name.split('/')[0]}_views\n")
|
||||
print(lines)
|
||||
lines.insert(len(lines) - 1, f" register_{self.name.split('/')[0]}_views(app)\n")
|
||||
print(lines)
|
||||
file.writelines(lines)
|
||||
file.close()
|
||||
|
||||
def new_view(self):
|
||||
self.new_dirs()
|
||||
self.add_view()
|
||||
self.add_init()
|
||||
@@ -1,6 +0,0 @@
|
||||
from flask import Flask
|
||||
from applications.view.{{name.replace('/','.')}} import {{name.replace('/','_')}}
|
||||
|
||||
|
||||
def register_{{name.split('/')[0]}}_views(app: Flask):
|
||||
app.register_blueprint({{name.replace('/','_')}})
|
||||
@@ -1,8 +0,0 @@
|
||||
from flask import Blueprint
|
||||
|
||||
{{name.replace('/','_')}} = Blueprint('{{name.replace('/','_')}}', __name__, url_prefix='/{{name}}')
|
||||
|
||||
|
||||
@{{name.replace('/','_')}}.route('/')
|
||||
def index():
|
||||
return "这是{{name}}路由"
|
||||
@@ -1,54 +0,0 @@
|
||||
from apscheduler.events import (
|
||||
EVENT_JOB_ADDED,
|
||||
EVENT_JOB_ERROR,
|
||||
EVENT_JOB_EXECUTED,
|
||||
EVENT_JOB_MISSED,
|
||||
EVENT_JOB_REMOVED,
|
||||
EVENT_JOB_SUBMITTED,
|
||||
)
|
||||
|
||||
from applications.extensions.init_apscheduler import scheduler
|
||||
|
||||
|
||||
def job_missed(event):
|
||||
"""Job missed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_error(event):
|
||||
"""Job error event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_executed(event):
|
||||
"""Job executed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_added(event):
|
||||
"""Job added event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_removed(event):
|
||||
"""Job removed event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
def job_submitted(event):
|
||||
"""Job scheduled to run event."""
|
||||
with scheduler.app.app_context():
|
||||
print(event) # noqa: T001
|
||||
|
||||
|
||||
scheduler.add_listener(job_missed, EVENT_JOB_MISSED)
|
||||
scheduler.add_listener(job_error, EVENT_JOB_ERROR)
|
||||
scheduler.add_listener(job_executed, EVENT_JOB_EXECUTED)
|
||||
scheduler.add_listener(job_added, EVENT_JOB_ADDED)
|
||||
scheduler.add_listener(job_removed, EVENT_JOB_REMOVED)
|
||||
scheduler.add_listener(job_submitted, EVENT_JOB_SUBMITTED)
|
||||
@@ -1,15 +0,0 @@
|
||||
import datetime
|
||||
|
||||
task_list = ['task2', 'task3', 'task4']
|
||||
|
||||
|
||||
def task2(a, b):
|
||||
print(f'定时任务_1_{a},{b},{datetime.datetime.now()}')
|
||||
|
||||
|
||||
def task3(a, b):
|
||||
print(f'定时任务_2_{a}{b}{datetime.datetime.now()}')
|
||||
|
||||
|
||||
def task4(a, b):
|
||||
print(f'定时任务_4_{a}{b}{datetime.datetime.now()}')
|
||||
@@ -1,14 +0,0 @@
|
||||
from captcha.image import ImageCaptcha
|
||||
|
||||
from PIL import Image
|
||||
from random import choices
|
||||
|
||||
|
||||
def gen_captcha(content='2345689abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ'):
|
||||
""" 生成验证码 """
|
||||
image = ImageCaptcha()
|
||||
# 获取字符串
|
||||
captcha_text = "".join(choices(content, k=4)).lower()
|
||||
# 生成图像
|
||||
captcha_image = Image.open(image.generate(captcha_text))
|
||||
return captcha_text, captcha_image
|
||||
@@ -1,24 +0,0 @@
|
||||
from flask import jsonify
|
||||
|
||||
|
||||
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)
|
||||
@@ -1,25 +0,0 @@
|
||||
from functools import wraps
|
||||
from flask import abort, request, jsonify, session
|
||||
from flask_login import login_required
|
||||
from applications.common.admin_log import admin_log
|
||||
|
||||
|
||||
def authorize(power: str, log: bool = False):
|
||||
def decorator(func):
|
||||
@login_required
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if not power in session.get('permissions'):
|
||||
if log:
|
||||
admin_log(request=request, is_access=False)
|
||||
if request.method == 'GET':
|
||||
abort(403)
|
||||
else:
|
||||
return jsonify(success=False, msg="权限不足!")
|
||||
if log:
|
||||
admin_log(request=request, is_access=True)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
@@ -1,36 +0,0 @@
|
||||
import os
|
||||
from flask import current_app
|
||||
from sqlalchemy import desc
|
||||
from applications.extensions import db
|
||||
from applications.extensions.init_upload import photos
|
||||
from applications.models import Photo
|
||||
from applications.schemas import PhotoOutSchema
|
||||
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=PhotoOutSchema, data=photo.items)
|
||||
return data, count
|
||||
|
||||
|
||||
def upload_one(photo, mime):
|
||||
filename = photos.save(photo)
|
||||
file_url = '/_uploads/photos/'+filename
|
||||
# file_url = photos.url(filename)
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
size = os.path.getsize(upload_url + '/' + filename)
|
||||
photo = Photo(name=filename, href=file_url, mime=mime, size=size)
|
||||
db.session.add(photo)
|
||||
db.session.commit()
|
||||
return file_url
|
||||
|
||||
|
||||
def delete_photo_by_id(_id):
|
||||
photo_name = Photo.query.filter_by(id=_id).first().name
|
||||
photo = Photo.query.filter_by(id=_id).delete()
|
||||
db.session.commit()
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
os.remove(upload_url + '/' + photo_name)
|
||||
return photo
|
||||
@@ -1,20 +0,0 @@
|
||||
# xss过滤
|
||||
from flask import abort, make_response, jsonify
|
||||
|
||||
|
||||
def xss_escape(s: str):
|
||||
if s is None:
|
||||
return None
|
||||
else:
|
||||
return s.replace("&", "&").replace(">", ">").replace("<", "<").replace("'", "'").replace('"',
|
||||
""")
|
||||
|
||||
|
||||
def check_data(schema, data):
|
||||
errors = schema.validate(data)
|
||||
for k, v in errors.items():
|
||||
for i in v:
|
||||
# print("{}{}".format(k, i))
|
||||
msg = "{}{}".format(k, i)
|
||||
if errors:
|
||||
abort(make_response(jsonify(result=False, msg=msg), 200))
|
||||
@@ -1 +0,0 @@
|
||||
from .config import config
|
||||
@@ -1,87 +0,0 @@
|
||||
import logging
|
||||
import os
|
||||
from urllib.parse import quote_plus as urlquote
|
||||
|
||||
|
||||
class BaseConfig:
|
||||
|
||||
SYSTEM_NAME = os.getenv('SYSTEM_NAME', 'Pear Admin')
|
||||
# 主题面板的链接列表配置
|
||||
SYSTEM_PANEL_LINKS = [
|
||||
{
|
||||
"icon": "layui-icon layui-icon-auz",
|
||||
"title": "官方网站",
|
||||
"href": "http://www.pearadmin.com"
|
||||
},
|
||||
{
|
||||
"icon": "layui-icon layui-icon-auz",
|
||||
"title": "开发文档",
|
||||
"href": "http://www.pearadmin.com"
|
||||
},
|
||||
{
|
||||
"icon": "layui-icon layui-icon-auz",
|
||||
"title": "开源地址",
|
||||
"href": "https://gitee.com/Jmysy/Pear-Admin-Layui"
|
||||
}
|
||||
]
|
||||
|
||||
UPLOADED_PHOTOS_DEST = 'static/upload'
|
||||
UPLOADED_FILES_ALLOW = ['gif', 'jpg']
|
||||
|
||||
# JSON配置
|
||||
JSON_AS_ASCII = False
|
||||
|
||||
SECRET_KEY = os.getenv('SECRET_KEY', 'dev key')
|
||||
|
||||
# redis配置
|
||||
REDIS_HOST = os.getenv('REDIS_HOST') or "127.0.0.1"
|
||||
REDIS_PORT = int(os.getenv('REDIS_PORT') or 6379)
|
||||
|
||||
# mysql 配置
|
||||
MYSQL_USERNAME = os.getenv('MYSQL_USERNAME') or "root"
|
||||
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD') or "123456"
|
||||
MYSQL_HOST = os.getenv('MYSQL_HOST') or "127.0.0.1"
|
||||
MYSQL_PORT = int(os.getenv('MYSQL_PORT') or 3306)
|
||||
MYSQL_DATABASE = os.getenv('MYSQL_DATABASE') or "PearAdminFlask"
|
||||
|
||||
# mysql 数据库的配置信息
|
||||
SQLALCHEMY_DATABASE_URI = f"mysql+pymysql://{MYSQL_USERNAME}:{urlquote(MYSQL_PASSWORD)}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}?charset=utf8mb4"
|
||||
|
||||
# 默认日志等级
|
||||
LOG_LEVEL = logging.WARN
|
||||
#
|
||||
MAIL_SERVER = os.getenv('MAIL_SERVER') or 'smtp.qq.com'
|
||||
MAIL_USE_TLS = False
|
||||
MAIL_USE_SSL = True
|
||||
MAIL_PORT = 465
|
||||
MAIL_USERNAME = os.getenv('MAIL_USERNAME') or '123@qq.com'
|
||||
MAIL_PASSWORD = os.getenv('MAIL_PASSWORD') or 'XXXXX' # 生成的授权码
|
||||
# 默认发件人的邮箱,这里填写和MAIL_USERNAME一致即可
|
||||
MAIL_DEFAULT_SENDER = ('pear admin', os.getenv('MAIL_USERNAME') or '123@qq.com')
|
||||
|
||||
|
||||
class TestingConfig(BaseConfig):
|
||||
""" 测试配置 """
|
||||
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' # 内存数据库
|
||||
|
||||
|
||||
class DevelopmentConfig(BaseConfig):
|
||||
""" 开发配置 """
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
||||
SQLALCHEMY_ECHO = False
|
||||
|
||||
|
||||
class ProductionConfig(BaseConfig):
|
||||
"""生成环境配置"""
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
SQLALCHEMY_ECHO = False
|
||||
SQLALCHEMY_POOL_RECYCLE = 8
|
||||
|
||||
LOG_LEVEL = logging.ERROR
|
||||
|
||||
|
||||
config = {
|
||||
'development': DevelopmentConfig,
|
||||
'testing': TestingConfig,
|
||||
'production': ProductionConfig
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
from flask import Flask
|
||||
|
||||
from .init_sqlalchemy import db, ma, init_databases
|
||||
from .init_login import init_login_manager
|
||||
from .init_debug_tool import init_debug_tool
|
||||
from .init_template_directives import init_template_directives
|
||||
from .init_error_views import init_error_views
|
||||
from .init_mail import init_mail
|
||||
from .init_apscheduler import init_scheduler
|
||||
from .init_upload import init_upload
|
||||
from .init_dotenv import init_dotenv
|
||||
|
||||
|
||||
def init_plugs(app: Flask) -> None:
|
||||
# init_debug_tool(app)
|
||||
init_login_manager(app)
|
||||
init_databases(app)
|
||||
init_template_directives(app)
|
||||
init_error_views(app)
|
||||
init_mail(app)
|
||||
init_scheduler(app)
|
||||
init_upload(app)
|
||||
init_dotenv()
|
||||
@@ -1,12 +0,0 @@
|
||||
from flask import Flask
|
||||
from flask_apscheduler import APScheduler
|
||||
|
||||
scheduler = APScheduler()
|
||||
|
||||
|
||||
def init_scheduler(app: Flask):
|
||||
scheduler.init_app(app)
|
||||
with app.app_context():
|
||||
from applications.common.tasks import tasks
|
||||
scheduler.start()
|
||||
from applications.common.tasks import events
|
||||
@@ -1,7 +0,0 @@
|
||||
from flask import Flask
|
||||
from flask_debugtoolbar import DebugToolbarExtension
|
||||
|
||||
|
||||
def init_debug_tool(app: Flask):
|
||||
toolbar = DebugToolbarExtension()
|
||||
toolbar.init_app(app)
|
||||
@@ -1,14 +0,0 @@
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
root_path = os.path.abspath(os.path.dirname(__file__)).split('applications')[0]
|
||||
# dot_env_path = os.path.join(root_path, '.env')
|
||||
flask_env_path = os.path.join(root_path, '.flaskenv')
|
||||
|
||||
|
||||
# if os.path.exists(dot_env_path):
|
||||
# load_dotenv(dot_env_path)
|
||||
|
||||
def init_dotenv():
|
||||
if os.path.exists(flask_env_path):
|
||||
load_dotenv(flask_env_path)
|
||||
@@ -1,34 +0,0 @@
|
||||
from flask import render_template, jsonify
|
||||
|
||||
|
||||
def init_error_views(app):
|
||||
@app.errorhandler(403)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/403.html'), 403
|
||||
|
||||
@app.errorhandler(404)
|
||||
def page_not_found(e):
|
||||
return render_template('errors/404.html'), 404
|
||||
|
||||
@app.errorhandler(500)
|
||||
def internal_server_error(e):
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
# Return validation errors as JSON
|
||||
@app.errorhandler(422)
|
||||
@app.errorhandler(400)
|
||||
def handle_error(err):
|
||||
headers = err.data.get("headers", None)
|
||||
messages = err.data.get("messages", ["Invalid request."]).get('json')
|
||||
print(err.data.get("messages"))
|
||||
print(messages.items())
|
||||
msg = ''
|
||||
|
||||
for i in messages.items():
|
||||
msg = str(i[0]) + str(i[1][0])
|
||||
break
|
||||
|
||||
if headers:
|
||||
return jsonify({"success": False, "msg": msg})
|
||||
else:
|
||||
return jsonify({"success": False, "msg": msg})
|
||||
@@ -1,15 +0,0 @@
|
||||
from flask_login import LoginManager
|
||||
|
||||
|
||||
def init_login_manager(app):
|
||||
login_manager = LoginManager()
|
||||
login_manager.init_app(app)
|
||||
|
||||
login_manager.login_view = 'passport.login'
|
||||
login_manager.login_message = u'请登录以访问此页面'
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
from applications.models import User
|
||||
user = User.query.get(int(user_id))
|
||||
return user
|
||||
@@ -1,8 +0,0 @@
|
||||
from flask import Flask
|
||||
from flask_mail import Mail
|
||||
|
||||
mail = Mail()
|
||||
|
||||
|
||||
def init_mail(app: Flask):
|
||||
mail.init_app(app)
|
||||
@@ -1,93 +0,0 @@
|
||||
import datetime
|
||||
|
||||
from flask import Flask, request
|
||||
from flask_sqlalchemy import SQLAlchemy, BaseQuery
|
||||
from flask_marshmallow import Marshmallow
|
||||
from marshmallow import fields
|
||||
from marshmallow.validate import (
|
||||
URL, Email, Range, Length, Equal, Regexp,
|
||||
Predicate, NoneOf, OneOf, ContainsOnly
|
||||
)
|
||||
|
||||
URL.default_message = '无效的链接'
|
||||
Email.default_message = '无效的邮箱地址'
|
||||
Range.message_min = '不能小于{min}'
|
||||
Range.message_max = '不能小于{max}'
|
||||
Range.message_all = '不能超过{min}和{max}这个范围'
|
||||
Length.message_min = '长度不得小于{min}位'
|
||||
Length.message_max = '长度不得大于{max}位'
|
||||
Length.message_all = '长度不能超过{min}和{max}这个范围'
|
||||
Length.message_equal = '长度必须等于{equal}位'
|
||||
Equal.default_message = '必须等于{other}'
|
||||
Regexp.default_message = '非法输入'
|
||||
Predicate.default_message = '非法输入'
|
||||
NoneOf.default_message = '非法输入'
|
||||
OneOf.default_message = '无效的选择'
|
||||
ContainsOnly.default_message = '一个或多个无效的选择'
|
||||
|
||||
fields.Field.default_error_messages = {
|
||||
"required": "缺少必要数据",
|
||||
"null": "数据不能为空",
|
||||
"validator_failed": "非法数据",
|
||||
}
|
||||
|
||||
fields.Str.default_error_messages = {
|
||||
'invalid': "不是合法文本"
|
||||
}
|
||||
|
||||
fields.Int.default_error_messages = {
|
||||
"invalid": "不是合法整数"
|
||||
}
|
||||
|
||||
fields.Number.default_error_messages = {
|
||||
"invalid": "不是合法数字"
|
||||
}
|
||||
|
||||
fields.Boolean.default_error_messages = {
|
||||
"invalid": "不是合法布尔值"
|
||||
}
|
||||
|
||||
|
||||
class Query(BaseQuery):
|
||||
def soft_delete(self):
|
||||
return self.update({"delete_at": datetime.datetime.now()})
|
||||
|
||||
def logic_all(self):
|
||||
return self.filter_by(delete_at=None).all()
|
||||
|
||||
def all_json(self, schema: Marshmallow().Schema):
|
||||
return schema(many=True).dump(self.all())
|
||||
|
||||
def layui_paginate(self):
|
||||
return self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
|
||||
def layui_paginate_json(self, schema: Marshmallow().Schema):
|
||||
"""
|
||||
返回dict
|
||||
"""
|
||||
_res = self.paginate(
|
||||
page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False
|
||||
)
|
||||
return schema(many=True).dump(_res.items), _res.total, _res.page, _res.per_page
|
||||
|
||||
def layui_paginate_db_json(self):
|
||||
"""
|
||||
db.query(A.name).layui_paginate_db_json()
|
||||
"""
|
||||
_res = self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
return [dict(i) for i in _res.items], _res.total
|
||||
|
||||
|
||||
db = SQLAlchemy(query_class=Query)
|
||||
ma = Marshmallow()
|
||||
|
||||
|
||||
def init_databases(app: Flask):
|
||||
db.init_app(app)
|
||||
ma.init_app(app)
|
||||
@@ -1,7 +0,0 @@
|
||||
from flask import session
|
||||
|
||||
|
||||
def init_template_directives(app):
|
||||
@app.template_global()
|
||||
def authorize(power):
|
||||
return bool(power in session.get('permissions'))
|
||||
@@ -1,9 +0,0 @@
|
||||
from flask import Flask
|
||||
from flask_uploads import configure_uploads
|
||||
from flask_uploads import UploadSet, IMAGES
|
||||
|
||||
photos = UploadSet('photos', IMAGES)
|
||||
|
||||
|
||||
def init_upload(app: Flask):
|
||||
configure_uploads(app, photos)
|
||||
@@ -1,9 +0,0 @@
|
||||
from marshmallow import EXCLUDE
|
||||
from webargs.flaskparser import FlaskParser
|
||||
|
||||
|
||||
class Parser(FlaskParser):
|
||||
DEFAULT_UNKNOWN_BY_LOCATION = {"query": EXCLUDE}
|
||||
|
||||
|
||||
parser = Parser()
|
||||
@@ -1,9 +0,0 @@
|
||||
from .admin_dept import Dept
|
||||
from .admin_dict import DictType, DictData
|
||||
from .admin_log import AdminLog
|
||||
from .admin_photo import Photo
|
||||
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
|
||||
@@ -1,18 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
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='创建时间')
|
||||
@@ -1,26 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class DictType(db.Model):
|
||||
__tablename__ = 'admin_dict_type'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
type_name = db.Column(db.String(255), comment='字典类型名称')
|
||||
type_code = db.Column(db.String(255), comment='字典类型标识')
|
||||
description = db.Column(db.String(255), comment='字典类型描述')
|
||||
enable = db.Column(db.Integer, comment='是否开启')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
|
||||
|
||||
class DictData(db.Model):
|
||||
__tablename__ = 'admin_dict_data'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
data_label = db.Column(db.String(255), comment='字典类型名称')
|
||||
data_value = db.Column(db.String(255), comment='字典类型标识')
|
||||
type_code = db.Column(db.String(255), comment='字典类型描述')
|
||||
is_default = db.Column(db.Integer, comment='是否默认')
|
||||
enable = db.Column(db.Integer, comment='是否开启')
|
||||
remark = db.Column(db.String(255), comment='备注')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
@@ -1,14 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
class AdminLog(db.Model):
|
||||
__tablename__ = 'admin_admin_log'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
method = db.Column(db.String(10))
|
||||
uid = db.Column(db.Integer)
|
||||
url = db.Column(db.String(255))
|
||||
desc = db.Column(db.Text)
|
||||
ip = db.Column(db.String(255))
|
||||
success = db.Column(db.Integer)
|
||||
user_agent = db.Column(db.Text)
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now)
|
||||
@@ -1,12 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class Photo(db.Model):
|
||||
__tablename__ = 'admin_photo'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), nullable=False)
|
||||
href = db.Column(db.String(255))
|
||||
mime = db.Column(db.CHAR(50), nullable=False)
|
||||
size = db.Column(db.CHAR(30), nullable=False)
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now)
|
||||
@@ -1,18 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class Power(db.Model):
|
||||
__tablename__ = 'admin_power'
|
||||
id = db.Column(db.Integer, primary_key=True, comment='权限编号')
|
||||
name = db.Column(db.String(255), comment='权限名称')
|
||||
type = db.Column(db.String(1), comment='权限类型')
|
||||
code = db.Column(db.String(30), comment='权限标识')
|
||||
url = db.Column(db.String(255), comment='权限路径')
|
||||
open_type = db.Column(db.String(10), comment='打开方式')
|
||||
parent_id = db.Column(db.Integer, comment='父类编号')
|
||||
icon = db.Column(db.String(128), comment='图标')
|
||||
sort = db.Column(db.Integer, comment='排序')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
enable = db.Column(db.Integer, comment='是否开启')
|
||||
@@ -1,16 +0,0 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class Role(db.Model):
|
||||
__tablename__ = 'admin_role'
|
||||
id = db.Column(db.Integer, primary_key=True, comment='角色ID')
|
||||
name = db.Column(db.String(255), comment='角色名称')
|
||||
code = db.Column(db.String(255), comment='角色标识')
|
||||
enable = db.Column(db.Integer, comment='是否启用')
|
||||
remark = db.Column(db.String(255), comment='备注')
|
||||
details = db.Column(db.String(255), comment='详情')
|
||||
sort = db.Column(db.Integer, comment='排序')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
power = db.relationship('Power', secondary="admin_role_power", backref=db.backref('role'))
|
||||
@@ -1,9 +0,0 @@
|
||||
from applications.extensions import db
|
||||
|
||||
# 创建中间表
|
||||
role_power = db.Table(
|
||||
"admin_role_power", # 中间表名称
|
||||
db.Column("id", db.Integer, primary_key=True, autoincrement=True, comment='标识'), # 主键
|
||||
db.Column("power_id", db.Integer, db.ForeignKey("admin_power.id"), comment='用户编号'), # 属性 外键
|
||||
db.Column("role_id", db.Integer, db.ForeignKey("admin_role.id"), comment='角色编号'), # 属性 外键
|
||||
)
|
||||
@@ -1,25 +0,0 @@
|
||||
import datetime
|
||||
from flask_login import UserMixin
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
__tablename__ = 'admin_user'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
|
||||
username = db.Column(db.String(20), comment='用户名')
|
||||
realname = db.Column(db.String(20), comment='真实名字')
|
||||
avatar = db.Column(db.String(255), comment='头像', default="/static/admin/admin/images/avatar.jpg")
|
||||
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')
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
def validate_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
@@ -1,9 +0,0 @@
|
||||
from applications.extensions import db
|
||||
|
||||
# 创建中间表
|
||||
user_role = db.Table(
|
||||
"admin_user_role", # 中间表名称
|
||||
db.Column("id", db.Integer, primary_key=True, autoincrement=True, comment='标识'), # 主键
|
||||
db.Column("user_id", db.Integer, db.ForeignKey("admin_user.id"), comment='用户编号'), # 属性 外键
|
||||
db.Column("role_id", db.Integer, db.ForeignKey("admin_role.id"), comment='角色编号'), # 属性 外键
|
||||
)
|
||||
@@ -1,7 +0,0 @@
|
||||
from .admin_user import UserOutSchema
|
||||
from .admin_role import RoleOutSchema
|
||||
from .admin_power import PowerOutSchema, PowerOutSchema2
|
||||
from .admin_dict import DictDataOutSchema, DictTypeOutSchema
|
||||
from .admin_dept import DeptOutSchema
|
||||
from .admin_log import LogOutSchema
|
||||
from .admin_photo import PhotoOutSchema
|
||||
@@ -1,25 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields, validate
|
||||
|
||||
|
||||
class DeptInSchema(ma.Schema):
|
||||
parentId = fields.Integer(required=True)
|
||||
deptName = fields.Str(required=True)
|
||||
leader = fields.Str(required=True)
|
||||
phone = fields.Str(required=True)
|
||||
email = fields.Str(validate=validate.Email())
|
||||
address = fields.Str()
|
||||
status = fields.Str(validate=validate.OneOf(["0", "1"]))
|
||||
sort = fields.Integer()
|
||||
|
||||
|
||||
class DeptOutSchema(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(validate=validate.Email())
|
||||
address = fields.Str()
|
||||
status = fields.Str(validate=validate.OneOf(["0", "1"]))
|
||||
sort = fields.Integer()
|
||||
@@ -1,21 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
class DictTypeOutSchema(ma.Schema):
|
||||
id = fields.Str(attribute="id")
|
||||
typeName = fields.Str(attribute="type_name")
|
||||
typeCode = fields.Str(attribute="type_code")
|
||||
description = fields.Str(attribute="description")
|
||||
createTime = fields.Str(attribute="create_time")
|
||||
updateName = fields.Str(attribute="update_time")
|
||||
remark = fields.Str()
|
||||
enable = fields.Str()
|
||||
|
||||
|
||||
class DictDataOutSchema(ma.Schema):
|
||||
dataId = fields.Str(attribute="id")
|
||||
dataLabel = fields.Str(attribute="data_label")
|
||||
dataValue = fields.Str(attribute="data_value")
|
||||
remark = fields.Str()
|
||||
enable = fields.Str()
|
||||
@@ -1,14 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
class LogOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
method = fields.Str()
|
||||
uid = fields.Str()
|
||||
url = fields.Str()
|
||||
desc = fields.Str()
|
||||
ip = fields.Str()
|
||||
user_agent = fields.Str()
|
||||
success = fields.Bool()
|
||||
create_time = fields.DateTime()
|
||||
@@ -1,12 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
class PhotoOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
name = fields.Str()
|
||||
href = fields.Str()
|
||||
mime = fields.Str()
|
||||
size = fields.Str()
|
||||
ext = fields.Str()
|
||||
create_time = fields.DateTime()
|
||||
@@ -1,32 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
# 权限models序列化类
|
||||
class PowerOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
title = fields.Str(attribute="name")
|
||||
type = fields.Str()
|
||||
code = fields.Str()
|
||||
href = fields.Str(attribute="url")
|
||||
openType = fields.Str(attribute="open_type")
|
||||
parent_id = fields.Integer()
|
||||
icon = fields.Str()
|
||||
sort = fields.Integer()
|
||||
create_time = fields.DateTime()
|
||||
update_time = fields.DateTime()
|
||||
enable = fields.Integer()
|
||||
|
||||
|
||||
class PowerOutSchema2(ma.Schema): # 序列化类
|
||||
powerId = fields.Str(attribute="id")
|
||||
powerName = fields.Str(attribute="name")
|
||||
powerType = fields.Str(attribute="type")
|
||||
powerUrl = fields.Str(attribute="url")
|
||||
openType = fields.Str(attribute="open_type")
|
||||
parentId = fields.Str(attribute="parent_id")
|
||||
icon = fields.Str()
|
||||
sort = fields.Integer()
|
||||
create_time = fields.DateTime()
|
||||
update_time = fields.DateTime()
|
||||
enable = fields.Integer()
|
||||
@@ -1,14 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
|
||||
|
||||
class RoleOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
roleName = fields.Str(attribute="name")
|
||||
roleCode = fields.Str(attribute="code")
|
||||
enable = fields.Str()
|
||||
remark = fields.Str()
|
||||
details = fields.Str()
|
||||
sort = fields.Integer()
|
||||
create_at = fields.DateTime()
|
||||
update_at = fields.DateTime()
|
||||
@@ -1,20 +0,0 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
from applications.models import Dept
|
||||
|
||||
|
||||
# 用户models的序列化类
|
||||
class UserOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
username = fields.Str()
|
||||
realname = fields.Str()
|
||||
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
|
||||
@@ -1,13 +0,0 @@
|
||||
from applications.view.admin import register_admin_views
|
||||
from applications.view.index import register_index_views
|
||||
from applications.view.passport import register_passport_views
|
||||
from applications.view.rights import register_rights_view
|
||||
from applications.view.department import register_dept_views
|
||||
|
||||
|
||||
def init_view(app):
|
||||
register_admin_views(app)
|
||||
register_index_views(app)
|
||||
register_rights_view(app)
|
||||
register_passport_views(app)
|
||||
register_dept_views(app)
|
||||
@@ -1,23 +0,0 @@
|
||||
from flask import Flask
|
||||
|
||||
from applications.view.admin.admin_log import admin_log
|
||||
from applications.view.admin.dict import admin_dict
|
||||
from applications.view.admin.index import admin_bp
|
||||
from applications.view.admin.file import admin_file
|
||||
from applications.view.admin.power import admin_power
|
||||
from applications.view.admin.role import admin_role
|
||||
from applications.view.admin.user import admin_user
|
||||
from applications.view.admin.monitor import admin_monitor_bp
|
||||
from applications.view.admin.task import admin_task
|
||||
|
||||
|
||||
def register_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_bp)
|
||||
app.register_blueprint(admin_user)
|
||||
app.register_blueprint(admin_file)
|
||||
app.register_blueprint(admin_monitor_bp)
|
||||
app.register_blueprint(admin_log)
|
||||
app.register_blueprint(admin_power)
|
||||
app.register_blueprint(admin_role)
|
||||
app.register_blueprint(admin_dict)
|
||||
app.register_blueprint(admin_task)
|
||||
@@ -1,40 +0,0 @@
|
||||
from flask import Blueprint, request, render_template
|
||||
from sqlalchemy import desc
|
||||
from applications.common.utils.http import table_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.models import AdminLog
|
||||
from applications.schemas import LogOutSchema
|
||||
from applications.common.curd import model_to_dicts
|
||||
|
||||
admin_log = Blueprint('adminLog', __name__, url_prefix='/admin/log')
|
||||
|
||||
|
||||
# 日志管理
|
||||
@admin_log.get('/')
|
||||
@authorize("admin:log:main")
|
||||
def index():
|
||||
return render_template('admin/admin_log/main.html')
|
||||
|
||||
|
||||
# 登录日志
|
||||
@admin_log.get('/loginLog')
|
||||
@authorize("admin:log:main")
|
||||
def login_log():
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
log = AdminLog.query.filter_by(url='/passport/login').order_by(desc(AdminLog.create_time)).layui_paginate()
|
||||
count = log.total
|
||||
return table_api(data= model_to_dicts(schema=LogOutSchema, data=log.items), count=count)
|
||||
|
||||
|
||||
# 操作日志
|
||||
@admin_log.get('/operateLog')
|
||||
@authorize("admin:log:main")
|
||||
def operate_log():
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
log = AdminLog.query.filter(
|
||||
AdminLog.url != '/passport/login').order_by(
|
||||
desc(AdminLog.create_time)).layui_paginate()
|
||||
count = log.total
|
||||
return table_api(data=model_to_dicts(schema=LogOutSchema, data=log.items), count=count)
|
||||
@@ -1,221 +0,0 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
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 xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import DictType, DictData
|
||||
from applications.schemas import DictTypeOutSchema, DictDataOutSchema
|
||||
|
||||
admin_dict = Blueprint('adminDict', __name__, url_prefix='/admin/dict')
|
||||
|
||||
|
||||
# 数据字典
|
||||
@admin_dict.get('/')
|
||||
@authorize("admin:dict:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/dict/main.html')
|
||||
|
||||
|
||||
@admin_dict.get('/dictType/data')
|
||||
@authorize("admin:dict:main", log=True)
|
||||
def dict_type_data():
|
||||
# 获取请求参数
|
||||
type_name = xss_escape(request.args.get('typeName', type=str))
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if type_name:
|
||||
mf.vague(field_name="type_name", value=type_name)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
dict_all = DictType.query.filter(mf.get_filter(DictType)).layui_paginate()
|
||||
count = dict_all.total
|
||||
data = curd.model_to_dicts(schema=DictTypeOutSchema, data=dict_all.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
@admin_dict.get('/dictType/add')
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_type_add():
|
||||
return render_template('admin/dict/add.html')
|
||||
|
||||
|
||||
@admin_dict.post('/dictType/save')
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_type_save():
|
||||
req_json = request.json
|
||||
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="增加成功")
|
||||
|
||||
|
||||
# 编辑字典类型
|
||||
@admin_dict.get('/dictType/edit')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_type_edit():
|
||||
_id = request.args.get('dictTypeId', type=int)
|
||||
dict_type = DictType.query.filter_by(id=_id).first()
|
||||
return render_template('admin/dict/edit.html', dict_type=dict_type)
|
||||
|
||||
|
||||
# 编辑字典类型
|
||||
@admin_dict.put('/dictType/update')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_type_update():
|
||||
req_json = request.json
|
||||
id = xss_escape(req_json.get("id"))
|
||||
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"))
|
||||
DictType.query.filter_by(id=id).update({
|
||||
"description": description,
|
||||
"enable": enable,
|
||||
"type_code": type_code,
|
||||
"type_name": type_name
|
||||
})
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 启用字典
|
||||
@admin_dict.put('/dictType/enable')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_type_enable():
|
||||
_id = request.json.get('id')
|
||||
if id:
|
||||
res = curd.enable_status(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api("启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用字典
|
||||
@admin_dict.put('/dictType/disable')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_type_dis_enable():
|
||||
_id = request.json.get('id')
|
||||
if id:
|
||||
res = curd.disable_status(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api("禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 删除字典类型
|
||||
@admin_dict.delete('/dictType/remove/<int:_id>')
|
||||
@authorize("admin:dict:remove", log=True)
|
||||
def dict_type_delete(_id):
|
||||
res = curd.delete_one_by_id(DictType,_id)
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
@admin_dict.get('/dictData/data')
|
||||
@authorize("admin:dict:main", log=True)
|
||||
def dict_code_data():
|
||||
type_code = xss_escape(request.args.get('typeCode', type=str))
|
||||
dict_data = DictData.query.filter_by(type_code=type_code).layui_paginate()
|
||||
count = dict_data.total
|
||||
data = curd.model_to_dicts(schema=DictDataOutSchema, data=dict_data.items)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
# 增加字典数据
|
||||
@admin_dict.get('/dictData/add')
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_data_add():
|
||||
type_code = request.args.get('typeCode', type=str)
|
||||
return render_template('admin/dict/data/add.html', type_code=type_code)
|
||||
|
||||
|
||||
# 增加字典数据
|
||||
@admin_dict.post('/dictData/save')
|
||||
@authorize("admin:dict:add", log=True)
|
||||
def dict_data_save():
|
||||
req_json = request.json
|
||||
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="增加成功")
|
||||
|
||||
|
||||
# 编辑字典数据
|
||||
@admin_dict.get('/dictData/edit')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_edit():
|
||||
_id = request.args.get('dataId', type=str)
|
||||
dict_data = curd.get_one_by_id(DictData, _id)
|
||||
return render_template('admin/dict/data/edit.html', dict_data=dict_data)
|
||||
|
||||
|
||||
# 编辑字典数据
|
||||
@admin_dict.put('/dictData/update')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_update():
|
||||
req_json = request.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="更新成功")
|
||||
|
||||
|
||||
# 启用字典数据
|
||||
@admin_dict.put('/dictData/enable')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_enable():
|
||||
_id = request.json.get('dataId')
|
||||
if _id:
|
||||
res = curd.enable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用字典数据
|
||||
@admin_dict.put('/dictData/disable')
|
||||
@authorize("admin:dict:edit", log=True)
|
||||
def dict_data_disenable():
|
||||
_id = request.json.get('dataId')
|
||||
if _id:
|
||||
res = curd.disable_status(model=DictData, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 删除字典类型
|
||||
@admin_dict.delete('dictData/remove/<int:id>')
|
||||
@authorize("admin:dict:remove", log=True)
|
||||
def dict_data_delete(id):
|
||||
res = curd.delete_one_by_id(model=DictData, id=id)
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
@@ -1,83 +0,0 @@
|
||||
import os
|
||||
from flask import Blueprint, request, render_template, jsonify, current_app
|
||||
|
||||
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.common.utils import upload as upload_curd
|
||||
|
||||
admin_file = Blueprint('adminFile', __name__, url_prefix='/admin/file')
|
||||
|
||||
|
||||
# 图片管理
|
||||
@admin_file.get('/')
|
||||
@authorize("admin:file:main", log=True)
|
||||
def index():
|
||||
return render_template('admin/photo/photo.html')
|
||||
|
||||
|
||||
# 图片数据
|
||||
@admin_file.get('/table')
|
||||
@authorize("admin:file:main", log=True)
|
||||
def table():
|
||||
page = request.args.get('page', type=int)
|
||||
limit = request.args.get('limit', type=int)
|
||||
data, count = upload_curd.get_photo(page=page, limit=limit)
|
||||
return table_api(data=data, count=count)
|
||||
|
||||
|
||||
# 上传
|
||||
@admin_file.get('/upload')
|
||||
@authorize("admin:file:add", log=True)
|
||||
def upload():
|
||||
return render_template('admin/photo/photo_add.html')
|
||||
|
||||
|
||||
# 上传接口
|
||||
@admin_file.post('/upload')
|
||||
@authorize("admin:file:add", log=True)
|
||||
def upload_api():
|
||||
if 'file' in request.files:
|
||||
photo = request.files['file']
|
||||
mime = request.files['file'].content_type
|
||||
|
||||
file_url = upload_curd.upload_one(photo=photo, mime=mime)
|
||||
res = {
|
||||
"msg": "上传成功",
|
||||
"code": 0,
|
||||
"success": True,
|
||||
"data":
|
||||
{"src": file_url}
|
||||
}
|
||||
return jsonify(res)
|
||||
return fail_api()
|
||||
|
||||
|
||||
# 图片删除
|
||||
@admin_file.route('/delete', methods=['GET', 'POST'])
|
||||
@authorize("admin:file:delete", log=True)
|
||||
def delete():
|
||||
_id = request.form.get('id')
|
||||
res = upload_curd.delete_photo_by_id(_id)
|
||||
if res:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
|
||||
|
||||
# 图片批量删除
|
||||
@admin_file.route('/batchRemove', methods=['GET', 'POST'])
|
||||
@authorize("admin:file:delete", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
photo_name = Photo.query.filter(Photo.id.in_(ids)).all()
|
||||
upload_url = current_app.config.get("UPLOADED_PHOTOS_DEST")
|
||||
for p in photo_name:
|
||||
os.remove(upload_url + '/' + p.name)
|
||||
photo = Photo.query.filter(Photo.id.in_(ids)).delete(synchronize_session=False)
|
||||
db.session.commit()
|
||||
if photo:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -1,19 +0,0 @@
|
||||
from flask import Blueprint, render_template
|
||||
from flask_login import login_required, current_user
|
||||
|
||||
admin_bp = Blueprint('admin', __name__, url_prefix='/admin')
|
||||
|
||||
|
||||
# 首页
|
||||
@admin_bp.get('/')
|
||||
@login_required
|
||||
def index():
|
||||
user = current_user
|
||||
return render_template('admin/index.html', user=user)
|
||||
|
||||
|
||||
# 控制台页面
|
||||
@admin_bp.get('/welcome')
|
||||
@login_required
|
||||
def welcome():
|
||||
return render_template('admin/console/console.html')
|
||||
@@ -1,88 +0,0 @@
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
from datetime import datetime
|
||||
import time
|
||||
import psutil
|
||||
from flask import Blueprint, render_template, jsonify
|
||||
from applications.common.utils.rights import authorize
|
||||
|
||||
admin_monitor_bp = Blueprint('adminMonitor', __name__, url_prefix='/admin/monitor')
|
||||
|
||||
|
||||
# 系统监控
|
||||
@admin_monitor_bp.get('/')
|
||||
@authorize("admin:monitor:main", log=True)
|
||||
def main():
|
||||
# 主机名称
|
||||
hostname = platform.node()
|
||||
# 系统版本
|
||||
system_version = platform.platform()
|
||||
# python版本
|
||||
python_version = platform.python_version()
|
||||
# 逻辑cpu数量
|
||||
cpu_count = psutil.cpu_count()
|
||||
# cup使用率
|
||||
cpus_percent = psutil.cpu_percent(interval=0.1)
|
||||
# 内存
|
||||
memory_information = psutil.virtual_memory()
|
||||
# 内存使用率
|
||||
memory_usage = memory_information.percent
|
||||
memory_used = str(round(memory_information.used / 1024 / 1024))
|
||||
memory_total = str(round(memory_information.total / 1024 / 1024))
|
||||
memory_free = str(round(memory_information.free / 1024 / 1024))
|
||||
# 磁盘信息
|
||||
|
||||
disk_partitions_list = []
|
||||
# 判断是否在容器中
|
||||
if not os.path.exists('/.dockerenv'):
|
||||
disk_partitions = psutil.disk_partitions()
|
||||
for i in disk_partitions:
|
||||
a = psutil.disk_usage(i.device)
|
||||
disk_partitions_dict = {
|
||||
'device': i.device,
|
||||
'fstype': i.fstype,
|
||||
'total': str(round(a.total / 1024 / 1024)),
|
||||
'used': str(round(a.used / 1024 / 1024)),
|
||||
'free': str(round(a.free / 1024 / 1024)),
|
||||
'percent': a.percent
|
||||
}
|
||||
disk_partitions_list.append(disk_partitions_dict)
|
||||
|
||||
# 开机时间
|
||||
boot_time = datetime.fromtimestamp(psutil.boot_time()).replace(microsecond=0)
|
||||
up_time = datetime.now().replace(microsecond=0) - boot_time
|
||||
up_time_list = re.split(r':', str(up_time))
|
||||
up_time_format = " {} 小时{} 分钟{} 秒".format(up_time_list[0], up_time_list[1], up_time_list[2])
|
||||
|
||||
# 当前时间
|
||||
time_now = time.strftime('%H:%M:%S ', time.localtime(time.time()))
|
||||
return render_template('admin/monitor.html',
|
||||
hostname=hostname,
|
||||
system_version=system_version,
|
||||
python_version=python_version,
|
||||
cpus_percent=cpus_percent,
|
||||
memory_usage=memory_usage,
|
||||
cpu_count=cpu_count,
|
||||
memory_used=memory_used,
|
||||
memory_total=memory_total,
|
||||
memory_free=memory_free,
|
||||
boot_time=boot_time,
|
||||
up_time_format=up_time_format,
|
||||
disk_partitions_list=disk_partitions_list,
|
||||
time_now=time_now
|
||||
|
||||
)
|
||||
|
||||
|
||||
# 图表 api
|
||||
@admin_monitor_bp.get('/polling')
|
||||
@authorize("admin:monitor:main")
|
||||
def ajax_polling():
|
||||
# 获取cup使用率
|
||||
cpus_percent = psutil.cpu_percent(interval=0.1)
|
||||
# 获取内存使用率
|
||||
memory_information = psutil.virtual_memory()
|
||||
memory_usage = memory_information.percent
|
||||
time_now = time.strftime('%H:%M:%S ', time.localtime(time.time()))
|
||||
return jsonify(cups_percent=cpus_percent, memory_used=memory_usage, time_now=time_now)
|
||||
@@ -1,167 +0,0 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
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 xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Power
|
||||
from applications.schemas import PowerOutSchema2
|
||||
|
||||
admin_power = Blueprint('adminPower', __name__, url_prefix='/admin/power')
|
||||
|
||||
|
||||
@admin_power.get('/')
|
||||
@authorize("admin:power:main", log=True)
|
||||
def index():
|
||||
return render_template('admin/power/main.html')
|
||||
|
||||
|
||||
@admin_power.post('/data')
|
||||
@authorize("admin:power:main", log=True)
|
||||
def data():
|
||||
power = Power.query.all()
|
||||
res = {
|
||||
"data": curd.model_to_dicts(schema=PowerOutSchema2, data=power)
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@admin_power.get('/add')
|
||||
@authorize("admin:power:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/power/add.html')
|
||||
|
||||
|
||||
@admin_power.get('/selectParent')
|
||||
@authorize("admin:power:main", log=True)
|
||||
def select_parent():
|
||||
power = Power.query.all()
|
||||
res = curd.model_to_dicts(schema=PowerOutSchema2, data=power)
|
||||
res.append({"powerId": 0, "powerName": "顶级权限", "parentId": -1})
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": res
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
# 增加
|
||||
@admin_power.post('/save')
|
||||
@authorize("admin:power:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
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="成功")
|
||||
|
||||
|
||||
# 权限编辑
|
||||
@admin_power.get('/edit/<int:_id>')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def edit(_id):
|
||||
power = curd.get_one_by_id(Power, _id)
|
||||
icon = str(power.icon).split()
|
||||
if len(icon) == 2:
|
||||
icon = icon[1]
|
||||
else:
|
||||
icon = None
|
||||
return render_template('admin/power/edit.html', power=power, icon=icon)
|
||||
|
||||
|
||||
# 权限更新
|
||||
@admin_power.put('/update')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def update():
|
||||
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="更新权限成功")
|
||||
|
||||
|
||||
# 启用权限
|
||||
@admin_power.put('/enable')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def enable():
|
||||
_id = request.json.get('powerId')
|
||||
if id:
|
||||
res = curd.enable_status(Power, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用权限
|
||||
@admin_power.put('/disable')
|
||||
@authorize("admin:power:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('powerId')
|
||||
if id:
|
||||
res = curd.disable_status(Power, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 权限删除
|
||||
@admin_power.delete('/remove/<int:id>')
|
||||
@authorize("admin:power:remove", log=True)
|
||||
def remove(id):
|
||||
power = Power.query.filter_by(id=id).first()
|
||||
power.role = []
|
||||
|
||||
r = Power.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if r:
|
||||
return success_api(msg="删除成功")
|
||||
else:
|
||||
return fail_api(msg="删除失败")
|
||||
|
||||
|
||||
# 批量删除
|
||||
@admin_power.delete('/batchRemove')
|
||||
@authorize("admin:power:remove", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
for id in ids:
|
||||
power = Power.query.filter_by(id=id).first()
|
||||
power.role = []
|
||||
|
||||
r = Power.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
return success_api(msg="批量删除成功")
|
||||
@@ -1,204 +0,0 @@
|
||||
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 xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Role, Power, User
|
||||
from applications.schemas import RoleOutSchema, PowerOutSchema2
|
||||
|
||||
admin_role = Blueprint('adminRole', __name__, url_prefix='/admin/role')
|
||||
|
||||
|
||||
# 用户管理
|
||||
@admin_role.get('/')
|
||||
@authorize("admin:role:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/role/main.html')
|
||||
|
||||
|
||||
# 表格数据
|
||||
@admin_role.get('/data')
|
||||
@authorize("admin:role:main", log=True)
|
||||
def table():
|
||||
# 获取请求参数
|
||||
role_name = xss_escape(request.args.get('roleName', type=str))
|
||||
role_code = xss_escape(request.args.get('roleCode', type=str))
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if role_name:
|
||||
mf.vague(field_name="name", value=role_name)
|
||||
if role_code:
|
||||
mf.vague(field_name="code", value=role_code)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
role = Role.query.filter(mf.get_filter(Role)).layui_paginate()
|
||||
count = role.total
|
||||
# 返回api
|
||||
return table_api(data=model_to_dicts(schema=RoleOutSchema, data=role.items), count=count)
|
||||
|
||||
|
||||
# 角色增加
|
||||
@admin_role.get('/add')
|
||||
@authorize("admin:role:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/role/add.html')
|
||||
|
||||
|
||||
# 角色增加
|
||||
@admin_role.post('/save')
|
||||
@authorize("admin:role:add", log=True)
|
||||
def save():
|
||||
req = request.json
|
||||
details = xss_escape(req.get("details"))
|
||||
enable = xss_escape(req.get("enable"))
|
||||
roleCode = xss_escape(req.get("roleCode"))
|
||||
roleName = xss_escape(req.get("roleName"))
|
||||
sort = xss_escape(req.get("sort"))
|
||||
role = Role(
|
||||
details=details,
|
||||
enable=enable,
|
||||
code=roleCode,
|
||||
name=roleName,
|
||||
sort=sort
|
||||
)
|
||||
db.session.add(role)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
# 角色授权
|
||||
@admin_role.get('/power/<int:_id>')
|
||||
@authorize("admin:role:power", log=True)
|
||||
def power(_id):
|
||||
return render_template('admin/role/power.html', id=_id)
|
||||
|
||||
|
||||
# 获取角色权限
|
||||
@admin_role.get('/getRolePower/<int:id>')
|
||||
@authorize("admin:role:main", log=True)
|
||||
def get_role_power(id):
|
||||
role = Role.query.filter_by(id=id).first()
|
||||
check_powers = role.power
|
||||
check_powers_list = []
|
||||
for cp in check_powers:
|
||||
check_powers_list.append(cp.id)
|
||||
powers = Power.query.all()
|
||||
power_schema = PowerOutSchema2(many=True) # 用已继承ma.ModelSchema类的自定制类生成序列化类
|
||||
output = power_schema.dump(powers) # 生成可序列化对象
|
||||
for i in output:
|
||||
if int(i.get("powerId")) in check_powers_list:
|
||||
i["checkArr"] = "1"
|
||||
else:
|
||||
i["checkArr"] = "0"
|
||||
res = {
|
||||
"data": output,
|
||||
"status": {"code": 200, "message": "默认"}
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
# 保存角色权限
|
||||
@admin_role.put('/saveRolePower')
|
||||
@authorize("admin:role:edit", log=True)
|
||||
def save_role_power():
|
||||
req_form = request.form
|
||||
power_ids = req_form.get("powerIds")
|
||||
power_list = power_ids.split(',')
|
||||
role_id = req_form.get("roleId")
|
||||
role = Role.query.filter_by(id=role_id).first()
|
||||
|
||||
powers = Power.query.filter(Power.id.in_(power_list)).all()
|
||||
role.power = powers
|
||||
|
||||
db.session.commit()
|
||||
return success_api(msg="授权成功")
|
||||
|
||||
|
||||
# 角色编辑
|
||||
@admin_role.get('/edit/<int:id>')
|
||||
@authorize("admin:role:edit", log=True)
|
||||
def edit(id):
|
||||
r = get_one_by_id(model=Role, id=id)
|
||||
return render_template('admin/role/edit.html', role=r)
|
||||
|
||||
|
||||
# 更新角色
|
||||
@admin_role.put('/update')
|
||||
@authorize("admin:role:edit", log=True)
|
||||
def update():
|
||||
req_json = request.json
|
||||
id = req_json.get("roleId")
|
||||
data = {
|
||||
"code": xss_escape(req_json.get("roleCode")),
|
||||
"name": xss_escape(req_json.get("roleName")),
|
||||
"sort": xss_escape(req_json.get("sort")),
|
||||
"enable": xss_escape(req_json.get("enable")),
|
||||
"details": xss_escape(req_json.get("details"))
|
||||
}
|
||||
role = Role.query.filter_by(id=id).update(data)
|
||||
db.session.commit()
|
||||
if not role:
|
||||
return fail_api(msg="更新角色失败")
|
||||
return success_api(msg="更新角色成功")
|
||||
|
||||
|
||||
# 启用用户
|
||||
@admin_role.put('/enable')
|
||||
@authorize("admin:role:edit", log=True)
|
||||
def enable():
|
||||
id = request.json.get('roleId')
|
||||
if id:
|
||||
res = enable_status(Role, id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用用户
|
||||
@admin_role.put('/disable')
|
||||
@authorize("admin:role:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('roleId')
|
||||
if _id:
|
||||
res = disable_status(Role, _id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 角色删除
|
||||
@admin_role.delete('/remove/<int:id>')
|
||||
@authorize("admin:role:remove", log=True)
|
||||
def remove(id):
|
||||
role = Role.query.filter_by(id=id).first()
|
||||
# 删除该角色的权限和用户
|
||||
role.power = []
|
||||
role.user = []
|
||||
|
||||
r = Role.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="角色删除失败")
|
||||
return success_api(msg="角色删除成功")
|
||||
|
||||
|
||||
# 批量删除
|
||||
@admin_role.delete('/batchRemove')
|
||||
@authorize("admin:role:remove", log=True)
|
||||
@login_required
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
for id in ids:
|
||||
role = Role.query.filter_by(id=id).first()
|
||||
# 删除该角色的权限和用户
|
||||
role.power = []
|
||||
role.user = []
|
||||
|
||||
r = Role.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
return success_api(msg="批量删除成功")
|
||||
@@ -1,112 +0,0 @@
|
||||
from flask import Blueprint, request, render_template
|
||||
from flask_apscheduler.utils import job_to_dict
|
||||
|
||||
from applications.common.tasks import tasks
|
||||
from applications.common.tasks.tasks import task_list
|
||||
from applications.common.utils.http import table_api, fail_api, success_api
|
||||
from applications.extensions.init_apscheduler import scheduler
|
||||
|
||||
admin_task = Blueprint('adminTask', __name__, url_prefix='/admin/task')
|
||||
|
||||
|
||||
@admin_task.route('/add_job', methods=['GET'])
|
||||
def add_task():
|
||||
scheduler.add_job(func=tasks.get(), id='4', args=(1, 1), trigger='interval', seconds=3,
|
||||
replace_existing=True)
|
||||
return '6'
|
||||
|
||||
|
||||
@admin_task.get('/')
|
||||
def main():
|
||||
return render_template('admin/task/main.html')
|
||||
|
||||
|
||||
@admin_task.route('/data', methods=['GET'])
|
||||
def get_task(): # 获取
|
||||
jobs = scheduler.get_jobs()
|
||||
jobs_list = []
|
||||
for job in jobs:
|
||||
jobs_list.append(job_to_dict(job))
|
||||
return table_api(data=jobs_list, count=len(jobs_list))
|
||||
|
||||
|
||||
# 增加
|
||||
@admin_task.get('/add')
|
||||
def add():
|
||||
return render_template('admin/task/add.html', task_list=task_list)
|
||||
|
||||
|
||||
@admin_task.post('/save')
|
||||
def save():
|
||||
_id = request.json.get("id")
|
||||
name = request.json.get("id")
|
||||
type = request.json.get("type")
|
||||
functions = request.json.get("functions")
|
||||
datetime = request.json.get("datetime")
|
||||
time = request.json.get("time")
|
||||
if not hasattr(tasks, functions):
|
||||
return fail_api()
|
||||
if type == 'date':
|
||||
scheduler.add_job(
|
||||
func=getattr(tasks, functions),
|
||||
id=_id,
|
||||
name=name,
|
||||
args=(1, 1),
|
||||
trigger=type,
|
||||
run_date=datetime,
|
||||
replace_existing=True)
|
||||
elif type == 'interval':
|
||||
scheduler.add_job(
|
||||
func=getattr(tasks, functions),
|
||||
id=_id,
|
||||
name=name,
|
||||
args=(1, 1),
|
||||
trigger=type,
|
||||
replace_existing=True)
|
||||
elif type == 'cron':
|
||||
scheduler.add_job(
|
||||
func=getattr(tasks, functions),
|
||||
id=_id,
|
||||
name=name,
|
||||
args=(1, 1),
|
||||
trigger=type,
|
||||
replace_existing=True)
|
||||
|
||||
return success_api()
|
||||
|
||||
|
||||
# 恢复
|
||||
@admin_task.put('/enable')
|
||||
def enable():
|
||||
_id = request.json.get('id')
|
||||
# print(id)
|
||||
if _id:
|
||||
scheduler.resume_job(str(_id))
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 暂停
|
||||
@admin_task.put('/disable')
|
||||
def dis_enable():
|
||||
_id = request.json.get('id')
|
||||
if _id:
|
||||
scheduler.pause_job(str(_id))
|
||||
return success_api(msg="暂停成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@admin_task.delete('/remove/<int:_id>')
|
||||
def remove_job(_id): # 移除
|
||||
scheduler.remove_job(str(_id))
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
# scheduler.add_job(func=task1, id='2', args=(1, 1), trigger='cron', day_of_week='0-6', hour=18, minute=24,
|
||||
# second=10, replace_existing=True)
|
||||
|
||||
# scheduler.add_job(func=task4, id='4', args=(2, 2), trigger='interval', seconds=3,
|
||||
# replace_existing=True, misfire_grace_time=3)
|
||||
|
||||
# # trigger='interval' 表示是一个循环任务,每隔多久执行一次
|
||||
# scheduler.add_job(func=task2, id='3', args=(2, 2), trigger='interval', seconds=3,
|
||||
# replace_existing=True)
|
||||
@@ -1,235 +0,0 @@
|
||||
from flask import Blueprint, render_template, request
|
||||
from flask_login import login_required, current_user
|
||||
from sqlalchemy import 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
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import xss_escape
|
||||
from applications.extensions import db
|
||||
from applications.models import Role
|
||||
from applications.models import User, AdminLog
|
||||
from applications.schemas import UserOutSchema
|
||||
|
||||
admin_user = Blueprint('adminUser', __name__, url_prefix='/admin/user')
|
||||
|
||||
|
||||
# 用户管理
|
||||
@admin_user.get('/')
|
||||
@authorize("admin:user:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/user/main.html')
|
||||
|
||||
|
||||
# 用户分页查询
|
||||
@admin_user.get('/data')
|
||||
@authorize("admin:user:main", log=True)
|
||||
def data():
|
||||
# 获取请求参数
|
||||
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)
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if real_name:
|
||||
mf.contains(field_name="realname", value=real_name)
|
||||
if username:
|
||||
mf.contains(field_name="username", value=username)
|
||||
if dept_id:
|
||||
mf.exact(field_name="dept_id", value=dept_id)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
user = User.query.filter(mf.get_filter(model=User)).layui_paginate()
|
||||
count = user.total
|
||||
# 返回api
|
||||
return table_api(data=model_to_dicts(schema=UserOutSchema, data=user.items), count=count)
|
||||
|
||||
|
||||
# 用户增加
|
||||
@admin_user.get('/add')
|
||||
@authorize("admin:user:add", log=True)
|
||||
def add():
|
||||
roles = Role.query.all()
|
||||
return render_template('admin/user/add.html', roles=roles)
|
||||
|
||||
|
||||
@admin_user.post('/save')
|
||||
@authorize("admin:user:add", log=True)
|
||||
def save():
|
||||
req_json = request.json
|
||||
a = req_json.get("roleIds")
|
||||
username = xss_escape(req_json.get('username'))
|
||||
real_name = xss_escape(req_json.get('realName'))
|
||||
password = xss_escape(req_json.get('password'))
|
||||
role_ids = a.split(',')
|
||||
|
||||
if not username or not real_name or not password:
|
||||
return fail_api(msg="账号姓名密码不得为空")
|
||||
|
||||
if bool(User.query.filter_by(username=username).count()):
|
||||
return fail_api(msg="用户已经存在")
|
||||
user = User(username=username, realname=real_name)
|
||||
user.set_password(password)
|
||||
db.session.add(user)
|
||||
roles = Role.query.filter(Role.id.in_(role_ids)).all()
|
||||
for r in roles:
|
||||
user.role.append(r)
|
||||
db.session.commit()
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
|
||||
# 删除用户
|
||||
@admin_user.delete('/remove/<int:id>')
|
||||
@authorize("admin:user:remove", log=True)
|
||||
def delete(id):
|
||||
user = User.query.filter_by(id=id).first()
|
||||
user.role = []
|
||||
|
||||
res = User.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
# 编辑用户
|
||||
@admin_user.get('/edit/<int:id>')
|
||||
@authorize("admin:user:edit", log=True)
|
||||
def edit(id):
|
||||
user = curd.get_one_by_id(User,id)
|
||||
roles = Role.query.all()
|
||||
checked_roles = []
|
||||
for r in user.role:
|
||||
checked_roles.append(r.id)
|
||||
return render_template('admin/user/edit.html', user=user, roles=roles, checked_roles=checked_roles)
|
||||
|
||||
|
||||
# 编辑用户
|
||||
@admin_user.put('/update')
|
||||
@authorize("admin:user:edit", log=True)
|
||||
def update():
|
||||
req_json = request.json
|
||||
a = xss_escape(req_json.get("roleIds"))
|
||||
id = xss_escape(req_json.get("userId"))
|
||||
username = xss_escape(req_json.get('username'))
|
||||
real_name = xss_escape(req_json.get('realName'))
|
||||
dept_id = xss_escape(req_json.get('deptId'))
|
||||
role_ids = a.split(',')
|
||||
User.query.filter_by(id=id).update({'username': username, 'realname': real_name, 'dept_id': dept_id})
|
||||
u = User.query.filter_by(id=id).first()
|
||||
|
||||
roles = Role.query.filter(Role.id.in_(role_ids)).all()
|
||||
u.role = roles
|
||||
|
||||
db.session.commit()
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 个人中心
|
||||
@admin_user.get('/center')
|
||||
@login_required
|
||||
def center():
|
||||
user_info = current_user
|
||||
user_logs = AdminLog.query.filter_by(url='/passport/login').filter_by(uid=current_user.id).order_by(
|
||||
desc(AdminLog.create_time)).limit(10)
|
||||
return render_template('admin/user/center.html', user_info=user_info, user_logs=user_logs)
|
||||
|
||||
|
||||
# 修改头像
|
||||
@admin_user.get('/profile')
|
||||
@login_required
|
||||
def profile():
|
||||
return render_template('admin/user/profile.html')
|
||||
|
||||
|
||||
# 修改头像
|
||||
@admin_user.put('/updateAvatar')
|
||||
@login_required
|
||||
def update_avatar():
|
||||
url = request.json.get("avatar").get("src")
|
||||
r = User.query.filter_by(id=current_user.id).update({"avatar": url})
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="修改成功")
|
||||
|
||||
|
||||
# 修改当前用户信息
|
||||
@admin_user.put('/updateInfo')
|
||||
@login_required
|
||||
def update_info():
|
||||
req_json = request.json
|
||||
r = User.query.filter_by(id=current_user.id).update(
|
||||
{"realname": req_json.get("realName"), "remark": req_json.get("details")})
|
||||
db.session.commit()
|
||||
if not r:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="更新成功")
|
||||
|
||||
|
||||
# 修改当前用户密码
|
||||
@admin_user.get('/editPassword')
|
||||
@login_required
|
||||
def edit_password():
|
||||
return render_template('admin/user/edit_password.html')
|
||||
|
||||
|
||||
# 修改当前用户密码
|
||||
@admin_user.put('/editPassword')
|
||||
@login_required
|
||||
def edit_password_put():
|
||||
res_json = request.json
|
||||
if res_json.get("newPassword") == '':
|
||||
return fail_api("新密码不得为空")
|
||||
if res_json.get("newPassword") != res_json.get("confirmPassword"):
|
||||
return fail_api("俩次密码不一样")
|
||||
user = current_user
|
||||
is_right = user.validate_password(res_json.get("oldPassword"))
|
||||
if not is_right:
|
||||
return fail_api("旧密码错误")
|
||||
user.set_password(res_json.get("newPassword"))
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return success_api("更改成功")
|
||||
|
||||
|
||||
# 启用用户
|
||||
@admin_user.put('/enable')
|
||||
@authorize("admin:user:edit", log=True)
|
||||
def enable():
|
||||
_id = request.json.get('userId')
|
||||
if _id:
|
||||
res = enable_status(model=User, id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="启动成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 禁用用户
|
||||
@admin_user.put('/disable')
|
||||
@authorize("admin:user:edit", log=True)
|
||||
def dis_enable():
|
||||
_id = request.json.get('userId')
|
||||
if _id:
|
||||
res = disable_status(model=User,id=_id)
|
||||
if not res:
|
||||
return fail_api(msg="出错啦")
|
||||
return success_api(msg="禁用成功")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 批量删除
|
||||
@admin_user.delete('/batchRemove')
|
||||
@authorize("admin:user:remove", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
for id in ids:
|
||||
user = User.query.filter_by(id=id).first()
|
||||
user.role = []
|
||||
|
||||
res = User.query.filter_by(id=id).delete()
|
||||
db.session.commit()
|
||||
return success_api(msg="批量删除成功")
|
||||
@@ -1,144 +0,0 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
|
||||
from applications.common import curd
|
||||
from applications.common.utils import validate
|
||||
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 Dept, User
|
||||
from applications.schemas import DeptOutSchema
|
||||
|
||||
dept_bp = Blueprint('dept', __name__, url_prefix='/dept')
|
||||
|
||||
|
||||
def register_dept_views(app):
|
||||
app.register_blueprint(dept_bp)
|
||||
|
||||
|
||||
@dept_bp.get('/')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/dept/main.html')
|
||||
|
||||
|
||||
@dept_bp.post('/data')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def data():
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
power_data = curd.model_to_dicts(schema=DeptOutSchema, data=dept)
|
||||
res = {
|
||||
"data": power_data
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@dept_bp.get('/add')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/dept/add.html')
|
||||
|
||||
|
||||
@dept_bp.get('/tree')
|
||||
@authorize("admin:dept:main", log=True)
|
||||
def tree():
|
||||
dept = Dept.query.order_by(Dept.sort).all()
|
||||
power_data = curd.model_to_dicts(schema=DeptOutSchema, data=dept)
|
||||
res = {
|
||||
"status": {"code": 200, "message": "默认"},
|
||||
"data": power_data
|
||||
|
||||
}
|
||||
return jsonify(res)
|
||||
|
||||
|
||||
@dept_bp.post('/save')
|
||||
@authorize("admin:dept:add", log=True)
|
||||
def save():
|
||||
req_json = request.json
|
||||
dept = Dept(
|
||||
parent_id=req_json.get('parentId'),
|
||||
dept_name=xss_escape(req_json.get('deptName')),
|
||||
sort=xss_escape(req_json.get('sort')),
|
||||
leader=xss_escape(req_json.get('leader')),
|
||||
phone=xss_escape(req_json.get('phone')),
|
||||
email=xss_escape(req_json.get('email')),
|
||||
status=xss_escape(req_json.get('status')),
|
||||
address=xss_escape(req_json.get('address'))
|
||||
)
|
||||
r = db.session.add(dept)
|
||||
db.session.commit()
|
||||
return success_api(msg="成功")
|
||||
|
||||
|
||||
@dept_bp.get('/edit')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def edit():
|
||||
_id = request.args.get("deptId")
|
||||
dept = curd.get_one_by_id(model=Dept, 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:
|
||||
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="数据错误")
|
||||
|
||||
|
||||
# 禁用
|
||||
@dept_bp.put('/disable')
|
||||
@authorize("admin:dept:edit", log=True)
|
||||
def dis_enable():
|
||||
id = request.json.get('deptId')
|
||||
if id:
|
||||
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():
|
||||
json = request.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):
|
||||
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:
|
||||
return fail_api(msg="删除失败")
|
||||
@@ -1,12 +0,0 @@
|
||||
from applications.view.index.index import index_bp
|
||||
|
||||
from . import index
|
||||
|
||||
|
||||
def register_index_views(app):
|
||||
"""
|
||||
初始化蓝图
|
||||
|
||||
"""
|
||||
|
||||
app.register_blueprint(index_bp)
|
||||
@@ -1,8 +0,0 @@
|
||||
from flask import render_template, Blueprint
|
||||
|
||||
index_bp = Blueprint('Index', __name__, url_prefix='/')
|
||||
|
||||
|
||||
@index_bp.route('/')
|
||||
def index():
|
||||
return render_template('index/index.html')
|
||||
@@ -1,76 +0,0 @@
|
||||
from flask import Blueprint, session, redirect, url_for, render_template, request
|
||||
from flask_login import current_user, login_user, login_required, logout_user
|
||||
|
||||
from applications.common import admin as index_curd
|
||||
from applications.common.admin_log import login_log
|
||||
from applications.common.utils.http import fail_api, success_api
|
||||
from applications.models import User
|
||||
|
||||
passport_bp = Blueprint('passport', __name__, url_prefix='/passport')
|
||||
|
||||
|
||||
def register_passport_views(app):
|
||||
app.register_blueprint(passport_bp)
|
||||
|
||||
|
||||
# 获取验证码
|
||||
@passport_bp.get('/getCaptcha')
|
||||
def get_captcha():
|
||||
resp, code = index_curd.get_captcha()
|
||||
session["code"] = code
|
||||
return resp
|
||||
|
||||
|
||||
# 登录
|
||||
@passport_bp.get('/login')
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('admin.index'))
|
||||
return render_template('admin/login.html')
|
||||
|
||||
|
||||
# 登录
|
||||
@passport_bp.post('/login')
|
||||
def login_post():
|
||||
req = request.form
|
||||
username = req.get('username')
|
||||
password = req.get('password')
|
||||
code = req.get('captcha').__str__().lower()
|
||||
|
||||
if not username or not password or not code:
|
||||
return fail_api(msg="用户名或密码没有输入")
|
||||
s_code = session.get("code", None)
|
||||
session["code"] = None
|
||||
|
||||
if not all([code, s_code]):
|
||||
return fail_api(msg="参数错误")
|
||||
|
||||
if code != s_code:
|
||||
return fail_api(msg="验证码错误")
|
||||
user = User.query.filter_by(username=username).first()
|
||||
|
||||
if not user:
|
||||
return fail_api(msg="不存在的用户")
|
||||
|
||||
if user.enable == 0:
|
||||
return fail_api(msg="用户被暂停使用")
|
||||
|
||||
if username == user.username and user.validate_password(password):
|
||||
# 登录
|
||||
login_user(user)
|
||||
# 记录登录日志
|
||||
login_log(request, uid=user.id, is_access=True)
|
||||
# 存入权限
|
||||
index_curd.add_auth_session()
|
||||
return success_api(msg="登录成功")
|
||||
login_log(request, uid=user.id, is_access=False)
|
||||
return fail_api(msg="用户名或密码错误")
|
||||
|
||||
|
||||
# 退出登录
|
||||
@passport_bp.post('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
session.pop('permissions')
|
||||
return success_api(msg="注销成功")
|
||||
@@ -1,9 +0,0 @@
|
||||
from flask import Blueprint, Flask
|
||||
|
||||
rights_bp = Blueprint('rights', __name__, url_prefix='/rights')
|
||||
|
||||
from . import routes
|
||||
|
||||
|
||||
def register_rights_view(app: Flask):
|
||||
app.register_blueprint(rights_bp)
|
||||
@@ -1,20 +0,0 @@
|
||||
# 渲染配置
|
||||
from flask import jsonify
|
||||
from flask_login import login_required
|
||||
|
||||
from . import rights_bp
|
||||
from ...common import admin
|
||||
|
||||
|
||||
@rights_bp.get('/configs')
|
||||
@login_required
|
||||
def configs():
|
||||
return admin.get_render_config()
|
||||
|
||||
|
||||
# 菜单
|
||||
@rights_bp.get('/menu')
|
||||
@login_required
|
||||
def menu():
|
||||
menu_tree = admin.make_menu_tree()
|
||||
return jsonify(menu_tree)
|
||||
@@ -0,0 +1,19 @@
|
||||
from flask import current_app
|
||||
from cachelib.file import FileSystemCache
|
||||
from cachelib.memcached import MemcachedCache
|
||||
from cachelib.redis import RedisCache
|
||||
from cachelib.simple import SimpleCache
|
||||
from cachelib.uwsgi import UWSGICache
|
||||
|
||||
cacher: dict = {
|
||||
'filesystem': FileSystemCache,
|
||||
'memcached': MemcachedCache,
|
||||
'redis': RedisCache,
|
||||
'simple': SimpleCache,
|
||||
'uwsgi': UWSGICache
|
||||
}
|
||||
cache = RedisCache()
|
||||
|
||||
|
||||
def get_cache():
|
||||
return cache.get(current_app.config.get("CACHE_TYPE") or "filesystem")()
|
||||
@@ -0,0 +1,18 @@
|
||||
from celery import Celery
|
||||
|
||||
from entrance.config import celery as config
|
||||
from entrance.config.celery import broker_url
|
||||
|
||||
celery = Celery(__name__, broker=broker_url)
|
||||
|
||||
|
||||
def register_celery(app):
|
||||
celery.config_from_object(config)
|
||||
|
||||
class ContextTask(celery.Task):
|
||||
def __call__(self, *args, **kwargs):
|
||||
with app.app_context():
|
||||
return self.run(*args, **kwargs)
|
||||
|
||||
celery.Task = ContextTask
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
from flask_limiter import Limiter
|
||||
from flask_limiter.util import get_remote_address
|
||||
|
||||
limiter = Limiter(
|
||||
key_func=get_remote_address,
|
||||
default_limits=["200 per day", "50 per hour"]
|
||||
)
|
||||
@@ -0,0 +1,55 @@
|
||||
from logging.config import dictConfig
|
||||
|
||||
|
||||
def config_logger():
|
||||
dictConfig(
|
||||
{
|
||||
"version": 1,
|
||||
"disable_existing_loggers": False,
|
||||
"formatters": {
|
||||
"simple": {"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s"}
|
||||
},
|
||||
"handlers": {
|
||||
"console": {
|
||||
"class": "logging.StreamHandler",
|
||||
"level": "DEBUG",
|
||||
"formatter": "simple",
|
||||
"stream": "ext://sys.stdout",
|
||||
},
|
||||
"info_file_handler": {
|
||||
"class": "logging.handlers.RotatingFileHandler",
|
||||
"level": "INFO",
|
||||
"formatter": "simple",
|
||||
"filename": "logs/info.log",
|
||||
"maxBytes": 10485760,
|
||||
"backupCount": 50,
|
||||
"encoding": "utf8",
|
||||
},
|
||||
"error_file_handler": {
|
||||
"class": "logging.handlers.RotatingFileHandler",
|
||||
"level": "ERROR",
|
||||
"formatter": "simple",
|
||||
"filename": "logs/errors.log",
|
||||
"maxBytes": 10485760,
|
||||
"backupCount": 20,
|
||||
"encoding": "utf8",
|
||||
},
|
||||
"debug_file_handler": {
|
||||
"class": "logging.handlers.RotatingFileHandler",
|
||||
"level": "DEBUG",
|
||||
"formatter": "simple",
|
||||
"filename": "logs/debug.log",
|
||||
"maxBytes": 10485760,
|
||||
"backupCount": 50,
|
||||
"encoding": "utf8",
|
||||
},
|
||||
},
|
||||
"loggers": {
|
||||
"my_module": {"level": "ERROR", "handlers": ["console"], "propagate": "no"}
|
||||
},
|
||||
"root": {
|
||||
"level": "DEBUG",
|
||||
"handlers": ["error_file_handler", "debug_file_handler"],
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,3 @@
|
||||
from flask_mail import Mail
|
||||
|
||||
mail = Mail()
|
||||
@@ -0,0 +1,7 @@
|
||||
from flask_migrate import Migrate
|
||||
|
||||
from common.extend.orm import db
|
||||
from entrance.models import model_lists
|
||||
|
||||
migrate = Migrate(db=db, directory='entrance/migrations')
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import datetime
|
||||
from typing import Tuple
|
||||
|
||||
from flask import request
|
||||
from flask_sqlalchemy import SQLAlchemy, BaseQuery
|
||||
|
||||
|
||||
class Query(BaseQuery):
|
||||
|
||||
def all_json(self) -> list:
|
||||
return [dict(i) for i in self.all()]
|
||||
|
||||
def soft_delete(self):
|
||||
return self.update({"delete_at": datetime.datetime.now()})
|
||||
|
||||
def logic_all(self):
|
||||
return self.filter_by(delete_at=None).all()
|
||||
|
||||
def layui_paginate(self):
|
||||
return self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
|
||||
def layui_paginate_json(self) -> Tuple[list, int]:
|
||||
p = self.paginate(page=request.args.get('page', type=int),
|
||||
per_page=request.args.get('limit', type=int),
|
||||
error_out=False)
|
||||
|
||||
return [dict(i) for i in p.items], p.total
|
||||
|
||||
|
||||
db = SQLAlchemy(query_class=Query)
|
||||
@@ -0,0 +1,89 @@
|
||||
import datetime
|
||||
|
||||
from entrance.extensions import db
|
||||
from sqlalchemy.orm import relationship
|
||||
from sqlalchemy import ForeignKey
|
||||
from sqlalchemy.orm import backref
|
||||
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema
|
||||
|
||||
|
||||
class BaseModel(db.Model):
|
||||
__abstract__ = True
|
||||
|
||||
# insert and update
|
||||
def save(self):
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
|
||||
# delete
|
||||
def delete(self):
|
||||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
class CommonModel(BaseModel):
|
||||
__abstract__ = True
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
|
||||
|
||||
class CurdModel(BaseModel):
|
||||
__abstract__ = True
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
|
||||
create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
update_time = db.Column(db.DateTime, onupdate=datetime.datetime.now, comment='更新时间')
|
||||
delete_at = db.Column(db.DateTime, comment='删除时间')
|
||||
|
||||
|
||||
Column = db.Column
|
||||
Table = db.Table
|
||||
relationship = relationship
|
||||
ForeignKey = ForeignKey
|
||||
backref = backref
|
||||
|
||||
TypeDecorator = db.TypeDecorator
|
||||
INT = db.INT
|
||||
CHAR = db.CHAR
|
||||
VARCHAR = db.VARCHAR
|
||||
NCHAR = db.NCHAR
|
||||
NVARCHAR = db.NVARCHAR
|
||||
TEXT = db.TEXT
|
||||
Text = db.Text
|
||||
FLOAT = db.FLOAT
|
||||
NUMERIC = db.NUMERIC
|
||||
REAL = db.REAL
|
||||
DECIMAL = db.DECIMAL
|
||||
TIMESTAMP = db.TIMESTAMP
|
||||
DATETIME = db.DATETIME
|
||||
CLOB = db.CLOB
|
||||
BLOB = db.BLOB
|
||||
BINARY = db.BINARY
|
||||
VARBINARY = db.VARBINARY
|
||||
BOOLEAN = db.BOOLEAN
|
||||
BIGINT = db.BIGINT
|
||||
SMALLINT = db.SMALLINT
|
||||
INTEGER = db.INTEGER
|
||||
DATE = db.DATE
|
||||
TIME = db.TIME
|
||||
TupleType = db.TupleType
|
||||
String = db.String
|
||||
Integer = db.Integer
|
||||
SmallInteger = db.SmallInteger
|
||||
BigInteger = db.BigInteger
|
||||
Numeric = db.Numeric
|
||||
Float = db.Float
|
||||
DateTime = db.DateTime
|
||||
Date = db.Date
|
||||
Time = db.Time
|
||||
LargeBinary = db.LargeBinary
|
||||
Boolean = db.Boolean
|
||||
Unicode = db.Unicode
|
||||
UnicodeText = db.UnicodeText
|
||||
PickleType = db.PickleType
|
||||
Interval = db.Interval
|
||||
Enum = db.Enum
|
||||
ARRAY = db.ARRAY
|
||||
JSON = db.JSON
|
||||
|
||||
SQLAlchemyAutoSchema = SQLAlchemyAutoSchema
|
||||
@@ -0,0 +1,121 @@
|
||||
import random, math
|
||||
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
||||
|
||||
|
||||
class vieCode:
|
||||
__fontSize = 20 # 字体大小
|
||||
__width = 120 # 画布宽度
|
||||
__heigth = 45 # 画布高度
|
||||
__length = 4 # 验证码长度
|
||||
__draw = None # 画布
|
||||
__img = None # 图片资源
|
||||
__code = None # 验证码字符
|
||||
__str = None # 自定义验证码字符集
|
||||
__inCurve = True # 是否画干扰线
|
||||
__inNoise = True # 是否画干扰点
|
||||
__type = 2 # 验证码类型 1、纯字母 2、数字字母混合
|
||||
__fontPatn = 'applications/common/utils/fonts/2.ttf' # 字体
|
||||
|
||||
def GetCodeImage(self, size=80, length=4):
|
||||
'''获取验证码图片
|
||||
@param int size 验证码大小
|
||||
@param int length 验证码长度
|
||||
'''
|
||||
# 准备基础数据
|
||||
self.__length = length
|
||||
self.__fontSize = size
|
||||
self.__width = self.__fontSize * self.__length
|
||||
self.__heigth = int(self.__fontSize * 1.5)
|
||||
|
||||
# 生成验证码图片
|
||||
self.__createCode()
|
||||
self.__createImage()
|
||||
self.__createNoise()
|
||||
self.__printString()
|
||||
self.__cerateFilter()
|
||||
|
||||
return self.__img, self.__code
|
||||
|
||||
def __cerateFilter(self):
|
||||
'''模糊处理'''
|
||||
self.__img = self.__img.filter(ImageFilter.BLUR)
|
||||
filter = ImageFilter.ModeFilter(8)
|
||||
self.__img = self.__img.filter(filter)
|
||||
|
||||
def __createCode(self):
|
||||
'''创建验证码字符'''
|
||||
# 是否自定义字符集合
|
||||
if not self.__str:
|
||||
# 源文本
|
||||
number = "3456789"
|
||||
srcLetter = "qwertyuipasdfghjkzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"
|
||||
srcUpper = srcLetter.upper()
|
||||
if self.__type == 1:
|
||||
self.__str = number
|
||||
else:
|
||||
self.__str = srcLetter + srcUpper + number
|
||||
|
||||
# 构造验证码
|
||||
self.__code = random.sample(self.__str, self.__length)
|
||||
|
||||
def __createImage(self):
|
||||
'''创建画布'''
|
||||
bgColor = (random.randint(200, 255), random.randint(200, 255), random.randint(200, 255))
|
||||
self.__img = Image.new('RGB', (self.__width, self.__heigth), bgColor)
|
||||
self.__draw = ImageDraw.Draw(self.__img)
|
||||
|
||||
def __createNoise(self):
|
||||
'''画干扰点'''
|
||||
if not self.__inNoise:
|
||||
return
|
||||
font = ImageFont.truetype(self.__fontPatn, int(self.__fontSize / 1.5))
|
||||
for i in range(5):
|
||||
# 杂点颜色
|
||||
noiseColor = (random.randint(150, 200), random.randint(150, 200), random.randint(150, 200))
|
||||
putStr = random.sample(self.__str, 2)
|
||||
for j in range(2):
|
||||
# 绘杂点
|
||||
size = (random.randint(-10, self.__width), random.randint(-10, self.__heigth))
|
||||
self.__draw.text(size, putStr[j], font=font, fill=noiseColor)
|
||||
pass
|
||||
|
||||
def __createCurve(self):
|
||||
'''画干扰线'''
|
||||
if not self.__inCurve:
|
||||
return
|
||||
x = y = 0
|
||||
|
||||
# 计算曲线系数
|
||||
a = random.uniform(1, self.__heigth / 2)
|
||||
b = random.uniform(-self.__width / 4, self.__heigth / 4)
|
||||
f = random.uniform(-self.__heigth / 4, self.__heigth / 4)
|
||||
t = random.uniform(self.__heigth, self.__width * 2)
|
||||
xend = random.randint(self.__width / 2, self.__width * 2)
|
||||
w = (2 * math.pi) / t
|
||||
|
||||
# 画曲线
|
||||
color = (random.randint(30, 150), random.randint(30, 150), random.randint(30, 150))
|
||||
for x in range(xend):
|
||||
if w != 0:
|
||||
for k in range(int(self.__heigth / 10)):
|
||||
y = a * math.sin(w * x + f) + b + self.__heigth / 2
|
||||
i = int(self.__fontSize / 5)
|
||||
while i > 0:
|
||||
px = x + i
|
||||
py = y + i + k
|
||||
self.__draw.point((px, py), color)
|
||||
i -= i
|
||||
|
||||
def __printString(self):
|
||||
'''打印验证码字符串'''
|
||||
font = ImageFont.truetype(self.__fontPatn, self.__fontSize)
|
||||
x = 0
|
||||
# 打印字符到画板
|
||||
for i in range(self.__length):
|
||||
# 设置字体随机颜色
|
||||
color = (random.randint(30, 150), random.randint(30, 150), random.randint(30, 150))
|
||||
# 计算座标
|
||||
x = random.uniform(self.__fontSize * i * 0.95, self.__fontSize * i * 1.1)
|
||||
y = self.__fontSize * random.uniform(0.3, 0.5)
|
||||
# 打印字符
|
||||
self.__draw.text((x, y), self.__code[i], font=font, fill=color)
|
||||
Binary file not shown.
@@ -1,9 +1,9 @@
|
||||
from flask_mail import Message
|
||||
|
||||
from applications.extensions.init_mail import mail
|
||||
|
||||
|
||||
# send_mail(subject='title', recipients=['123@qq.com'], content='body')
|
||||
from common.extend.mail import mail
|
||||
|
||||
|
||||
def send_mail(subject, recipients, content):
|
||||
try:
|
||||
message = Message(subject=subject, recipients=recipients, body=content)
|
||||
@@ -0,0 +1,14 @@
|
||||
from file_system_uploader import FilesystemUploader
|
||||
from qiniu_uploader import QiniuUploader
|
||||
from upyun_uploader import UpyunUploader
|
||||
from flask import current_app
|
||||
|
||||
uploader: dict = {
|
||||
"filesystem": FilesystemUploader,
|
||||
"qiniu": QiniuUploader,
|
||||
'upyun': UpyunUploader
|
||||
}
|
||||
|
||||
|
||||
def get_uploader():
|
||||
return uploader.get(current_app.config.get("UPLOAD_PROVIDER") or "filesystem")()
|
||||
@@ -0,0 +1,19 @@
|
||||
class BaseUploader(object):
|
||||
def __init__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def store(self, fileobj, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def upload(self, file_obj, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def download(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def delete(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def sync(self):
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import os
|
||||
import posixpath
|
||||
from pathlib import PurePath
|
||||
from shutil import copyfileobj, rmtree
|
||||
|
||||
from PIL.features import codecs
|
||||
from flask import current_app, send_file
|
||||
from werkzeug.utils import safe_join, secure_filename
|
||||
|
||||
from common.utils.uploads.base_uploader import BaseUploader
|
||||
|
||||
|
||||
def hexencode(s):
|
||||
if isinstance(s, (str,)):
|
||||
s = s.encode("utf-8")
|
||||
encoded = codecs.encode(s, "hex")
|
||||
try:
|
||||
encoded = encoded.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
return encoded
|
||||
|
||||
|
||||
class FilesystemUploader(BaseUploader):
|
||||
def __init__(self, base_path=None):
|
||||
super(BaseUploader, self).__init__()
|
||||
self.base_path = base_path or current_app.config.get("UPLOAD_FOLDER")
|
||||
|
||||
def store(self, fileobj, filename):
|
||||
location = os.path.join(self.base_path, filename)
|
||||
directory = os.path.dirname(location)
|
||||
|
||||
if not os.path.exists(directory):
|
||||
os.makedirs(directory)
|
||||
|
||||
with open(location, "wb") as dst:
|
||||
copyfileobj(fileobj, dst, 16384)
|
||||
|
||||
return filename
|
||||
|
||||
def upload(self, file_obj, filename):
|
||||
if len(filename) == 0:
|
||||
raise Exception("Empty filenames cannot be used")
|
||||
|
||||
filename = secure_filename(filename)
|
||||
md5hash = hexencode(os.urandom(16))
|
||||
file_path = posixpath.join(md5hash, filename)
|
||||
|
||||
return self.store(file_obj, file_path)
|
||||
|
||||
def download(self, filename):
|
||||
return send_file(safe_join(self.base_path, filename), as_attachment=True)
|
||||
|
||||
def delete(self, filename):
|
||||
if os.path.exists(os.path.join(self.base_path, filename)):
|
||||
file_path = PurePath(filename).parts[0]
|
||||
rmtree(os.path.join(self.base_path, file_path))
|
||||
return True
|
||||
return False
|
||||
|
||||
def sync(self):
|
||||
pass
|
||||
@@ -0,0 +1,38 @@
|
||||
from qiniu import Auth, put_data
|
||||
from base_uploader import BaseUploader
|
||||
|
||||
access_key = '自己账号的access key'
|
||||
|
||||
secret_key = '自己账号的secret key'
|
||||
|
||||
bucket_name = '空间名'
|
||||
|
||||
|
||||
class QiniuUploader(BaseUploader):
|
||||
|
||||
def __init__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def store(self, fileobj, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def upload(self, file_obj, filename):
|
||||
|
||||
token = Auth(access_key=access_key, secret_key=secret_key).upload_token(bucket=bucket_name)
|
||||
ret, res = put_data(token, None, data=file_obj)
|
||||
ret.get('key')
|
||||
print(ret)
|
||||
print(res)
|
||||
|
||||
if res.status_code != 200:
|
||||
raise Exception("upload failed")
|
||||
return ret, res
|
||||
|
||||
def download(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def delete(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def sync(self):
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,22 @@
|
||||
from base_uploader import BaseUploader
|
||||
import upyun
|
||||
|
||||
|
||||
class UpyunUploader(BaseUploader):
|
||||
def __init__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def store(self, fileobj, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def upload(self, file_obj, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def download(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def delete(self, filename):
|
||||
raise NotImplementedError
|
||||
|
||||
def sync(self):
|
||||
raise NotImplementedError
|
||||
@@ -0,0 +1,67 @@
|
||||
from flask import Blueprint as FlaskBlueprint
|
||||
from flask.views import MethodViewType, MethodView
|
||||
|
||||
|
||||
class Blueprint(FlaskBlueprint):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self._endpoints = []
|
||||
|
||||
def add_url_rule(
|
||||
self,
|
||||
rule,
|
||||
endpoint=None,
|
||||
view_func=None,
|
||||
provide_automatic_options=None,
|
||||
*,
|
||||
parameters=None,
|
||||
tags=None,
|
||||
**options,
|
||||
):
|
||||
|
||||
if view_func is None:
|
||||
raise TypeError("view_func must be provided")
|
||||
|
||||
if endpoint is None:
|
||||
endpoint = view_func.__name__
|
||||
|
||||
# Ensure endpoint name is unique
|
||||
# - to avoid a name clash when registering a MethodView
|
||||
# - to use it as a key internally in endpoint -> doc mapping
|
||||
if endpoint in self._endpoints:
|
||||
endpoint = f"{endpoint}_{len(self._endpoints)}"
|
||||
self._endpoints.append(endpoint)
|
||||
|
||||
if isinstance(view_func, MethodViewType):
|
||||
func = view_func.as_view(endpoint)
|
||||
else:
|
||||
func = view_func
|
||||
|
||||
# Add URL rule in Flask and store endpoint documentation
|
||||
super().add_url_rule(rule, endpoint, func, **options)
|
||||
|
||||
def route(self, rule, *, parameters=None, tags=None, **options):
|
||||
"""Decorator to register view function in application and documentation
|
||||
|
||||
Calls :meth:`add_url_rule <Blueprint.add_url_rule>`.
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
endpoint = options.pop("endpoint", None)
|
||||
self.add_url_rule(
|
||||
rule, endpoint, func, parameters=parameters, tags=tags, **options
|
||||
)
|
||||
return func
|
||||
|
||||
return decorator
|
||||
|
||||
def register_child_bp(self, bp_lists):
|
||||
for bp in bp_lists:
|
||||
self.register_blueprint(bp)
|
||||
|
||||
|
||||
class View(MethodView):
|
||||
pass
|
||||
@@ -1,14 +0,0 @@
|
||||
version: '3'
|
||||
services:
|
||||
flask:
|
||||
build: .
|
||||
ports:
|
||||
- "8000:8000"
|
||||
restart: always
|
||||
links:
|
||||
- mysql:dbserver
|
||||
mysql:
|
||||
image: "mysql:5.7"
|
||||
environment:
|
||||
- MYSQL_ROOT_PASSWORD=root
|
||||
restart: always
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 58 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 44 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 89 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 130 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 63 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 86 KiB |
@@ -0,0 +1,27 @@
|
||||
from flask import Flask
|
||||
import os
|
||||
from common.extend.logger import config_logger
|
||||
from entrance.bps import sys, register_parent_bp
|
||||
from entrance.extensions import init_extensions
|
||||
|
||||
|
||||
def create_app():
|
||||
config_logger()
|
||||
app = Flask(__name__)
|
||||
app.config.from_pyfile('config/config.py')
|
||||
register_parent_bp(app)
|
||||
init_extensions(app)
|
||||
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
|
||||
print(
|
||||
"""
|
||||
|
||||
___ _ _ _ ___ _ _
|
||||
| _ \___ __ _ _ _ /_\ __| |_ __ (_)_ _ | __| |__ _ __| |__
|
||||
| _/ -_) _` | '_| / _ \/ _` | ' \| | ' \ | _|| / _` (_-< / /
|
||||
|_| \___\__,_|_| /_/ \_\__,_|_|_|_|_|_||_| |_| |_\__,_/__/_\_\\
|
||||
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
return app
|
||||
@@ -0,0 +1,19 @@
|
||||
from flask import Flask
|
||||
from typing import List
|
||||
from common.view import Blueprint
|
||||
from modules.sys.view.user import user
|
||||
|
||||
sys = Blueprint('sys', __name__, url_prefix='/sys')
|
||||
sys_lists: List[Blueprint] = [
|
||||
user
|
||||
]
|
||||
sys.register_child_bp(sys_lists)
|
||||
|
||||
parent_lists: List[Blueprint] = [
|
||||
sys
|
||||
]
|
||||
|
||||
|
||||
def register_parent_bp(app: Flask):
|
||||
for bp in parent_lists:
|
||||
app.register_blueprint(bp)
|
||||
@@ -0,0 +1,8 @@
|
||||
broker_url = 'redis://127.0.0.1:6379/1'
|
||||
result_backend = 'rpc://'
|
||||
|
||||
task_serializer = 'json'
|
||||
result_serializer = 'json'
|
||||
accept_content = ['json']
|
||||
timezone = 'Asia/Shanghai'
|
||||
enable_utc = True
|
||||
@@ -0,0 +1,15 @@
|
||||
import os
|
||||
|
||||
# mysql 配置
|
||||
MYSQL_USERNAME: str = os.getenv('MYSQL_USERNAME') or "root"
|
||||
MYSQL_PASSWORD: str = os.getenv('MYSQL_PASSWORD') or "123456"
|
||||
MYSQL_HOST: str = os.getenv('MYSQL_HOST') or "127.0.0.1"
|
||||
MYSQL_PORT: int = int(os.getenv('MYSQL_PORT') or 3306)
|
||||
MYSQL_DATABASE: str = os.getenv('MYSQL_DATABASE') or "PearAdminFlask"
|
||||
|
||||
# mysql 数据库的配置信息
|
||||
SQLALCHEMY_DATABASE_URI: str = f"mysql+pymysql://{MYSQL_USERNAME}:{MYSQL_PASSWORD}@{MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DATABASE}"
|
||||
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = True
|
||||
SQLALCHEMY_ECHO = False
|
||||
SQLALCHEMY_POOL_RECYCLE = 8
|
||||
@@ -0,0 +1,13 @@
|
||||
from flask import Flask
|
||||
|
||||
from common.extend.celeryer import register_celery
|
||||
from common.extend.limit import limiter
|
||||
from common.extend.orm import db
|
||||
from common.extend.migrate import migrate
|
||||
|
||||
|
||||
def init_extensions(app: Flask):
|
||||
db.init_app(app)
|
||||
migrate.init_app(app)
|
||||
register_celery(app)
|
||||
limiter.init_app(app)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user