dev文件夹合并至utils
This commit is contained in:
@@ -44,6 +44,8 @@ def auto_model_jsonify(data, model: db.Model):
|
||||
|
||||
def model_to_dicts(schema: ma.Schema, data):
|
||||
"""
|
||||
将模型查询的数据对象转化为字典
|
||||
|
||||
:param schema: schema类
|
||||
:param model: sqlalchemy查询结果
|
||||
:return: 返回单个查询结果
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
from . import user
|
||||
from . import role
|
||||
from . import power
|
||||
from . import department
|
||||
from . import console
|
||||
from . import gen_captcha
|
||||
from . import http
|
||||
from . import mail
|
||||
from . import rights
|
||||
from . import upload
|
||||
from . import validate
|
||||
from flask import Flask
|
||||
|
||||
# 获取app应用实例,会被初始化插件时重新赋值
|
||||
app = None # type: Flask
|
||||
|
||||
@@ -1,13 +1,83 @@
|
||||
"""
|
||||
集成了对 Pear Admin Flask 二次开发的的邮件操作,并给了相对应的示例。
|
||||
"""
|
||||
from flask import current_app
|
||||
from flask_mail import Message
|
||||
|
||||
from applications.extensions.init_mail import mail
|
||||
from applications.common.curd import model_to_dicts
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.extensions import db, flask_mail
|
||||
from applications.models import Mail
|
||||
from applications.schemas import MailOutSchema
|
||||
|
||||
|
||||
# send_mail(subject='title', recipients=['123@qq.com'], content='body')
|
||||
def send_mail(subject, recipients, content):
|
||||
def get_all(receiver=None, subject=None, content=None):
|
||||
"""
|
||||
获取邮件
|
||||
|
||||
返回的列表中的字典构造如下::
|
||||
|
||||
{
|
||||
"content": "", # html内容
|
||||
"create_at": "2022-12-25T10:51:17", # 时间
|
||||
"id": 17, # 邮件ID
|
||||
"realname": "超级管理", # 创建者
|
||||
"receiver": "", # 接收者
|
||||
"subject": "" # 主题
|
||||
}
|
||||
|
||||
:param receiver: 发送者
|
||||
:param subject: 邮件标题
|
||||
:param content: 邮件内容
|
||||
:return: 列表
|
||||
"""
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if receiver:
|
||||
mf.contains(field_name="receiver", value=receiver)
|
||||
if subject:
|
||||
mf.contains(field_name="subject", value=subject)
|
||||
if content:
|
||||
mf.exact(field_name="content", value=content)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
mail = Mail.query.filter(mf.get_filter(Mail)).layui_paginate()
|
||||
return model_to_dicts(schema=MailOutSchema, data=mail.items)
|
||||
|
||||
|
||||
def add(receiver, subject, content, user_id):
|
||||
"""
|
||||
发送一封邮件,若发送成功立刻提交数据库。
|
||||
|
||||
:param receiver: 接收者 多个用英文逗号隔开
|
||||
:param subject: 邮件主题
|
||||
:param content: 邮件 html
|
||||
:param user_id: 发送用户ID(谁发送的?) 可以用 from flask_login import current_user ; current_user.id 来表示当前登录用户
|
||||
:return: 成功与否
|
||||
"""
|
||||
try:
|
||||
message = Message(subject=subject, recipients=recipients, body=content)
|
||||
mail.send(message)
|
||||
except Exception as e:
|
||||
print('邮箱发送出错了')
|
||||
raise
|
||||
msg = Message(subject=subject, recipients=receiver.split(";"), html=content)
|
||||
flask_mail.send(msg)
|
||||
except BaseException as e:
|
||||
current_app.log_exception(e)
|
||||
return False
|
||||
|
||||
mail = Mail(receiver=receiver, subject=subject, content=content, user_id=user_id)
|
||||
|
||||
db.session.add(mail)
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
|
||||
def delete(id):
|
||||
"""
|
||||
删除邮件记录,立刻写入数据库。
|
||||
|
||||
:param id: 邮件ID
|
||||
:return: 成功与否
|
||||
"""
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return False
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
@@ -2,7 +2,15 @@
|
||||
from flask import abort, make_response, jsonify, escape
|
||||
|
||||
|
||||
def str_escape(s):
|
||||
def str_escape(s: str) -> str:
|
||||
"""xss过滤,内部采用flask自带的过滤函数。
|
||||
与原过滤函数不同的是此过滤函数将在 s 为 None 时返回 None。
|
||||
|
||||
:param s: 要过滤的字符串
|
||||
:type s: str
|
||||
:return: s 为 None 时返回 None,否则过滤字符串后返回。
|
||||
:rtype: str
|
||||
"""
|
||||
if not s:
|
||||
return None
|
||||
return str(escape(s))
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
from applications.dev import user
|
||||
from applications.dev import role
|
||||
from applications.dev import power
|
||||
from applications.dev import department
|
||||
from applications.dev import console
|
||||
from flask import Flask
|
||||
|
||||
# 获取app应用实例,会被初始化插件时重新赋值
|
||||
app = None # type: Flask
|
||||
@@ -1,83 +0,0 @@
|
||||
"""
|
||||
集成了对 Pear Admin Flask 二次开发的的邮件操作,并给了相对应的示例。
|
||||
"""
|
||||
from flask import current_app
|
||||
from flask_mail import Message
|
||||
|
||||
from applications.common.curd import model_to_dicts
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.extensions import db, flask_mail
|
||||
from applications.models import Mail
|
||||
from applications.schemas import MailOutSchema
|
||||
|
||||
|
||||
def get_all(receiver=None, subject=None, content=None):
|
||||
"""
|
||||
获取邮件
|
||||
|
||||
返回的列表中的字典构造如下::
|
||||
|
||||
{
|
||||
"content": "", # html内容
|
||||
"create_at": "2022-12-25T10:51:17", # 时间
|
||||
"id": 17, # 邮件ID
|
||||
"realname": "超级管理", # 创建者
|
||||
"receiver": "", # 接收者
|
||||
"subject": "" # 主题
|
||||
}
|
||||
|
||||
:param receiver: 发送者
|
||||
:param subject: 邮件标题
|
||||
:param content: 邮件内容
|
||||
:return: 列表
|
||||
"""
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if receiver:
|
||||
mf.contains(field_name="receiver", value=receiver)
|
||||
if subject:
|
||||
mf.contains(field_name="subject", value=subject)
|
||||
if content:
|
||||
mf.exact(field_name="content", value=content)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
mail = Mail.query.filter(mf.get_filter(Mail)).layui_paginate()
|
||||
return model_to_dicts(schema=MailOutSchema, data=mail.items)
|
||||
|
||||
|
||||
def add(receiver, subject, content, user_id):
|
||||
"""
|
||||
发送一封邮件,若发送成功立刻提交数据库。
|
||||
|
||||
:param receiver: 接收者 多个用英文逗号隔开
|
||||
:param subject: 邮件主题
|
||||
:param content: 邮件 html
|
||||
:param user_id: 发送用户ID(谁发送的?) 可以用 from flask_login import current_user ; current_user.id 来表示当前登录用户
|
||||
:return: 成功与否
|
||||
"""
|
||||
try:
|
||||
msg = Message(subject=subject, recipients=receiver.split(";"), html=content)
|
||||
flask_mail.send(msg)
|
||||
except BaseException as e:
|
||||
current_app.log_exception(e)
|
||||
return False
|
||||
|
||||
mail = Mail(receiver=receiver, subject=subject, content=content, user_id=user_id)
|
||||
|
||||
db.session.add(mail)
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
|
||||
def delete(id):
|
||||
"""
|
||||
删除邮件记录,立刻写入数据库。
|
||||
|
||||
:param id: 邮件ID
|
||||
:return: 成功与否
|
||||
"""
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return False
|
||||
db.session.commit()
|
||||
return True
|
||||
@@ -8,7 +8,7 @@ import json
|
||||
import traceback
|
||||
import importlib
|
||||
|
||||
import applications.dev
|
||||
import applications.common.utils
|
||||
from applications.common.utils.rights import authorize
|
||||
|
||||
plugin_bp = Blueprint('plugin', __name__, url_prefix='/plugin')
|
||||
@@ -16,7 +16,7 @@ PLUGIN_ENABLE_FOLDERS = []
|
||||
|
||||
def register_plugin_views(app: Flask):
|
||||
global PLUGIN_ENABLE_FOLDERS
|
||||
applications.dev.app = app # 对app重新赋值 便于插件简单调用
|
||||
applications.common.utils.app = app # 对app重新赋值 便于插件简单调用
|
||||
app.register_blueprint(plugin_bp)
|
||||
# 载入插件过程
|
||||
# plugin_folder 配置的是插件的文件夹名
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from applications.dev import *
|
||||
|
||||
from flask import render_template, Blueprint
|
||||
|
||||
# 创建蓝图
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, request
|
||||
from applications.dev import console
|
||||
from applications.common.utils import console
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
|
||||
@@ -2,9 +2,7 @@
|
||||
初始化插件
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, render_template_string
|
||||
from applications.dev import console
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
|
||||
Reference in New Issue
Block a user