Tweens

Introduction

Tweens is a middleware subsystem for Pyramid framework. It allows you to run code before and after processing HTTP request. See main Pyramid article.

Creating a tween

The following tween keeps nagging the user every time a page is loaded if the user profile information is incomplete.

tweens.py:

from pyramid.registry import Registry
from pyramid.renderers import render

from websauna.system.http import Request
from websauna.system.core import messages


class NagTween:
    """Remind user on every page load that the user profile info is incomplete.
    """

    def __init__(self, handler, registry: Registry):
        self.handler = handler
        self.registry = registry

    def nag(self, request):
        html = render("views/profile_nag.html", {}, request=request)
        messages.add(request, kind="info", msg=html, html=True)

    def __call__(self, request: Request):
        user = request.user
        if user:
            if not user.full_name:
                self.nag(request)

        response = self.handler(request)
        return response

profile_nag.html:

Please complete your <a href="{{ 'profile'| route_url }}"><strong>user profile information</strong></a>.

Registering a tween

In your websauna.system.Initializer.configure_tweens():

pyramid.tweens

# ...

def configure_tweens(self):
    super()
    self.config.add_tween("exampleapp.tweens.NagTween", over=pyramid.tweens.MAIN)

Displaying tween stack

Please see ws-tweens command.