python-aspectlib icon indicating copy to clipboard operation
python-aspectlib copied to clipboard

Weaving flask's render_template function

Open N00bAdrian opened this issue 2 years ago • 0 comments

I am working on building software product lines for Flask applications and I am trying to use an aspect to make Flask's render_template function render from another HTML file:

from flask import render_template, Blueprint, url_for, redirect, request
from flask.views import MethodView
...

from aspectlib import Aspect, Proceed, weave

...

bp = Blueprint('home', __name__, template_folder='../templates')

class MessageForm(Form):
    ...

@Aspect
def render_author(*args, **kwargs):
	print("Rendering page with author information...")
	yield Proceed('homeWithAuthor.html', **kwargs)

class HomeView(MethodView):

    def get(self):
        with weave(render_template, render_author):
            form = MessageForm(request.form)
            messages = db.session.execute(db.select(Message)).scalars()
            return render_template(
                "home.html", 
                form=form,
                messages=messages
            )

    def post(self):
        ...
    
bp.add_url_rule("/", view_func=HomeView.as_view("home"))

The app would render from the original file instead of the stated file in the aspect though. There were no errors in weaving and running the app. Upon inspection with a debugger, the aspect has not been called at all. I have tried other arguments in the weaver including

weave('render_template', render_author)
weave('flask.render_template', render_author)
weave(flask.render_template, render_author)
weave('flask', render_author, methods='render_template')

and they all have the same result. The weaver works with builtin functions and class methods so I think it is an issue of me not weaving the aspect correctly.

Any help is appreciated, thanks!

N00bAdrian avatar Apr 04 '23 14:04 N00bAdrian