【问题标题】:How can a flask Addon overwrite a template?烧瓶插件如何覆盖模板?
【发布时间】:2021-08-12 01:30:00
【问题描述】:

我使用“flask fab create-addon”创建了一个烧瓶插件。

我想更改模板 appbuilder/general/security/login_oauth.html 所以我有:

templates
  appbuilder
     general
       security
          login_oauth.html

但是当我加载主机应用程序时,我的 login_oauth.html 版本没有加载。我尝试使用以下代码在this post 中注册蓝图:

from flask import Blueprint
bp = Blueprint('fab_addon_fslogin', __name__, template_folder='templates')

class MyAddOnManager(BaseManager):
    def __init__(self, appbuilder):
        """
        Use the constructor to setup any config keys specific for your app.
        """
        super(MyAddOnManager, self).__init__(appbuilder)
        self.appbuilder.get_app.config.setdefault("MYADDON_KEY", "SOME VALUE")
        self.appbuilder.register_blueprint(bp)

    def register_views(self):
        """
        This method is called by AppBuilder when initializing, use it to add you views
        """
        pass

    def pre_process(self):
        pass

    def post_process(self):
        pass

但是 register_blueprint(bp) 返回:

  File "/home/cquiros/data/projects2017/personal/software/superset/addons/fab_addon_fslogin/fab_addon_fslogin/manager.py", line 24, in __init__
    self.appbuilder.register_blueprint(bp)
  File "/home/cquiros/data/projects2017/personal/software/superset/env_superset/lib/python3.8/site-packages/Flask_AppBuilder-3.3.0-py3.8.egg/flask_appbuilder/base.py", line 643, in register_blueprint
    baseview.create_blueprint(
AttributeError: 'Blueprint' object has no attribute 'create_blueprint'

没有太多关于如何做到这一点的信息。任何线索表示赞赏

【问题讨论】:

    标签: python flask flask-appbuilder


    【解决方案1】:

    如果您想自定义login_oauth.html,最简单的方法是直接将其添加到您的应用程序中,而不是插件。 这意味着login_oauth.html 应该放在这个路径中。

    YourApp
    - app
    -- templates
    --- appbuilder
    ---- general
    ----- security
    ------ login_oauth.html
    

    对于插件的解决方案,函数self.appbuilder.register_blueprint 用于视图而不是Blueprint 函数返回的对象。它应该替换为

    self.appbuilder.get_app.register_blueprint(Blueprint('fab_addon_fslogin', __name__, template_folder='templates')) 
    

    保留蓝图注册和 尝试将login_oauth.html 重命名为login_oauth_xxxx.html

    {your python packages root path}\site-packages\flask_appbuilder\templates\appbuilder\general\security
    

    这将使您可以根据需要覆盖模板。我猜app-builder包的模板搜索顺序大于插件。蓝图的搜索顺序取决于注册的顺序

    终于找到了一个不用重命名文件的技巧,你可以试试下面的

    class MyAddOnManager(BaseManager):
    
    
        def __init__(self, appbuilder):
            """
                 Use the constructor to setup any config keys specific for your app. 
            """
            super(MyAddOnManager, self).__init__(appbuilder)
            self.appbuilder.get_app.config.setdefault('MYADDON_KEY', 'SOME VALUE')
            self.static_bp = Blueprint('fab_addon_fslogin', __name__, template_folder='templates')
    
        def register_views(self):
            """
                This method is called by AppBuilder when initializing, use it to add you views
            """
            pass
    
        def pre_process(self):
            self.appbuilder.get_app.register_blueprint(self.static_bp)
            blueprint_order = self.appbuilder.get_app._blueprint_order
            # move blueprint order of addon to top
            blueprint_order.insert(0, blueprint_order.pop())
    
        def post_process(self):
            pass
    

    参考: flask-appbuilder Customizing

    【讨论】:

    • 你好 Epic Chen。我对插件的猜测是,它们可以扩展/修改模块化的 FAB 应用程序,因此您无需修改​​ FAB 代码。就我而言,FAB 应用程序(Apache SuperSet)不会扩展/修改 login_oauth.html,但我需要我的插件来修改它。我尝试了您的代码: self.appbuilder.get_app.register_blueprint(Blueprint('fab_addon_fslogin', name, template_folder='templates')) 但是当 SuperSet 加载 login_oauth.html 时,它不会加载由插件。
    • 基本上顺序应该是:1)flask_appbuilder的版本,2)FAB App的版本(如果有的话),3)插件的版本。我知道最简单的方法是修改 FAB 应用程序,但就我而言,我无法修改 SuperSet,因为此类更改仅适用于我。
    • 我做过一些调查,因为我提到app-builder包的模板搜索顺序大于插件,所以login_laugh.html不会被覆盖而不重命名,我也检查了代码app-builder和flask的蓝图,我没有找到不修改模块代码的正常方法。
    • 您的猜测可能很直观,但考虑到安全原因,如果插件可能会覆盖主应用程序中的模板,那可能会有风险。此外,由于每个应用程序的开发和发布环境可能是隔离的,我认为如果模块文件无法满足您的需求,我认为修改它是可以的。
    • 如果app-builder包的模板搜索顺序大于addons则不可以。你知道我可以看到这样的订单的文件和行吗?这没有任何意义,似乎是一个设计缺陷。关于您对安全性的评论:这不是风险,因为您需要使用 config.py 中的 ADDON_MANAGERS = [] 指示要加载哪些插件。如果我构建附加组件,那么使用它们就没有风险。
    猜你喜欢
    • 2013-02-26
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多