From 981172af7e9167bb4772d05f9aa60a4650441814 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 14 May 2021 14:03:42 +0200
Subject: [PATCH 1/3] backoffice: use a template to render category page
 (#53667)

---
 tests/admin_pages/test_carddefcategory.py  |  4 +--
 tests/admin_pages/test_category.py         |  4 +--
 wcs/admin/categories.py                    | 41 ++++++----------------
 wcs/templates/wcs/backoffice/category.html | 32 +++++++++++++++++
 4 files changed, 47 insertions(+), 34 deletions(-)
 create mode 100644 wcs/templates/wcs/backoffice/category.html

diff --git a/tests/admin_pages/test_carddefcategory.py b/tests/admin_pages/test_carddefcategory.py
index a23fe7e58..c95655be1 100644
--- a/tests/admin_pages/test_carddefcategory.py
+++ b/tests/admin_pages/test_carddefcategory.py
@@ -75,7 +75,7 @@ def test_categories_edit(pub):
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/cards/categories/1/')
-    assert 'no card model associated to this category' in resp.text
+    assert 'No card model associated to this category' in resp.text
 
     resp = resp.click(href='edit')
     assert resp.forms[0]['name'].value == 'foobar'
@@ -127,7 +127,7 @@ def test_categories_with_carddefs(pub):
 
     resp = app.get('/backoffice/cards/categories/1/')
     assert 'form bar' in resp.text
-    assert 'no card model associated to this category' not in resp.text
+    assert 'No card model associated to this category' not in resp.text
 
 
 def test_categories_delete(pub):
diff --git a/tests/admin_pages/test_category.py b/tests/admin_pages/test_category.py
index c3df90869..9901dff9a 100644
--- a/tests/admin_pages/test_category.py
+++ b/tests/admin_pages/test_category.py
@@ -86,7 +86,7 @@ def test_categories_edit(pub):
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/forms/categories/1/')
-    assert 'no form associated to this category' in resp.text
+    assert 'No form associated to this category' in resp.text
 
     resp = resp.click(href='edit')
     assert resp.forms[0]['name'].value == 'foobar'
@@ -138,7 +138,7 @@ def test_categories_with_formdefs(pub):
 
     resp = app.get('/backoffice/forms/categories/1/')
     assert 'form bar' in resp.text
-    assert 'no form associated to this category' not in resp.text
+    assert 'No form associated to this category' not in resp.text
 
 
 def test_categories_delete(pub):
diff --git a/wcs/admin/categories.py b/wcs/admin/categories.py
index 33c6a7c95..65194624f 100644
--- a/wcs/admin/categories.py
+++ b/wcs/admin/categories.py
@@ -21,7 +21,7 @@ from quixote.html import TemplateIO, htmltext
 from wcs.carddef import CardDef
 from wcs.categories import CardDefCategory, Category
 from wcs.formdef import FormDef
-from wcs.qommon import _
+from wcs.qommon import _, template
 from wcs.qommon.backoffice.menu import html_top
 from wcs.qommon.form import Form, HtmlWidget, StringWidget, WysiwygTextWidget
 
@@ -84,8 +84,9 @@ class CategoryPage(Directory):
     category_ui_class = CategoryUI
     formdef_class = FormDef
     usage_title = _('Forms in this category')
-    empty_message = _('no form associated to this category')
+    empty_message = _('No form associated to this category.')
     _q_exports = ['', 'edit', 'delete', 'description']
+    do_not_call_in_templates = True
 
     def __init__(self, component):
         self.category = self.category_class.get(component)
@@ -94,35 +95,15 @@ class CategoryPage(Directory):
 
     def _q_index(self):
         html_top('categories', title=self.category.name)
-        r = TemplateIO(html=True)
-
-        r += htmltext('<div id="appbar">')
-        r += htmltext('<h2>%s</h2>') % self.category.name
-        r += htmltext('<span class="actions">')
-        r += htmltext('<a href="delete" rel="popup">%s</a>') % _('Delete')
-        r += htmltext('<a href="edit">%s</a>') % _('Edit')
-        r += htmltext('</span>')
-        r += htmltext('</div>')
-
-        if self.category.description:
-            r += htmltext('<div class="bo-block">')
-            r += self.category.get_description_html_text()
-            r += htmltext('</div>')
+        return template.QommonTemplateResponse(
+            templates=['wcs/backoffice/category.html'],
+            context={'view': self, 'category': self.category},
+            is_django_native=True,
+        )
 
+    def get_formdefs(self):
         formdefs = self.formdef_class.select(order_by='name')
-        formdefs = [x for x in formdefs if x.category_id == self.category.id]
-        r += htmltext('<div class="bo-block">')
-        r += htmltext('<h3>%s</h3>') % self.usage_title
-        r += htmltext('<ul>')
-        for formdef in formdefs:
-            r += htmltext('<li><a href="../../%s/">') % str(formdef.id)
-            r += formdef.name
-            r += htmltext('</a></li>')
-        if not formdefs:
-            r += htmltext('<li>%s</li>') % self.empty_message
-        r += htmltext('</ul>')
-        r += htmltext('</div>')
-        return r.getvalue()
+        return [x for x in formdefs if x.category_id == self.category.id]
 
     def edit(self):
         form = self.category_ui.get_form()
@@ -193,7 +174,7 @@ class CardDefCategoryPage(CategoryPage):
     category_ui_class = CardDefCategoryUI
     formdef_class = CardDef
     usage_title = _('Card models in this category')
-    empty_message = _('no card model associated to this category')
+    empty_message = _('No card model associated to this category.')
 
 
 class CategoriesDirectory(Directory):
diff --git a/wcs/templates/wcs/backoffice/category.html b/wcs/templates/wcs/backoffice/category.html
new file mode 100644
index 000000000..1a9c2dc72
--- /dev/null
+++ b/wcs/templates/wcs/backoffice/category.html
@@ -0,0 +1,32 @@
+{% extends "wcs/backoffice.html" %}
+{% load i18n %}
+
+{% block appbar-title %}{{ category.name }}{% endblock %}
+
+{% block appbar-actions %}
+<a href="delete" rel="popup">{% trans "Delete" %}</a>
+<a href="edit">{% trans "Edit" %}</a>
+{% endblock %}
+
+{% block content %}
+{{ block.super }}
+
+{% if category.description %}
+<div class="bo-block">{{ category.get_description_html_text|safe }}</div>
+{% endif %}
+
+<div class="section">
+<h3>{% trans view.usage_title %}</h3>
+{% with view.get_formdefs as formdefs %}
+  {% if formdefs %}
+  <ul class="objects-list single-links">
+  {% for formdef in formdefs %}
+  <li><a href="{{ formdef.get_admin_url }}">{{ formdef.name }}</a></li>
+  {% endfor %}
+  </ul>
+  {% else %}
+  <div><p>{% trans view.empty_message %}</p></div>
+  {% endif %}
+{% endwith %}
+</div>
+{% endblock %}
-- 
2.31.1

