Skip to content

Commit

Permalink
🦄 refactor: 重构 flask_app
Browse files Browse the repository at this point in the history
  • Loading branch information
WhaleFell committed Apr 16, 2022
1 parent 5438746 commit c37bb15
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 26 deletions.
9 changes: 0 additions & 9 deletions api.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,6 @@
"sendType": "4"
}
},
{
"desc": "志睿择",
"url": "https://www.vipexam.cn/user/identifyingCode.action",
"method": "POST",
"header": "",
"data": {
"phone": "[phone]"
}
},
{
"desc": "漏洞银行",
"url": "https://www.bugbank.cn/api/verifymobile",
Expand Down
37 changes: 28 additions & 9 deletions flask_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# app 工厂函数
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import sys,os
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView
from flask_babelex import Babel
import sys
import os
from loguru import logger


Expand All @@ -22,19 +26,34 @@
backtrace=True
)

app = Flask(__name__)

# app config

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + \
os.path.join(app.root_path, 'data.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 关闭对模型修改的监控
db = SQLAlchemy(app)

# buleprint
from .views import main as main_blueprint
app.register_blueprint(main_blueprint)
class AppConfig:
SQLALCHEMY_DATABASE_URI = prefix + \
os.path.join(app.root_path, 'data.db') # 数据库路径
SQLALCHEMY_TRACK_MODIFICATIONS = False # 关闭对模型修改的监控
FLASK_ADMIN_SWATCH = "cerulean" # admin 主题

# 密钥
SESSION_TYPE = 'filesystem'
SECRET_KEY = os.urandom(24)

BABEL_DEFAULT_LOCALE = 'zh_CN' # 汉化


app.config.from_object(AppConfig)

# 扩展
db = SQLAlchemy(app)
babel = Babel(app)

admin = Admin(app, name="短信接口调试", template_mode='bootstrap3')
from .model import ApisModelVies,Apis
admin.add_view(ApisModelVies(Apis, db.session))

# buleprint
from .views import main as main_blueprint
app.register_blueprint(main_blueprint)
Binary file modified flask_app/data.db
Binary file not shown.
20 changes: 16 additions & 4 deletions flask_app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@
# 储存数据库模型
from . import db
from datetime import datetime
from . import ModelView

class ApisModelVies(ModelView):
create_template = 'api_edit.html'
edit_template = 'api_edit.html'
# 在当前页面编辑
# create_modal = True
# edit_modal = True
# 启用搜索
column_searchable_list = ['desc']
# 可以导出 csv
can_export = True

class Apis(db.Model):
id = db.Column(db.Integer, primary_key=True) # 主键
desc = db.Column(db.String(100)) # 描述
url = db.Column(db.String(100)) # 链接
method = db.Column(db.String(10)) # 请求方法
desc = db.Column(db.String(100), default="Default") # 描述
url = db.Column(db.String(100), unique=True, nullable=False) # 链接
method = db.Column(db.Enum("GET","POST"), nullable=False) # 请求方法
header = db.Column(db.String(9999)) # 请求头
data = db.Column(db.String(9999)) # 请求数据
add_time = db.Column(db.DateTime(), default=datetime.now()) # 添加时间
add_time = db.Column(db.DateTime(), default=datetime.now) # 添加时间
File renamed without changes.
9 changes: 9 additions & 0 deletions flask_app/templates/api_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'admin/model/edit.html' %}

{% block body %}
<h2>短信接口添加</h2>
<p>替换字符: 手机号<code>[phone]</code> 时间戳<code>[timestamp]</code></p>
{{ super() }}
<input type="button" class="btn btn-primary" value="测试接口">
{% endblock %}

2 changes: 1 addition & 1 deletion flask_app/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

main = Blueprint("main",__name__)

from . import views
from . import views, error
from .. import db
16 changes: 16 additions & 0 deletions flask_app/views/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python python3
# coding=utf-8
from . import main
from flask import redirect, url_for


@main.app_errorhandler(404)
def page_not_found(e):
"""注册应用全局错误处理"""
print("404")
return redirect(url_for('main.index'))


@main.app_errorhandler(401)
def authfail(e):
return redirect('/static/401.jpg')
2 changes: 1 addition & 1 deletion flask_app/views/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding=utf8
# flask app views
from . import main
from .. import db
from flask_app import db

@main.route("/",methods=['GET','POST'])
def index():
Expand Down
10 changes: 8 additions & 2 deletions run.py → run_fask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ def json2sqlite():
header=str(js['header'])
)
# print(api.desc)
db.session.add(api)
db.session.commit()
try:
db.session.add(api)
db.session.commit()
logger.info(f"{api.desc} 写入成功!")
except Exception as e:
db.session.rollback() # 回滚
logger.error(f"{api.desc}写入数据库错误:{e}")

logger.success("json To sqlite 成功!")


Expand Down

0 comments on commit c37bb15

Please sign in to comment.