更新插件功能
This commit is contained in:
@@ -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