wuttaweb.subscribers

Event Subscribers

It is assumed that most apps will include this module somewhere during startup. For instance this happens within main():

pyramid_config.include('wuttaweb.subscribers')

This allows for certain common logic to be available for all apps.

However some custom apps may need to supplement or replace the event hooks contained here, depending on the circumstance.

wuttaweb.subscribers.before_render(event)[source]

Event hook called just before rendering a template.

The hook is auto-registered if this module is “included” by Pyramid config object. Or you can explicitly register it:

pyramid_config.add_subscriber('wuttaweb.subscribers.before_render',
                              'pyramid.events.BeforeRender')

This will add some things to the template context dict. Each of these may be used “directly” in a template then, e.g.:

${app.get_title()}

Here are the keys added to context dict by this hook:

'config'

Reference to the app config object.

'app'

Reference to the app handler.

'web'

Reference to the web handler.

'h'

Reference to the helper module, wuttaweb.helpers.

'json'

Reference to the built-in module, json.

'menus'

Set of entries to be shown in the main menu. This is obtained by calling do_make_menus() on the configured MenuHandler.

'url'

Reference to the request method, route_url().

wuttaweb.subscribers.default_user_getter(request, db_session=None)[source]

This is the default function used to retrieve user object from database. Result of this is then assigned to request.user as part of the new_request_set_user() hook.

wuttaweb.subscribers.new_request(event)[source]

Event hook called when processing a new request.

The hook is auto-registered if this module is “included” by Pyramid config object. Or you can explicitly register it:

pyramid_config.add_subscriber('wuttaweb.subscribers.new_request',
                              'pyramid.events.NewRequest')

This will add to the request object:

request.wutta_config

Reference to the app config object.

request.get_referrer(default=None)

Request method to get the “canonical” HTTP referrer value. This has logic to check for referrer in the request params, user session etc.

Parameters:

default – Optional default URL if none is found in request params/session. If no default is specified, the 'home' route is used.

request.use_oruga

Flag indicating whether the frontend should be displayed using Vue 3 + Oruga (if True), or else Vue 2 + Buefy (if False). This flag is False by default.

wuttaweb.subscribers.new_request_set_user(event, user_getter=<function default_user_getter>, db_session=None)[source]

Event hook called when processing a new request, for sake of setting the request.user and similar properties.

The hook is auto-registered if this module is “included” by Pyramid config object. Or you can explicitly register it:

pyramid_config.add_subscriber('wuttaweb.subscribers.new_request_set_user',
                              'pyramid.events.NewRequest')

You may wish to “supplement” this hook by registering your own custom hook and then invoking this one as needed. You can then pass certain params to override only parts of the logic:

Parameters:

This will add to the request object:

request.user

Reference to the authenticated User instance (if logged in), or None.

request.is_admin

Flag indicating whether current user is a member of the Administrator role.

request.is_root

Flag indicating whether user is currently elevated to root privileges. This is only possible if request.is_admin is also true.

request.user_permissions

The set of permission names which are granted to the current user.

This set is obtained by calling get_permissions().

request.has_perm(name)

Shortcut to check if current user has the given permission:

if not request.has_perm('users.edit'):
    raise self.forbidden()
request.has_any_perm(*names)

Shortcut to check if current user has any of the given permissions:

if request.has_any_perm('users.list', 'users.view'):
    return "can either list or view"
else:
    raise self.forbidden()