3
0
Fork 0

fix: add get_page_templates() method for master view

i thought i needed it to do something clever for report views, but
wound up not needing it..  however this seems like a reasonable
abstraction which may come in handy later
This commit is contained in:
Lance Edgar 2025-01-11 19:41:11 -06:00
parent c1e6053aaf
commit 956021dcbf

View file

@ -1777,14 +1777,14 @@ class MasterView(View):
context = self.get_template_context(context) context = self.get_template_context(context)
# first try the template path most specific to this view # first try the template path most specific to this view
template_prefix = self.get_template_prefix() page_templates = self.get_page_templates(template)
mako_path = f'{template_prefix}/{template}.mako' mako_path = page_templates[0]
try: try:
return render_to_response(mako_path, context, request=self.request) return render_to_response(mako_path, context, request=self.request)
except IOError: except IOError:
# failing that, try one or more fallback templates # failing that, try one or more fallback templates
for fallback in self.get_fallback_templates(template): for fallback in page_templates[1:]:
try: try:
return render_to_response(fallback, context, request=self.request) return render_to_response(fallback, context, request=self.request)
except IOError: except IOError:
@ -1815,21 +1815,51 @@ class MasterView(View):
""" """
return context return context
def get_page_templates(self, template):
"""
Returns a list of all templates which can be attempted, to
render the current page. This is called by
:meth:`render_to_response()`.
The list should be in order of preference, e.g. the first
entry will be the most "specific" template, with subsequent
entries becoming more generic.
In practice this method defines the first entry but calls
:meth:`get_fallback_templates()` for the rest.
:param template: Base name for a template (without prefix), e.g.
``'view'``.
:returns: List of template paths to be tried, based on the
specified template. For instance if ``template`` is
``'view'`` this will (by default) return::
[
'/widgets/view.mako',
'/master/view.mako',
]
"""
template_prefix = self.get_template_prefix()
page_templates = [f'{template_prefix}/{template}.mako']
page_templates.extend(self.get_fallback_templates(template))
return page_templates
def get_fallback_templates(self, template): def get_fallback_templates(self, template):
""" """
Returns a list of "fallback" template paths which may be Returns a list of "fallback" template paths which may be
attempted for rendering a view. This is used within attempted for rendering the current page. See also
:meth:`render_to_response()` if the "first guess" template :meth:`get_page_templates()`.
file was not found.
:param template: Base name for a template (without prefix), e.g. :param template: Base name for a template (without prefix), e.g.
``'custom'``. ``'view'``.
:returns: List of full template paths to be tried, based on :returns: List of template paths to be tried, based on the
the specified template. For instance if ``template`` is specified template. For instance if ``template`` is
``'custom'`` this will (by default) return:: ``'view'`` this will (by default) return::
['/master/custom.mako'] ['/master/view.mako']
""" """
return [f'/master/{template}.mako'] return [f'/master/{template}.mako']