From 5f039c5f39bacafbedcaa6c7ef1dd27f5345acd7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 29 Dec 2019 10:42:33 +0100
Subject: [PATCH] backoffice: use a template to render data management page
 (#38936)

---
 wcs/backoffice/data_management.py             | 33 ++++++++-----------
 wcs/context_processors.py                     |  3 +-
 wcs/templates/wcs/backoffice/base.html        | 12 +++++++
 .../wcs/backoffice/data-management.html       | 12 +++++++
 wcs/templates/wcs/backoffice/studio.html      |  6 +++-
 5 files changed, 44 insertions(+), 22 deletions(-)
 create mode 100644 wcs/templates/wcs/backoffice/base.html
 create mode 100644 wcs/templates/wcs/backoffice/data-management.html

diff --git a/wcs/backoffice/data_management.py b/wcs/backoffice/data_management.py
index d4763bff0..f2eb888d5 100644
--- a/wcs/backoffice/data_management.py
+++ b/wcs/backoffice/data_management.py
@@ -19,6 +19,7 @@ from quixote.html import TemplateIO, htmltext, htmlescape
 
 from ..qommon import _
 from ..qommon import errors
+from ..qommon import template
 from ..qommon.backoffice.menu import html_top
 
 from wcs.carddef import CardDef
@@ -26,6 +27,7 @@ from .management import ManagementDirectory, FormPage, FormFillPage, FormBackOff
 
 
 class DataManagementDirectory(ManagementDirectory):
+    do_not_call_in_templates = True
     _q_exports = ['']
 
     def _q_traverse(self, path):
@@ -44,30 +46,21 @@ class DataManagementDirectory(ManagementDirectory):
                     return True
         return False
 
-    def _q_index(self):
-        html_top('data_management', _('Cards'))
-        formdefs = CardDef.select(order_by='name', ignore_errors=True, lightweight=True)
-        if len(formdefs) == 0:
-            return self.empty_site_message(_('Cards'))
-        r = TemplateIO(html=True)
-        r += get_session().display_message()
-
-        r += htmltext('<div id="appbar">')
-        r += htmltext('<h2>%s</h2>') % _('Cards')
-        r += htmltext('</div>')
-
+    def get_carddefs(self):
         user = get_request().user
         if user:
-            cards = []
-            for formdef in formdefs:
+            for formdef in CardDef.select(order_by='name', ignore_errors=True, lightweight=True):
                 if user.is_admin or formdef.is_of_concern_for_user(user):
-                    cards.append(formdef)
-            r += htmltext('<ul class="biglist">')
-            for carddef in cards:
-                r += htmltext('<li><strong><a href="%s/">%s</a></strong></li>') % (carddef.url_name, carddef.name)
-            r += htmltext('</ul>')
+                    yield formdef
+
+    def _q_index(self):
+        html_top('data_management', _('Cards'))
+        if CardDef.count() == 0:
+            return self.empty_site_message(_('Cards'))
 
-        return r.getvalue()
+        return template.QommonTemplateResponse(
+                templates=['wcs/backoffice/data-management.html'],
+                context={'view': self})
 
     def _q_lookup(self, component):
         return CardPage(component)
diff --git a/wcs/context_processors.py b/wcs/context_processors.py
index 0413fa4a8..09617ae50 100644
--- a/wcs/context_processors.py
+++ b/wcs/context_processors.py
@@ -14,7 +14,7 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
-from quixote import get_publisher, get_response, get_request
+from quixote import get_publisher, get_response, get_request, get_session
 
 def get_global_context():
     pub = get_publisher()
@@ -31,4 +31,5 @@ def publisher(request):
             'user': lambda: get_request() and get_request().user,
             'template_base': template_base,
             'global_context': get_global_context,
+            'session_message': lambda: get_session() and get_session().display_message(),
             }
diff --git a/wcs/templates/wcs/backoffice/base.html b/wcs/templates/wcs/backoffice/base.html
new file mode 100644
index 000000000..ea74d7f37
--- /dev/null
+++ b/wcs/templates/wcs/backoffice/base.html
@@ -0,0 +1,12 @@
+{% block appbar %}
+<div id="appbar">
+<h2>{% block appbar-title %}{% endblock %}</h2>
+</div>
+{% endblock %}
+
+{% block session-message %}
+{{session_message|safe}}
+{% endblock %}
+
+{% block content %}
+{% endblock %}
diff --git a/wcs/templates/wcs/backoffice/data-management.html b/wcs/templates/wcs/backoffice/data-management.html
new file mode 100644
index 000000000..16ea190ed
--- /dev/null
+++ b/wcs/templates/wcs/backoffice/data-management.html
@@ -0,0 +1,12 @@
+{% extends "wcs/backoffice/base.html" %}
+{% load i18n %}
+
+{% block appbar-title %}{% trans "Cards" %}{% endblock %}
+
+{% block content %}
+<ul class="objects-list single-links">
+  {% for carddef in view.get_carddefs %}
+  <li><a href="{{ carddef.url_name }}/">{{ carddef.name }}</a></li>
+  {% endfor %}
+</ul>
+{% endblock %}
diff --git a/wcs/templates/wcs/backoffice/studio.html b/wcs/templates/wcs/backoffice/studio.html
index 6d40223d7..20cb090dc 100644
--- a/wcs/templates/wcs/backoffice/studio.html
+++ b/wcs/templates/wcs/backoffice/studio.html
@@ -1,9 +1,13 @@
+{% extends "wcs/backoffice/base.html" %}
 {% load i18n %}
 
-{% block body %}
+{% block appbar %}
 <div id="appbar" class="highlight">
 <h2>{% trans "Studio" %}</h2>
 </div>
+{% endblock %}
+
+{% block content %}
 <div id="studio">
 {% if user.can_go_in_backoffice_forms %}
 <a class="button button-paragraph" href="../forms/">{% trans "Forms" context "studio" %}
-- 
2.25.0.rc2

