0001-general-add-support-for-varying-cell-template-based-.patch
combo/data/models.py | ||
---|---|---|
17 | 17 |
import feedparser |
18 | 18 |
import hashlib |
19 | 19 |
import json |
20 |
import os |
|
20 | 21 |
import requests |
21 | 22 |
import subprocess |
22 | 23 | |
... | ... | |
429 | 430 | |
430 | 431 |
def render(self, context): |
431 | 432 |
context.update(self.get_cell_extra_context(context)) |
432 |
tmpl = template.loader.get_template(self.template_name) |
|
433 |
template_names = ['combo/' + self._meta.model_name + '.html'] |
|
434 |
base_template_name = self._meta.model_name + '.html' |
|
435 |
if self.template_name: |
|
436 |
base_template_name = os.path.basename(self.template_name) |
|
437 |
template_names.append(self.template_name) |
|
438 |
if self.slug: |
|
439 |
template_names.append('combo/cells/%s/%s' % (self.slug, base_template_name)) |
|
440 |
template_names.append('combo/%s/cells/%s/%s' % ( |
|
441 |
self._meta.app_label, self.slug, base_template_name)) |
|
442 |
template_names.reverse() |
|
443 |
tmpl = template.loader.select_template(template_names) |
|
433 | 444 |
return tmpl.render(context) |
434 | 445 | |
435 | 446 |
def modify_global_context(self, context, request=None): |
tests/templates-1/combo/cells/foobar/text-cell.html | ||
---|---|---|
1 |
<div class="XXX">{{cell.text|safe}}</div> |
tests/test_cells.py | ||
---|---|---|
1 |
import os |
|
1 | 2 |
import pytest |
2 | 3 | |
3 | 4 |
from combo.data.models import Page, CellBase, TextCell, LinkCell |
4 | 5 |
from django.forms.widgets import Media |
6 |
from django.test import override_settings |
|
5 | 7 | |
6 | 8 |
pytestmark = pytest.mark.django_db |
7 | 9 | |
... | ... | |
90 | 92 |
cell.link_page = None |
91 | 93 |
cell.save() |
92 | 94 |
assert cell.render(ctx).strip() == '<a href="http://example.net/#anchor">altertitle</a>' |
95 | ||
96 | ||
97 |
def test_variant_templates(): |
|
98 |
page = Page(title='example page', slug='example-page') |
|
99 |
page.save() |
|
100 | ||
101 |
cell = TextCell() |
|
102 |
cell.page = page |
|
103 |
cell.text = '<p>foobar</p>' |
|
104 |
cell.order = 0 |
|
105 |
cell.save() |
|
106 | ||
107 |
from django.template import Context |
|
108 |
ctx = Context() |
|
109 |
assert cell.render(ctx).strip() == '<p>foobar</p>' |
|
110 | ||
111 |
with override_settings(TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]): |
|
112 |
assert cell.render(ctx).strip() == '<p>foobar</p>' |
|
113 |
cell.slug = 'foobar' |
|
114 |
cell.save() |
|
115 |
assert cell.render(ctx).strip() == '<div class="XXX"><p>foobar</p></div>' |
|
116 | ||
117 |
assert cell.render(ctx).strip() == '<p>foobar</p>' |
|
93 |
- |