From 57ff25198a82b3f6f413440e4005f0ade8dfb8d8 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 29 Jul 2021 18:04:48 +0200 Subject: app: render and serve proper web pages --- app/app_sessions.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'app/app_sessions.py') diff --git a/app/app_sessions.py b/app/app_sessions.py index 89521fb..7a931d5 100644 --- a/app/app_sessions.py +++ b/app/app_sessions.py @@ -15,6 +15,33 @@ cookie_key = environ['COOKIE_SECRET_KEY'] SessionManager = partial(SessionMiddleware, secret_key=cookie_key) +class FlashMessageQueue: + """ + Session decorator for managing session flash messages to be displayed to + the user from one page to another. This suits confirmation and error + messages. Messages are stored in the session cookie, which is limited in + size to about 4kb. + """ + + def __init__(self, request: Request): + if 'messages' not in request.session: + request.session['messages'] = [] + + self._messages = request.session['messages'] + + def add(self, class_: str, message: str): + self._messages.append((class_, message)) + + def __iter__(self): + return self + + def __next__(self): + if not self._messages: + raise StopIteration + + return self._messages.pop(0) + + class UserSession: """ Session decorator for managing user login sessions. -- cgit v1.2.3