From 41fe3a3a1d586f698ddc47ffefb5f10e8752c41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 29 Sep 2016 15:30:03 +0200 Subject: [PATCH] general: add support for varying cell template based on slug (#7141) --- combo/data/models.py | 11 +++++++++- .../templates-1/combo/cells/foobar/text-cell.html | 1 + tests/test_cells.py | 25 ++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 tests/templates-1/combo/cells/foobar/text-cell.html diff --git a/combo/data/models.py b/combo/data/models.py index 8443665..3c238fd 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -17,6 +17,7 @@ import feedparser import hashlib import json +import os import requests import subprocess @@ -429,7 +430,15 @@ class CellBase(models.Model): def render(self, context): context.update(self.get_cell_extra_context(context)) - tmpl = template.loader.get_template(self.template_name) + template_names = ['combo/' + self._meta.model_name + '.html'] + base_template_name = self._meta.model_name + '.html' + if self.template_name: + base_template_name = os.path.basename(self.template_name) + template_names.append(self.template_name) + if self.slug: + template_names.append('combo/cells/%s/%s' % (self.slug, base_template_name)) + template_names.reverse() + tmpl = template.loader.select_template(template_names) return tmpl.render(context) def modify_global_context(self, context, request=None): diff --git a/tests/templates-1/combo/cells/foobar/text-cell.html b/tests/templates-1/combo/cells/foobar/text-cell.html new file mode 100644 index 0000000..8d5d546 --- /dev/null +++ b/tests/templates-1/combo/cells/foobar/text-cell.html @@ -0,0 +1 @@ +
{{cell.text|safe}}
diff --git a/tests/test_cells.py b/tests/test_cells.py index 08e3be7..da5446f 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -1,7 +1,9 @@ +import os import pytest from combo.data.models import Page, CellBase, TextCell, LinkCell from django.forms.widgets import Media +from django.test import override_settings pytestmark = pytest.mark.django_db @@ -90,3 +92,26 @@ def test_link_cell(): cell.link_page = None cell.save() assert cell.render(ctx).strip() == 'altertitle' + + +def test_variant_templates(): + page = Page(title='example page', slug='example-page') + page.save() + + cell = TextCell() + cell.page = page + cell.text = '

foobar

' + cell.order = 0 + cell.save() + + from django.template import Context + ctx = Context() + assert cell.render(ctx).strip() == '

foobar

' + + with override_settings(TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]): + assert cell.render(ctx).strip() == '

foobar

' + cell.slug = 'foobar' + cell.save() + assert cell.render(ctx).strip() == '

foobar

' + + assert cell.render(ctx).strip() == '

foobar

' -- 2.9.3