Browse Source

added session flash messages

php2python
Holger Frey 7 years ago
parent
commit
ca578303f1
  1. 1
      ordr2/__init__.py
  2. 3
      ordr2/templates/account/login.jinja2
  3. 14
      ordr2/templates/macros.jinja2
  4. 8
      ordr2/templates/mytemplate.jinja2
  5. 17
      ordr2/views/__init__.py

1
ordr2/__init__.py

@ -22,6 +22,7 @@ def main(global_config, **settings):
config.include('.resources') config.include('.resources')
config.include('.models') config.include('.models')
config.include('.security') config.include('.security')
config.include('.views')
config.include('pyramid_jinja2') config.include('pyramid_jinja2')
config.scan() config.scan()

3
ordr2/templates/account/login.jinja2

@ -1,4 +1,5 @@
{% extends "ordr2:templates/layout.jinja2" %} {% extends "ordr2:templates/layout.jinja2" %}
{% import 'ordr2:templates/macros.jinja2' as macros with context %}
{% block subtitle %} Login {% endblock subtitle %} {% block subtitle %} Login {% endblock subtitle %}
@ -7,7 +8,7 @@
<div class="row"> <div class="row">
<div class="span6 offset3"> <div class="span6 offset3">
<h1>Log in</h1> <h1>Log in</h1>
<?php echo $this->session->flashdata('message'); ?> {{ macros.flash_messages() }}
</div> </div>
</div> </div>
<div class="row"> <div class="row">

14
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 %}
<div class="alert alert-{{ css_class }} alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>{{message.message|safe}}</strong>
{% if message.description %}
<br>{{message.description|safe}}
{% endif %}
</div>
{% endfor %}
{% endfor %}
{%- endmacro %}

8
ordr2/templates/mytemplate.jinja2

@ -1,8 +0,0 @@
{% extends "ordr2:templates/layout.jinja2" %}
{% block content %}
<div class="content">
<h1><span class="font-semi-bold">Pyramid</span> <span class="smaller">Alchemy project</span></h1>
<p class="lead">Welcome to <span class="font-normal">Ordr2</span>, a&nbsp;Pyramid application generated&nbsp;by<br><span class="font-normal">Cookiecutter</span>.</p>
</div>
{% endblock content %}

17
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')