wcs: card custom title as a template (#55843)
gitea-wip/combo/pipeline/head There was a failure building this commit Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-07-27 10:05:56 +02:00
parent 666dacd26e
commit 14e3aa9f02
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 31 additions and 9 deletions

View File

@ -25,12 +25,12 @@ from django.contrib.postgres.fields import JSONField
from django.db import models
from django.forms import Select
from django.forms import models as model_forms
from django.template import Context, Template, TemplateSyntaxError, VariableDoesNotExist
from django.utils.safestring import mark_safe
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from requests.exceptions import RequestException
from combo import utils
from combo.data.library import register_cell_class
from combo.data.models import CellBase, Page
from combo.utils import requests
@ -978,8 +978,8 @@ class WcsCardInfosCell(CardMixin, CellBase):
def get_card_id(self, context):
if self.card_id:
try:
card_id = utils.get_templated_url('{%% load wcs %%}%s' % self.card_id, context)
except utils.TemplateError:
card_id = Template('{%% load wcs %%}%s' % self.card_id).render(Context(context))
except (VariableDoesNotExist, TemplateSyntaxError):
return None
else:
if card_id:
@ -998,8 +998,9 @@ class WcsCardInfosCell(CardMixin, CellBase):
def get_cell_extra_context(self, context):
extra_context = super().get_cell_extra_context(context)
extra_context['title'] = self.custom_title or self.cached_title
extra_context['schema'] = self.cached_json
# default value used if card is not found
extra_context['title'] = self.cached_title
card_id = self.get_card_id(context)
if not card_id:
@ -1025,6 +1026,13 @@ class WcsCardInfosCell(CardMixin, CellBase):
if response.status_code == 200:
extra_context['card'] = response.json()
try:
extra_context['title'] = Template(self.custom_title).render(Context(extra_context))
except (VariableDoesNotExist, TemplateSyntaxError):
extra_context['title'] = ''
# custom_title gives an empty title, use default value + card text
if not extra_context['title']:
extra_context['title'] = '%s - %s' % (self.cached_title, extra_context['card'].get('text'))
return extra_context

View File

@ -1870,12 +1870,13 @@ def test_card_cell_render(mock_send, context):
page = Page.objects.create(title='xxx', template_name='standard')
cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
cell.carddef_reference = u'default:card_model_1'
cell.custom_title = 'Foo bar {{ card.fields.title }}'
cell.save()
# card id not in context
assert 'card_model_1_id' not in context
result = cell.render(context)
assert '<h2>Card Model 1</h2>' in result
assert '<h2>Card Model 1</h2>' in result # default value
assert '<p>Unknown Card</p>' in result
context['card_model_1_id'] = 11
@ -1892,15 +1893,19 @@ def test_card_cell_render(mock_send, context):
mock_resp.status_code = 500
requests_get.return_value = mock_resp
result = cell.render(context)
assert '<h2>Card Model 1</h2>' in result # default value
assert '<p>Unknown Card</p>' in result
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
requests_get.side_effect = ConnectionError()
result = cell.render(context)
assert '<h2>Card Model 1</h2>' in result # default value
assert '<p>Unknown Card</p>' in result
cell.custom_title = ''
cell.save()
mock_send.reset_mock()
result = cell.render(context)
assert '<h2>Card Model 1</h2>' in result
assert '<h2>Card Model 1 - aa</h2>' in result # custom_title not defined, default value
assert PyQuery(result).find('span.label:contains("Field A") + span').text() == 'a'
assert PyQuery(result).find('span.label:contains("Field B") + span').text() == 'yes'
assert PyQuery(result).find('span.label:contains("Field C") + span').text() == 'Sept. 28, 2020'
@ -1909,11 +1914,20 @@ def test_card_cell_render(mock_send, context):
assert 'related_structured' not in result
assert 'Field D' not in result
cell.custom_title = 'Foo bar'
cell.custom_title = 'Foo bar {{ card.fields.fielda }}'
cell.save()
result = cell.render(context)
assert '<h2>Card Model 1</h2>' not in result
assert '<h2>Foo bar</h2>' in result
assert '<h2>Foo bar a</h2>' in result
cell.custom_title = '{{ foobar }}'
cell.save()
result = cell.render(context)
assert '<h2>Card Model 1 - aa</h2>' in result # empty value from template, default value
cell.custom_title = '{% if %}'
cell.save()
result = cell.render(context)
assert '<h2>Card Model 1 - aa</h2>' in result # template error, default value
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)