backoffice: add possibility to render templates in a single pass (#49405)

This commit is contained in:
Frédéric Péters 2020-12-12 19:58:46 +01:00
parent 9cb5c5b71b
commit f33c3ecbce
7 changed files with 48 additions and 16 deletions

View File

@ -180,6 +180,7 @@ def test_studio_home(pub):
assert '../forms/' in resp.text
assert '../cards/' in resp.text
assert '../workflows/' in resp.text
assert 'Logged Errors' in resp.text
pub.cfg['admin-permissions'] = {}
for part in ('forms', 'cards', 'workflows'):

View File

@ -32,13 +32,6 @@ class StudioDirectory(Directory):
def __init__(self):
self.logged_errors_dir = LoggedErrorsDirectory(parent_dir=self)
def get_sidebar(self):
r = TemplateIO(html=True)
r += htmltext('<ul id="sidebar-actions">')
r += htmltext('<li><a href="logged-errors/">%s</a></li>') % _('Logged Errors')
r += htmltext('</ul>')
return r.getvalue()
def html_top(self, title):
return html_top('studio', title)
@ -48,8 +41,9 @@ class StudioDirectory(Directory):
def _q_index(self):
self.html_top(_('Studio'))
get_response().filter['sidebar'] = self.get_sidebar()
return template.QommonTemplateResponse(templates=['wcs/backoffice/studio.html'], context={})
return template.QommonTemplateResponse(
templates=['wcs/backoffice/studio.html'], context={'has_sidebar': True}, is_django_native=True
)
@classmethod
def is_visible(cls, *args):

View File

@ -426,9 +426,12 @@ def render(template_name, context):
class QommonTemplateResponse:
def __init__(self, templates, context):
is_django_native = False
def __init__(self, templates, context, is_django_native=False):
self.templates = templates
self.context = context
self.is_django_native = is_django_native
def add_media(self):
if 'form' in self.context:

View File

@ -0,0 +1,6 @@
{% extends "wcs/backoffice.html" %}
{% block main-content %}
{{ body|safe }}
{% endblock %}

View File

@ -34,15 +34,27 @@
{% endblock %}
{% block main-content %}
{{ body|safe }}
{% block content %}
{% block appbar %}
<div id="appbar">
<h2>{% block appbar-title %}{% endblock %}</h2>
<span class="actions">{% block appbar-actions %}{% endblock %}</span>
</div>
{% endblock %}
{% block session-message %}
{{session_message|safe}}
{% endblock %}
{% endblock %}
{% endblock %}
{% block sidebar %}
{% if sidebar %}
{% if sidebar or has_sidebar %}
<aside id="sidebar">
<span id="sidebar-toggle">&#8286;</span>
<div id="sticky-sidebar">
{% block sidebar-content %}
{{ sidebar|safe }}
{% endblock %}
</div>
</aside>
{% endif %}

View File

@ -1,13 +1,14 @@
{% extends "wcs/backoffice/base.html" %}
{% extends "wcs/backoffice.html" %}
{% load i18n %}
{% block content %}
{% 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" %}
@ -26,3 +27,9 @@
{% endif %}
</div>
{% endblock %}
{% block sidebar-content %}
<ul id="sidebar-actions">
<li><a href="logged-errors/">{% trans "Logged Errors" %}</a></li>
</ul>
{% endblock %}

View File

@ -24,7 +24,8 @@ from .qommon import template
class Backoffice(compat.TemplateWithFallbackView):
template_name = 'wcs/backoffice.html'
template_name = 'wcs/backoffice-legacy.html'
template_names = None
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
@ -37,13 +38,21 @@ class Backoffice(compat.TemplateWithFallbackView):
body = get_publisher().try_publish(get_request())
if isinstance(body, template.QommonTemplateResponse):
body.add_media()
body = template.render(body.templates, body.context)
if body.is_django_native:
self.template_names = body.templates[0]
context.update(body.context)
else:
body = template.render(body.templates, body.context)
self.template_names = None
get_publisher().session_manager.finish_successful_request()
self.quixote_response = get_request().response
context.update(template.get_decorate_vars(body, get_response(), generate_breadcrumb=True))
return context
def get_template_names(self):
return self.template_names or [self.template_name]
backoffice = Backoffice.as_view()