更新插件功能
This commit is contained in:
@@ -29,4 +29,4 @@ MAIL_USERNAME = '123@qq.com'
|
||||
MAIL_PASSWORD = 'XXXXX' # 生成的授权码
|
||||
|
||||
# 插件配置
|
||||
PLUGIN_ENABLE_FOLDERS = ["HelloWorld", "helloworld"]
|
||||
PLUGIN_ENABLE_FOLDERS = ["helloworld"]
|
||||
@@ -26,7 +26,6 @@ def register_plugin_views(app: Flask):
|
||||
try:
|
||||
with open("plugins/" + plugin_folder + "/__init__.json", "r", encoding='utf-8') as f:
|
||||
plugin_info = json.loads(f.read())
|
||||
print(f"* Plugin: Loading {plugin_info['plugin_name']} ......")
|
||||
# 初始化完成事件
|
||||
try:
|
||||
getattr(importlib.import_module('plugins.' + plugin_folder), "event_init")(app)
|
||||
@@ -34,9 +33,9 @@ def register_plugin_views(app: Flask):
|
||||
pass
|
||||
except BaseException as error:
|
||||
return fail_api(msg="Crash a error! Info: " + str(error))
|
||||
print(f"* Plugin: Loaded {plugin_info['plugin_name']} !")
|
||||
print(f" * Plugin: Loaded plugin: {plugin_info['plugin_name']} .")
|
||||
except BaseException as e:
|
||||
info = f"* Plugin: Crash a error when loading {plugin_info['plugin_name'] if len(plugin_info) != 0 else 'plugin'} :" + "\n"
|
||||
info = f" * Plugin: Crash a error when loading {plugin_info['plugin_name'] if len(plugin_info) != 0 else 'plugin'} :" + "\n"
|
||||
info += 'str(Exception):\t' + str(Exception) + "\n"
|
||||
info += 'str(e):\t\t' + str(e) + "\n"
|
||||
info += 'repr(e):\t' + repr(e) + "\n"
|
||||
|
||||
@@ -212,7 +212,7 @@ INSERT INTO `admin_power` VALUES (57, '邮件管理', '1', 'admin:mail:main', '/
|
||||
INSERT INTO `admin_power` VALUES (58, '邮件发送', '2', 'admin:mail:add', '', '', '57', 'layui-icon layui-icon-ok-circle', 1, '2022-10-11 11:22:26', '2022-10-11 11:22:26', 1);
|
||||
INSERT INTO `admin_power` VALUES (59, '邮件删除', '2', 'admin:mail:remove', '', '', '57', 'layui-icon layui-icon layui-icon-close', 2, '2022-10-11 11:23:06', '2022-10-11 11:23:18', 1);
|
||||
INSERT INTO `admin_power` VALUES (60, '拓展插件', '0', '', '', '', '0', 'layui-icon layui-icon layui-icon-senior', 2, '2022-12-18 12:28:19', '2022-12-18 12:30:25', 1);
|
||||
INSERT INTO `admin_power` VALUES (61, '插件管理', '1', 'admin:plugin:main', '/plugin', '_iframe', '57', 'layui-icon layui-icon layui-icon layui-icon ', 1, '2022-12-18 12:30:13', '2022-12-18 13:57:20', 1);
|
||||
INSERT INTO `admin_power` VALUES (61, '插件管理', '1', 'admin:plugin:main', '/plugin', '_iframe', '60', 'layui-icon layui-icon layui-icon layui-icon ', 1, '2022-12-18 12:30:13', '2022-12-18 13:57:20', 1);
|
||||
INSERT INTO `admin_power` VALUES (62, '启禁插件', '2', 'admin:plugin:enable', '', '', '61', 'layui-icon ', 1, '2022-12-18 13:25:37', '2022-12-18 13:25:37', 1);
|
||||
INSERT INTO `admin_power` VALUES (63, '删除插件', '2', 'admin:plugin:remove', '', '', '61', 'layui-icon layui-icon ', 2, '2022-12-18 13:26:30', '2022-12-18 13:27:17', 1);
|
||||
|
||||
@@ -303,6 +303,10 @@ INSERT INTO `admin_role_power` VALUES (366, 56, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (367, 57, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (368, 58, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (369, 59, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (370, 60, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (371, 61, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (372, 62, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (373, 63, 1);
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for admin_user
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
"""
|
||||
初始化插件
|
||||
"""
|
||||
import os
|
||||
|
||||
from flask import Flask
|
||||
from .main import helloworld_blueprint
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
|
||||
|
||||
def event_enable():
|
||||
"""当此插件被启用时会调用此处"""
|
||||
print("启用插件")
|
||||
print(f"启用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
|
||||
def event_disable():
|
||||
"""当此插件被禁用时会调用此处"""
|
||||
print("禁用插件")
|
||||
print(f"禁用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
def event_init(app: Flask):
|
||||
"""初始化完成时会调用这里"""
|
||||
|
||||
@@ -2,13 +2,6 @@ from applications.dev import *
|
||||
|
||||
from flask import render_template, Blueprint
|
||||
|
||||
import os
|
||||
import psutil
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
|
||||
|
||||
# 创建蓝图
|
||||
helloworld_blueprint = Blueprint('hello_world', __name__, template_folder='templates', static_folder="static",
|
||||
url_prefix="/hello_world")
|
||||
|
||||
@@ -1 +1,67 @@
|
||||
<h1>Test!</h1>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>pear-admin-flask</title>
|
||||
<meta name="description" content="pear-admin-flask">
|
||||
{% include 'admin/common/header.html' %}
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/layui/css/layui.css') }}"/>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/css/main.css') }}"/>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/css/load.css') }}"/>
|
||||
<style>
|
||||
.layui-nav .layui-this:after {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.layui-nav .layui-nav-bar {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- header -->
|
||||
<div class="ew-header">
|
||||
<img class="layui-logo" src="{{ url_for('static', filename='index/images/un28.svg') }}"
|
||||
style="height:50px;padding: 10px;"/>
|
||||
<div class="ew-nav-group">
|
||||
<div class="nav-toggle"><i class="layui-icon layui-icon-more-vertical"></i></div>
|
||||
<ul class="layui-nav" lay-filter="ew-header-nav">
|
||||
<li class="layui-nav-item">
|
||||
<a lay-href="/">首页</a>
|
||||
</li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="/admin" target="_blank">后台</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<!-- banner -->
|
||||
<div class="ew-banner" style="box-shadow: 0 0 8px rgba(0,0,0,.101562);">
|
||||
<div class="layui-container">
|
||||
<div class="layui-row layui-col-space25" style="margin-top: 10px;">
|
||||
<h1>你好世界!这是一个测试页面!</h1>
|
||||
</div>
|
||||
</div>
|
||||
<br/>
|
||||
<br/>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- js部分 -->
|
||||
<script src="{{ url_for('static', filename='admin/component/layui/layui.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='admin/component/pear/pear.js') }}"></script>
|
||||
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'jquery'], function () {
|
||||
var $ = layui.jquery
|
||||
var layer = layui.layer
|
||||
var form = layui.form
|
||||
var jquery = layui.jquery
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugin_name": "真实IP插件",
|
||||
"plugin_version": "1.0.0.1",
|
||||
"plugin_description": "在上游更改访客IP地址,并采用自定义日志输出。",
|
||||
"developer_name": "Yishang",
|
||||
"developer_website": "https://lovepikachu.top",
|
||||
"developer_email": "422880152@qq.com",
|
||||
"developer_phone": "-"
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
"""
|
||||
初始化插件
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, request
|
||||
from applications.dev import console
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
|
||||
|
||||
def event_enable():
|
||||
"""当此插件被启用时会调用此处"""
|
||||
print(f"启用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
|
||||
def event_disable():
|
||||
"""当此插件被禁用时会调用此处"""
|
||||
print(f"禁用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
def event_init(app: Flask):
|
||||
"""初始化完成时会调用这里"""
|
||||
# 移除原有的输出日志
|
||||
app.logger = None
|
||||
log = logging.getLogger('werkzeug')
|
||||
log.setLevel(logging.ERROR)
|
||||
|
||||
# 更改IP地址,只有在最新版的flask中才能生效
|
||||
@app.before_request
|
||||
def before_request():
|
||||
request.remote_addr = get_user_ip(request)
|
||||
|
||||
|
||||
# 使用自定义的日志输出
|
||||
@app.after_request
|
||||
def after_request(rep):
|
||||
if rep.status_code == 200:
|
||||
console.success(f"{request.remote_addr} -- {request.full_path} 200")
|
||||
elif rep.status_code == 404:
|
||||
console.error(f"{request.remote_addr} -- {request.full_path} 404")
|
||||
elif rep.status_code == 500:
|
||||
console.warning(f"{request.remote_addr} -- {request.full_path} 500")
|
||||
else:
|
||||
console.info(f"{request.remote_addr} -- {request.full_path} {rep.status_code}")
|
||||
return rep
|
||||
|
||||
def get_user_ip(request):
|
||||
"""获取用户真实IP"""
|
||||
if 'HTTP_X_FORWARDED_FOR' in request.headers:
|
||||
arr = request.headers['HTTP_X_FORWARDED_FOR'].strip().split(",")
|
||||
i = 0
|
||||
while i < len(arr):
|
||||
if arr[i].find("unknown") != -1:
|
||||
del arr[i]
|
||||
else:
|
||||
i += 1
|
||||
if len(arr) != 0:
|
||||
return arr[0].strip()
|
||||
elif 'HTTP_CLIENT_IP' in request.headers:
|
||||
return request.headers['HTTP_CLIENT_IP']
|
||||
elif 'REMOTE_ADDR' in request.headers:
|
||||
return request.headers['REMOTE_ADDR']
|
||||
elif 'X-Forwarded-For' in request.headers:
|
||||
return request.headers['X-Forwarded-For']
|
||||
return request.remote_addr
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugin_name": "页面替换插件",
|
||||
"plugin_version": "1.0.0.1",
|
||||
"plugin_description": "在不更改原有框架视图函数的情况下更改渲染输出页面。",
|
||||
"developer_name": "Yishang",
|
||||
"developer_website": "https://lovepikachu.top",
|
||||
"developer_email": "422880152@qq.com",
|
||||
"developer_phone": "-"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
初始化插件
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
from flask import Flask, render_template_string
|
||||
from applications.dev import console
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
folder_name = dir_path[dir_path.rfind("/") + 1:] # 插件文件夹名称
|
||||
|
||||
def event_enable():
|
||||
"""当此插件被启用时会调用此处"""
|
||||
print(f"启用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
|
||||
def event_disable():
|
||||
"""当此插件被禁用时会调用此处"""
|
||||
print(f"禁用插件,dir_path: {dir_path} ; folder_name: {folder_name}")
|
||||
|
||||
def event_init(app: Flask):
|
||||
"""初始化完成时会调用这里"""
|
||||
# 使用下面的代码 查看所有注册的视图函数。对于 Flask app.route 函数的实现,请参考 https://www.jianshu.com/p/dff3bc2f4836
|
||||
# print(app.view_functions)
|
||||
|
||||
# 定义新视图函数
|
||||
def new_index():
|
||||
# 规避 render_template 的做法
|
||||
with open(dir_path + "/templates/replacePage_index.html", "r", encoding='utf-8') as f:
|
||||
return render_template_string(f.read())
|
||||
|
||||
# Index.index 是主页的视图函数对应的名称,原视图函数位于 applications/view/index/index.py
|
||||
del app.view_functions['Index.index'] # 释放原视图函数
|
||||
app.view_functions['Index.index'] = new_index # 替换原视图函数
|
||||
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>pear-admin-flask</title>
|
||||
<meta name="description" content="pear-admin-flask">
|
||||
{% include 'admin/common/header.html' %}
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/layui/css/layui.css') }}"/>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/css/main.css') }}"/>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='index/css/load.css') }}"/>
|
||||
<style>
|
||||
.layui-nav .layui-this:after {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.layui-nav .layui-nav-bar {
|
||||
display: none;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<!-- header -->
|
||||
<div class="ew-header">
|
||||
<img class="layui-logo" src="{{ url_for('static', filename='index/images/un28.svg') }}"
|
||||
style="height:50px;padding: 10px;"/>
|
||||
<div class="ew-nav-group">
|
||||
<div class="nav-toggle"><i class="layui-icon layui-icon-more-vertical"></i></div>
|
||||
<ul class="layui-nav" lay-filter="ew-header-nav">
|
||||
<li class="layui-nav-item">
|
||||
<a lay-href="/">首页</a>
|
||||
</li>
|
||||
<li class="layui-nav-item">
|
||||
<a href="/admin" target="_blank">后台</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<!-- banner -->
|
||||
<div class="ew-banner" style="box-shadow: 0 0 8px rgba(0,0,0,.101562);">
|
||||
<div class="layui-container">
|
||||
<div class="layui-row layui-col-space25" style="margin-top: 10px;">
|
||||
<h1>主页被我替换了哈哈哈!</h1>
|
||||
</div>
|
||||
</div>
|
||||
<br/>
|
||||
<br/>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
<!-- js部分 -->
|
||||
<script src="{{ url_for('static', filename='admin/component/layui/layui.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='admin/component/pear/pear.js') }}"></script>
|
||||
|
||||
<script>
|
||||
layui.use(['layer', 'form', 'jquery'], function () {
|
||||
var $ = layui.jquery
|
||||
var layer = layui.layer
|
||||
var form = layui.form
|
||||
var jquery = layui.jquery
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user