backoffice: use a template to render category page (#53667)

This commit is contained in:
Frédéric Péters 2021-05-14 14:03:42 +02:00
parent 6bf202a98d
commit fe12631493
4 changed files with 47 additions and 34 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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 %}