From c9b4ed5d4ec3a10a6775ced8de9fc4400e10b12e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 5 May 2019 13:33:04 +0200 Subject: [PATCH] misc: allow variables in URLs in href/src of text cells (#17360) --- combo/data/models.py | 15 ++++++++++++--- tests/test_cells.py | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/combo/data/models.py b/combo/data/models.py index 8a57a837..03198fb3 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -744,8 +744,16 @@ class TextCell(CellBase): def get_cell_extra_context(self, context): extra_context = super(TextCell, self).get_cell_extra_context(context) - extra_context['text'] = self.text + text = self.text render_skeleton = context.get('render_skeleton') + + def sub_variadic_url(match): + attribute = match.group(1) + url = match.group(2) + url = utils.get_templated_url(url, context=context) + return '%s="%s"' % (attribute, url) + text = re.sub(r'(href|src)="(.*?)"', sub_variadic_url, text) + if render_skeleton: request = context.get('request') def sub_src(match): @@ -758,8 +766,9 @@ class TextCell(CellBase): else: url = request.build_absolute_uri(url) return 'href="%s"' % url - extra_context['text'] = re.sub(r'src="(.*?)"', sub_src, self.text) - extra_context['text'] = re.sub(r'href="(.*?)"', sub_href, extra_context['text']) + text = re.sub(r'src="(.*?)"', sub_src, text) + text = re.sub(r'href="(.*?)"', sub_href, text) + extra_context['text'] = text return extra_context diff --git a/tests/test_cells.py b/tests/test_cells.py index 45763343..6f2f5f53 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -72,6 +72,23 @@ def test_additional_label(): assert len(cell.get_additional_label()) < 100 assert '...' in cell.get_additional_label() +def test_text_cell_variadic_url(): + page = Page() + page.save() + + cell = TextCell(page=page, order=0) + cell.text = 'test test2