From ca578303f10929982b3c6dc3100dc928b27a256d Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Wed, 27 Sep 2017 13:44:40 +0200 Subject: [PATCH] added session flash messages --- ordr2/__init__.py | 1 + ordr2/templates/account/login.jinja2 | 3 ++- ordr2/templates/macros.jinja2 | 14 ++++++++++++++ ordr2/templates/mytemplate.jinja2 | 8 -------- ordr2/views/__init__.py | 17 ++++++++++++++++- 5 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 ordr2/templates/macros.jinja2 delete mode 100644 ordr2/templates/mytemplate.jinja2 diff --git a/ordr2/__init__.py b/ordr2/__init__.py index 097fbd6..f7670f7 100644 --- a/ordr2/__init__.py +++ b/ordr2/__init__.py @@ -22,6 +22,7 @@ def main(global_config, **settings): config.include('.resources') config.include('.models') config.include('.security') + config.include('.views') config.include('pyramid_jinja2') config.scan() diff --git a/ordr2/templates/account/login.jinja2 b/ordr2/templates/account/login.jinja2 index 000564f..54f2651 100644 --- a/ordr2/templates/account/login.jinja2 +++ b/ordr2/templates/account/login.jinja2 @@ -1,4 +1,5 @@ {% extends "ordr2:templates/layout.jinja2" %} +{% import 'ordr2:templates/macros.jinja2' as macros with context %} {% block subtitle %} Login {% endblock subtitle %} @@ -7,7 +8,7 @@

Log in

- session->flashdata('message'); ?> + {{ macros.flash_messages() }}
diff --git a/ordr2/templates/macros.jinja2 b/ordr2/templates/macros.jinja2 new file mode 100644 index 0000000..19720c3 --- /dev/null +++ b/ordr2/templates/macros.jinja2 @@ -0,0 +1,14 @@ +{% macro flash_messages() -%} + {% for queue in ('success', 'info', 'warning', 'error') %} + {% for message in request.session.pop_flash(queue) %} + {% set css_class = 'danger' if queue == 'error' else queue %} + + {% endfor %} + {% endfor %} +{%- endmacro %} diff --git a/ordr2/templates/mytemplate.jinja2 b/ordr2/templates/mytemplate.jinja2 deleted file mode 100644 index 12814dd..0000000 --- a/ordr2/templates/mytemplate.jinja2 +++ /dev/null @@ -1,8 +0,0 @@ -{% extends "ordr2:templates/layout.jinja2" %} - -{% block content %} -
-

Pyramid Alchemy project

-

Welcome to Ordr2, a Pyramid application generated by
Cookiecutter.

-
-{% endblock content %} diff --git a/ordr2/views/__init__.py b/ordr2/views/__init__.py index 5bb534f..7e802be 100644 --- a/ordr2/views/__init__.py +++ b/ordr2/views/__init__.py @@ -1 +1,16 @@ -# package +from collections import namedtuple + + +FlashMessage = namedtuple('FlashMessage', 'message description') + + +def flash(request, channel, message, description=''): + ''' small wrapper around request.session.flash ''' + msg = FlashMessage(message=message, description=description) + request.session.flash(msg, channel, allow_duplicate=False) + + +def includeme(config): + ''' adding a request helper for views ''' + config.add_request_method(flash, 'flash') +