gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-bank] branch master updated: jinja conversion


From: gnunet
Subject: [GNUnet-SVN] [taler-bank] branch master updated: jinja conversion
Date: Tue, 14 Mar 2017 16:07:20 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository bank.

The following commit(s) were added to refs/heads/master by this push:
     new 921106c  jinja conversion
921106c is described below

commit 921106cea32c78e308a6728542ecc7dc284828d5
Author: Florian Dold <address@hidden>
AuthorDate: Tue Mar 14 14:46:50 2017 +0100

    jinja conversion
---
 talerbank/app/templates/base.html            | 12 +++--
 talerbank/app/templates/login.html           | 21 ++++-----
 talerbank/app/templates/pin_tan.html         |  8 ++--
 talerbank/app/templates/profile_page.html    | 19 ++++----
 talerbank/app/templates/public_accounts.html |  6 +--
 talerbank/app/templates/register.html        | 10 ++---
 talerbank/app/templatetags/__init__.py       |  0
 talerbank/app/templatetags/mystatic.py       | 30 -------------
 talerbank/app/templatetags/settings.py       |  8 ----
 talerbank/app/views.py                       |  5 ++-
 talerbank/jinja2.py                          | 67 ++++++++++++++++++++++++++++
 talerbank/settings_base.py                   | 19 ++++----
 12 files changed, 112 insertions(+), 93 deletions(-)

diff --git a/talerbank/app/templates/base.html 
b/talerbank/app/templates/base.html
index c7a007b..b79e5f1 100644
--- a/talerbank/app/templates/base.html
+++ b/talerbank/app/templates/base.html
@@ -17,14 +17,12 @@
   @author Florian Dold
 -->
 
-{% load static from mystatic %}
-
 <html data-taler-nojs="true">
   <head>
     <title>{{ currency }} Bank - Taler Demo</title>
-    <link rel="stylesheet" type="text/css" href="{% static "pure.css" %}" />
-    <link rel="stylesheet" type="text/css" href="{% static 
"web-common/taler-fallback.css" %}" id="taler-presence-stylesheet" />
-    <script src="{% static "web-common/taler-wallet-lib.js" %}" 
type="application/javascript"></script>
+    <link rel="stylesheet" type="text/css" href="{{ static('pure.css') }}" />
+    <link rel="stylesheet" type="text/css" href="{{ 
static('web-common/taler-fallback.css') }}" id="taler-presence-stylesheet" />
+    <script src="{{ static('web-common/taler-wallet-lib.js') }}" 
type="application/javascript"></script>
     <style>
     .demobar h1 {
       text-align: center;
@@ -99,9 +97,9 @@
   <body>
     <div class="demobar">
       <h1><span class="tt adorn-brackets">Taler Demo</span></h1>
-      <h1><span class="it"><a href="{% url 'index' %}">Bank</a></span></h1>
+      <h1><span class="it"><a href="{{ url('index') }}">Bank</a></span></h1>
       <p>This part of the demo shows how a bank that supports Taler directly 
would work.  In addition to
-      using your own bank account, you can also see the transaction history of 
some <a href="{% url 'public-accounts' %}">Public Accounts</a>.</p>
+      using your own bank account, you can also see the transaction history of 
some <a href="{{ url('public-accounts') }}">Public Accounts</a>.</p>
       <p>Other parts of the demo:</p>
       <ul>
         <li><a href="landing">Introduction</a></li>
diff --git a/talerbank/app/templates/login.html 
b/talerbank/app/templates/login.html
index 3162b8f..99dff60 100644
--- a/talerbank/app/templates/login.html
+++ b/talerbank/app/templates/login.html
@@ -1,5 +1,4 @@
-<!DOCTYPE html>
-<!-- 
+{#
   This file is part of GNU TALER.
   Copyright (C) 2014, 2015, 2016 INRIA
 
@@ -16,13 +15,11 @@
 
   @author Marcello Stanisci
   @author Florian Dold
--->
-
+#}
 {% extends "base.html" %}
-{% load settings_value from settings %}
 
 {% block headermsg %}
-  <h1 class="nav">Welcome to the {% settings_value "TALER_CURRENCY" %} 
Bank!</h1>
+  <h1 class="nav">Welcome to the {{ settings_value("TALER_CURRENCY") }} 
Bank!</h1>
 {% endblock headermsg %}
 
 {% block content %}
@@ -43,15 +40,15 @@
         {% endif %}
 
         {% if next %}
-            {% if user.is_authenticated %}
+            {% if user.is_authenticated() %}
             <p class="informational informational-fail">Your account doesn't 
have access to this page. To proceed,
             please login with an account that has access.</p>
             {% else %}
             <p>Please login to see this page.</p>
             {% endif %}
         {% endif %}
-        <form method="post" class="pure-form" action="{% url 'login' %}">
-          {% csrf_token %}
+        <form method="post" class="pure-form" action="{{ url('login') }}">
+          <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token 
}}">
           {{ form.username }}
           <input type="password" name="password" 
placeholder="password"></input>
           <input type="submit" value="login" class="pure-button 
pure-button-primary" />
@@ -59,13 +56,13 @@
         </form>
       </div>
       <p>
-      If you are a new customer, please <a href="{% url 'register' 
%}">register</a>.
+      If you are a new customer, please <a href="{{ url('register') 
}}">register</a>.
       Registration is fast and gratis, and it gives you a registration bonus
-      of 100 {% settings_value "TALER_CURRENCY" %}!
+      of 100 {{ settings_value("TALER_CURRENCY") }}!
       </p>
       <p>
       To view transactions of public accounts,
-      please <a href="{% url "public-accounts" %}">click here</a>.
+      please <a href="{{ url('public-accounts') }}">click here</a>.
       </p>
     </article>
   </section>
diff --git a/talerbank/app/templates/pin_tan.html 
b/talerbank/app/templates/pin_tan.html
index 244cf9c..7480a8d 100644
--- a/talerbank/app/templates/pin_tan.html
+++ b/talerbank/app/templates/pin_tan.html
@@ -19,8 +19,6 @@
 
 {% extends "base.html" %}
 
-{% load settings_value from settings %}
-
 {% block headermsg %}
   <h1 class="nav">PIN/TAN:  Confirm transaction</h1>
 {% endblock %}
@@ -36,14 +34,14 @@
       </p>
       {% endif %}
       <p>
-        {% settings_value "TALER_CURRENCY" %} Bank needs to verify that you
-        intend to withdraw <b>{{ amount }} {% settings_value "TALER_CURRENCY" 
%}</b> from
+        {{ settings_value("TALER_CURRENCY") }} Bank needs to verify that you
+        intend to withdraw <b>{{ amount }} {{ settings_value("TALER_CURRENCY") 
}}</b> from
         <b>{{ exchange }}</b>.
         To prove that you are the account owner, please answer the
         following &quot;security question&quot; (*):
       </p>
       <form method="post" action="{% url "pin-verify" %}">
-        {% csrf_token %}
+        <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token 
}}">
         {{ form.pin }}
         <input type="hidden" name="question_url" value="{{ 
request.get_full_path }}"></input>
         <input type="submit" value="Ok"></input>
diff --git a/talerbank/app/templates/profile_page.html 
b/talerbank/app/templates/profile_page.html
index ca6d03b..f82c399 100644
--- a/talerbank/app/templates/profile_page.html
+++ b/talerbank/app/templates/profile_page.html
@@ -17,22 +17,21 @@
   @author Marcello Stanisci
 -->
 {% extends "base.html" %}
-{% load static from mystatic %}
 
 {% block head %}
   <meta name="currency" value="{{ currency }}">
   <meta name="precision" value="{{ precision }}">
-  <meta name="callback-url" value="{% url 'pin-question' %}">
+  <meta name="callback-url" value="{{ url('pin-question') }}">
   {% if withdraw and withdraw == "success" %}
     <meta name="reserve-pub" value="{{ reserve_pub }}">
   {% endif %}
   {% if suggested_exchange %}
     <meta name="suggested-exchange" value="{{ suggested_exchange }}">
   {% endif %}
-  <link rel="stylesheet" type="text/css" href="{% static "disabled-button.css" 
%}">
-  <script src="{% static "chrome-store-link.js" %}" 
type="application/javascript"></script>
+  <link rel="stylesheet" type="text/css" href="{{ 
static('disabled-button.css') }}">
+  <script src="{{ static('chrome-store-link.js') }}" 
type="application/javascript"></script>
   {% if use_js %}
-    <script src="{% static "profile-page.js" %}" 
type="application/javascript"></script>
+    <script src="{{ static('profile-page.js') }}" 
type="application/javascript"></script>
   {% endif %}
 {% endblock head %}
 {% block headermsg %}
@@ -40,7 +39,7 @@
 {% endblock headermsg %}
 {% block content %}
   <section id="menu">
-    <a href="{% url 'logout' %}" class="pure-button">[Logout]</a><br>
+    <a href="{{ url('logout') }}" class="pure-button">[Logout]</a><br>
     <p>Account: # {{ account_no }}</p>
     <p>Current balance: {{ balance }} {{ currency }}</p>
   </section>
@@ -84,11 +83,11 @@
               {% if js == 'use_js' %}
                 action=""
               {% else %}
-                action="{% url 'withdraw-nojs' %}"
+                action="{{ url('withdraw-nojs') }}"
                 method="post"
               {% endif %}
               name="tform">
-         {% csrf_token %}
+          <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token 
}}">
           Amount to withdraw:
           <select id="reserve-amount" name="kudos_amount" autofocus>
               <option value="1.00 {{ currency }}">1.00 {{ currency }}</option>
@@ -112,9 +111,9 @@
       </div>
       <p>
       {% if use_js %}
-      You're using the JavaScript version of the bank.  You can <a href="{% 
url 'profile' %}?use_js=false">switch</a> to the JS-free version.
+      You're using the JavaScript version of the bank.  You can <a href="{{ 
url('profile') }}?use_js=false">switch</a> to the JS-free version.
       {% else %}
-      You're using the JavaScript-free version of the bank.  You can <a 
href="{% url 'profile' %}?use_js=true">switch</a> to the JS version.
+      You're using the JavaScript-free version of the bank.  You can <a 
href="{{ url('profile') }}?use_js=true">switch</a> to the JS version.
       {% endif %}
       </p>
     </article>
diff --git a/talerbank/app/templates/public_accounts.html 
b/talerbank/app/templates/public_accounts.html
index 59ed29a..b360af6 100644
--- a/talerbank/app/templates/public_accounts.html
+++ b/talerbank/app/templates/public_accounts.html
@@ -18,8 +18,6 @@
 -->
 {% extends "base.html" %}
 
-{% load static from mystatic %}
-
 {% block headermsg %}
   <h1 class="nav">History of public accounts</h1>
 {% endblock headermsg %}
@@ -29,13 +27,13 @@
   </aside>
   <section id="main">
     <article>
-      <a href="{% url 'index' %}">Back</a>
+      <a href="{{ url('index') }}">Back</a>
       <table bgcolor="#E0E0E0" width="100%" width="100%" border="0" 
cellpadding="2" cellspacing="1">
         <tr>
           {% for account in public_accounts %}
             <td width="12%" align="center">
               <a id="{{ account.user.username }}"
-                 href="{% url "public-accounts" name=account.user.username %}"
+                 href="{{ url("public-accounts", name=account.user.username) 
}}"
                  {% if account.account_no == selected_account.number %}
                  style="font-weight: bold"
                  {% endif %}
diff --git a/talerbank/app/templates/register.html 
b/talerbank/app/templates/register.html
index de7af64..509c689 100644
--- a/talerbank/app/templates/register.html
+++ b/talerbank/app/templates/register.html
@@ -1,4 +1,3 @@
-<!DOCTYPE html>
 <!-- 
   This file is part of GNU TALER.
   Copyright (C) 2014, 2015, 2016 INRIA
@@ -18,10 +17,9 @@
 -->
 
 {% extends "base.html" %}
-{% load settings_value from settings %}
 
 {% block headermsg %}
-  <h1 class="nav">Register to the {% settings_value "TALER_CURRENCY" %} 
bank!</h1>
+  <h1 class="nav">Register to the {{ settings_value('TALER_CURRENCY') }} 
bank!</h1>
 {% endblock headermsg %}
 
 {% block content %}
@@ -29,7 +27,7 @@
   </aside>
   <section id="main">
     <article>
-      <a href="{% url 'index' %}">Back</a>
+      <a href="{{ url('index') }}">Back</a>
       <div class="notification">
         {% if wrong %}
           <p class="informational informational-fail">
@@ -49,8 +47,8 @@
     <article>
       <div class="register-form">
         <h1>Registration form</h1>
-        <form class="pure-form" method="post" action="{% url 'register' %}">
-          {% csrf_token %}
+        <form class="pure-form" method="post" action="{{ url('register') }}">
+          <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token 
}}">
           <input type="text" name="username" placeholder="username" 
autofocus></input>
           <input type="password" name="password" 
placeholder="password"></input>
           <input type="submit" value="Ok" class="pure-button 
pure-button-primary"></input>
diff --git a/talerbank/app/templatetags/__init__.py 
b/talerbank/app/templatetags/__init__.py
deleted file mode 100644
index e69de29..0000000
diff --git a/talerbank/app/templatetags/mystatic.py 
b/talerbank/app/templatetags/mystatic.py
deleted file mode 100644
index 2c57841..0000000
--- a/talerbank/app/templatetags/mystatic.py
+++ /dev/null
@@ -1,30 +0,0 @@
-from django import template
-from django.conf import settings
-from urllib.parse import urlparse
-from django.core.urlresolvers import get_script_prefix
-
-def is_absolute(url):
-    return bool(urlparse(url).netloc)
-
-def join_urlparts(*parts):
-    s = ""
-    i = 0
-    while i < len(parts):
-        n = parts[i]
-        i += 1
-        if s.endswith("/"):
-            n = n.lstrip("/")
-        elif s and  not n.startswith("/"):
-            n = "/" + n
-        s += n
-    return s
-
-
-register = template.Library()
-
address@hidden(takes_context=True)
-def static(context, url):
-    if is_absolute(url):
-        return url
-    request = context["request"]
-    return join_urlparts(get_script_prefix(), settings.STATIC_URL, url)
diff --git a/talerbank/app/templatetags/settings.py 
b/talerbank/app/templatetags/settings.py
deleted file mode 100644
index 08fb084..0000000
--- a/talerbank/app/templatetags/settings.py
+++ /dev/null
@@ -1,8 +0,0 @@
-import django.template
-from django.conf import settings
-
-register = django.template.Library()
-
address@hidden
-def settings_value(name):
-    return getattr(settings, name, "")
diff --git a/talerbank/app/views.py b/talerbank/app/views.py
index d3b8428..a3b8610 100644
--- a/talerbank/app/views.py
+++ b/talerbank/app/views.py
@@ -60,7 +60,10 @@ def javascript_licensing(request):
 def login_view(request):
     just_logged_out = get_session_flag(request, "just_logged_out")
     response = django.contrib.auth.views.login(
-            request, authentication_form=MyAuthenticationForm, 
template_name="login.html")
+            request,
+            authentication_form=MyAuthenticationForm,
+            template_name="login.html",
+            extra_context={"user": request.user})
     # sometimes the response is a redirect and not a template response
     if hasattr(response, "context_data"):
         response.context_data["just_logged_out"] = just_logged_out
diff --git a/talerbank/jinja2.py b/talerbank/jinja2.py
new file mode 100644
index 0000000..bc66a77
--- /dev/null
+++ b/talerbank/jinja2.py
@@ -0,0 +1,67 @@
+#  This file is part of TALER
+#  (C) 2017 INRIA
+#
+#  TALER is free software; you can redistribute it and/or modify it under the
+#  terms of the GNU Affero General Public License as published by the Free 
Software
+#  Foundation; either version 3, or (at your option) any later version.
+#
+#  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
FOR
+#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License along with
+#  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+#
+#  @author Florian Dold
+
+from django.contrib.staticfiles.storage import staticfiles_storage
+from django.core.urlresolvers import reverse
+from django.conf import settings
+from django.core.urlresolvers import get_script_prefix
+from urllib.parse import urlparse
+from jinja2 import Environment
+
+
+def is_absolute(url):
+    return bool(urlparse(url).netloc)
+
+
+def join_urlparts(*parts):
+    s = ""
+    i = 0
+    while i < len(parts):
+        n = parts[i]
+        i += 1
+        if s.endswith("/"):
+            n = n.lstrip("/")
+        elif s and  not n.startswith("/"):
+            n = "/" + n
+        s += n
+    return s
+
+
+def static(url):
+    if is_absolute(url):
+        return url
+    return join_urlparts(get_script_prefix(), settings.STATIC_URL, url)
+
+
+def settings_value(name):
+    return getattr(settings, name, "")
+
+def url(*args, **kwargs):
+    # strangely, Django's 'reverse' function
+    # takes a named parameter 'kwargs' instead
+    # of real kwargs.
+    return reverse(*args, kwargs=kwargs)
+
+
+def environment(**options):
+    env = Environment(**options)
+    env.globals.update({
+        'static': static,
+        'url': url,
+        'settings_value': settings_value,
+    })
+    return env
+
diff --git a/talerbank/settings_base.py b/talerbank/settings_base.py
index f92d9f3..0cc33ed 100644
--- a/talerbank/settings_base.py
+++ b/talerbank/settings_base.py
@@ -69,20 +69,19 @@ MIDDLEWARE_CLASSES = [
 
 TEMPLATES = [
     {
-        'BACKEND': 'django.template.backends.django.DjangoTemplates',
-        'DIRS': [os.path.join(BASE_DIR, "talerbank/app/static/web-common/")],
-        'APP_DIRS': True,
+        'BACKEND': 'django.template.backends.jinja2.Jinja2',
+        'DIRS': [os.path.join(BASE_DIR, "talerbank/app/static/web-common/"),
+                 os.path.join(BASE_DIR, "talerbank/app/templates")],
         'OPTIONS': {
-            'context_processors': [
-                'django.template.context_processors.debug',
-                'django.template.context_processors.request',
-                'django.contrib.auth.context_processors.auth',
-                'django.contrib.messages.context_processors.messages',
-            ],
-        },
+            'environment': 'talerbank.jinja2.environment',
+            },
     },
 ]
 
+# Disable those, since they don't work with
+# jinja2 anyways.
+TEMPLATE_CONTEXT_PROCESSORS = []
+
 WSGI_APPLICATION = 'talerbank.wsgi.application'
 
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]