增加命令行新建空白视图功能
This commit is contained in:
@@ -1,17 +1,10 @@
|
||||
from applications import create_app
|
||||
from flask_migrate import Migrate
|
||||
from applications.extensions import db
|
||||
from applications.common.utils.init_databases import init_db
|
||||
|
||||
app = create_app()
|
||||
|
||||
migrate = Migrate(app, db)
|
||||
|
||||
|
||||
@app.cli.command()
|
||||
def init():
|
||||
init_db()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import os
|
||||
from flask import Flask
|
||||
from flask_uploads import configure_uploads
|
||||
|
||||
from applications.common.script import init_script
|
||||
from applications.common.utils.upload import photos
|
||||
from applications.configs import common
|
||||
from applications.extensions import init_plugs
|
||||
@@ -18,12 +20,16 @@ def create_app(config_name=None):
|
||||
# 引入数据库配置
|
||||
app.config.from_object(common)
|
||||
app.config.from_object(config[config_name])
|
||||
|
||||
# 注册各种插件
|
||||
init_plugs(app)
|
||||
|
||||
# 注册路由
|
||||
init_view(app)
|
||||
|
||||
# 注册命令
|
||||
init_script(app)
|
||||
|
||||
# 文件上传
|
||||
configure_uploads(app, photos)
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
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()
|
||||
|
||||
@@ -51,7 +51,7 @@ def execute_fromfile(filename):
|
||||
|
||||
def init_db():
|
||||
if is_exist_database():
|
||||
print('数据库已经存在,为防止误初始化,请收动删除 %s 数据库' % str(DATABASE))
|
||||
print('数据库已经存在,为防止误初始化,请手动删除 %s 数据库' % str(DATABASE))
|
||||
return
|
||||
if init_database():
|
||||
print('数据库%s创建成功' % str(DATABASE))
|
||||
@@ -0,0 +1,92 @@
|
||||
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()
|
||||
@@ -0,0 +1,6 @@
|
||||
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('/','_')}})
|
||||
@@ -0,0 +1,8 @@
|
||||
from flask import Blueprint
|
||||
|
||||
{{name.replace('/','_')}} = Blueprint('{{name.replace('/','_')}}', __name__, url_prefix='/{{name}}')
|
||||
|
||||
|
||||
@{{name.replace('/','_')}}.route('/')
|
||||
def index():
|
||||
return "这是{{name}}路由"
|
||||
Reference in New Issue
Block a user