feat: move more subscriber logic to wuttaweb

This commit is contained in:
Lance Edgar 2024-08-05 15:00:11 -05:00
parent 3b92bb3a9e
commit 9d2684046f

View file

@ -52,30 +52,17 @@ def new_request(event):
""" """
Event hook called when processing a new request. Event hook called when processing a new request.
This first invokes the upstream hook: This first invokes the upstream hooks:
:func:`wuttaweb:wuttaweb.subscribers.new_request()`
* :func:`wuttaweb:wuttaweb.subscribers.new_request()`
* :func:`wuttaweb:wuttaweb.subscribers.new_request_set_user()`
It then adds more things to the request object; among them: It then adds more things to the request object; among them:
.. attribute:: request.rattail_config .. attribute:: request.rattail_config
Reference to the app :term:`config object`. Note that this Reference to the app :term:`config object`. Note that this
will be the same as ``request.wutta_config``. will be the same as :attr:`wuttaweb:request.wutta_config`.
.. attribute:: request.user
Reference to the current authenticated user, or ``None``.
.. attribute:: request.is_admin
Flag indicating whether current user is a member of the
Administrator role.
.. attribute:: request.is_root
Flag indicating whether user is currently elevated to root
privileges. This is only possible if ``request.is_admin =
True``.
.. method:: request.has_perm(name) .. method:: request.has_perm(name)
@ -94,10 +81,9 @@ def new_request(event):
then in the base template all registered components will be then in the base template all registered components will be
properly loaded. properly loaded.
""" """
# log.debug("new request: %s", event)
request = event.request request = event.request
# invoke upstream logic # invoke main upstream logic
# nb. this sets request.wutta_config # nb. this sets request.wutta_config
base.new_request(event) base.new_request(event)
@ -109,25 +95,20 @@ def new_request(event):
rattail_config = config rattail_config = config
request.rattail_config = rattail_config request.rattail_config = rattail_config
def user(request): def user_getter(request, db_session=None):
user = None user = base.default_user_getter(request, db_session=db_session)
uuid = request.authenticated_userid if user:
if uuid: # nb. we also assign continuum user to session
app = request.rattail_config.get_app() session = db_session or Session()
model = app.model session.set_continuum_user(user)
user = Session.get(model.User, uuid) return user
if user:
Session().set_continuum_user(user)
return user
request.set_property(user, reify=True) # invoke upstream hook to set user
base.new_request_set_user(event, user_getter=user_getter, db_session=Session())
# assign client IP address to the session, for sake of versioning # assign client IP address to the session, for sake of versioning
Session().continuum_remote_addr = request.client_addr Session().continuum_remote_addr = request.client_addr
request.is_admin = auth.user_is_admin(request.user)
request.is_root = request.is_admin and request.session.get('is_root', False)
# TODO: why would this ever be null? # TODO: why would this ever be null?
if rattail_config: if rattail_config:
@ -286,27 +267,10 @@ def context_found(event):
The following is attached to the request: The following is attached to the request:
* ``get_referrer()`` function
* ``get_session_timeout()`` function * ``get_session_timeout()`` function
""" """
request = event.request request = event.request
def get_referrer(default=None, **kwargs):
if request.params.get('referrer'):
return request.params['referrer']
if request.session.get('referrer'):
return request.session.pop('referrer')
referrer = request.referrer
if (not referrer or referrer == request.current_route_url()
or not referrer.startswith(request.host_url)):
if default:
referrer = default
else:
referrer = request.route_url('home')
return referrer
request.get_referrer = get_referrer
def get_session_timeout(): def get_session_timeout():
""" """
Returns the timeout in effect for the current session Returns the timeout in effect for the current session