借助蓝图机制,实现红图功能,以便进一步细化web模块
1.建立红图类
# 红图类
class Redprint:
def __init__(self, name):
self.name = name
self.mound = []
def route(self, rule, **options):
def decorator(f):
self.mound.append((f, rule, options))
return f
return decorator
def register(self, bp, url_prefix=None):
if url_prefix is None:
url_prefix = '/' + self.name
for f, rule, options in self.mound:
endpoint = self.name + '+' + \
options.pop("endpoint", f.__name__)
bp.add_url_rule(url_prefix + rule, endpoint, f, **options)
2.建立红图实例
from app.libs.redprint import Redprint
api = Redprint('test')
@api.route('/')
def index():
return '<h1>Hello World</h1>'
3.在蓝图上注册红图
from flask import Blueprint
from app.sys.admin.api import temp_test
__author__ = 'ljz'
def create_blueprint_admin():
bp_admin = Blueprint('admin', __name__)
# 在蓝图上注册红图
temp_test.api.register(bp_admin)
return bp_admin
4.测试