wcs: add card identifier on Card Info cell (#50001)
gitea-wip/combo/pipeline/head Build started... Details
gitea/combo/pipeline/head Build started... Details

This commit is contained in:
Lauréline Guérin 2021-01-12 10:15:11 +01:00
parent 71aa546fec
commit 36461f2d48
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 65 additions and 5 deletions

View File

@ -51,7 +51,7 @@ class WcsCardsCellForm(forms.ModelForm):
class WcsCardInfoCellForm(forms.ModelForm):
class Meta:
model = WcsCardInfosCell
fields = ('carddef_reference',)
fields = ('carddef_reference', 'card_id')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('wcs', '0022_cards_user'),
]
operations = [
migrations.AddField(
model_name='wcscardinfoscell',
name='card_id',
field=models.CharField(blank=True, max_length=150, verbose_name='Card Identifier'),
),
]

View File

@ -31,6 +31,7 @@ from requests.exceptions import RequestException
from combo.data.models import CellBase, Page
from combo.data.library import register_cell_class
from combo import utils
from combo.utils import requests
from .utils import get_wcs_json, is_wcs_enabled, get_wcs_services
@ -837,6 +838,7 @@ class WcsCardsCell(CardMixin, WcsBlurpMixin, CellBase):
@register_cell_class
class WcsCardInfosCell(CardMixin, CellBase):
carddef_reference = models.CharField(_('Card Model'), max_length=150)
card_id = models.CharField(_('Card Identifier'), max_length=150, blank=True)
cached_title = models.CharField(_('Title'), max_length=150)
cached_json = JSONField(blank=True)
@ -882,16 +884,30 @@ class WcsCardInfosCell(CardMixin, CellBase):
populate_cache()
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:
return None
else:
if card_id:
return card_id
card_slug = self.carddef_reference.split(':')[1]
card_id = '%s_id' % card_slug
return context.get(card_id) or None
def get_cell_extra_context(self, context):
extra_context = super().get_cell_extra_context(context)
extra_context['title'] = self.cached_title
extra_context['schema'] = self.cached_json
card_slug = self.carddef_reference.split(':')[1]
card_id = '%s_id' % card_slug
if not context.get(card_id):
card_id = self.get_card_id(context)
if not card_id:
return extra_context
api_url = 'api/cards/%s/%s/' % (card_slug, context.get(card_id))
card_slug = self.carddef_reference.split(':')[1]
api_url = 'api/cards/%s/%s/' % (card_slug, card_id)
wcs_site = get_wcs_services().get(self.wcs_site)

View File

@ -27,6 +27,7 @@ from combo.apps.wcs.models import (
WcsFormsOfCategoryCell, WcsCurrentDraftsCell, WcsCategoryCell,
TrackingCodeInputCell, BackofficeSubmissionCell, WcsCareFormsCell,
WcsCardsCell, WcsCardInfosCell)
from combo.apps.wcs.context_processors import Cards
from combo.apps.search.models import SearchCell
from combo.apps.search.utils import index_site, search_site
@ -1374,6 +1375,7 @@ def test_card_cell_render(mock_send, context):
result = cell.render(context)
assert '<p>Unknown Card</p>' in result
mock_send.reset_mock()
result = cell.render(context)
assert '<h2>Card Model 1 - aa</h2>' in result
assert '<span class="label">Field A</span>\n \n <span class="value">a</span>' in result
@ -1383,6 +1385,29 @@ def test_card_cell_render(mock_send, context):
assert 'related_raw' not in result
assert 'related_structured' not in result
# check url called
assert '/api/cards/card_model_1/11/' in mock_send.call_args_list[0][0][0].url
# with identifier
cell.card_id = '42'
cell.save()
mock_send.reset_mock()
result = cell.render(context)
assert '/api/cards/card_model_1/42/' in mock_send.call_args_list[0][0][0].url
context['cards'] = Cards()
cell.card_id = '{% cards|objects:"card_model_1"|last|get:"id" %}' # syntax error
cell.save()
mock_send.reset_mock()
result = cell.render(context)
assert mock_send.call_args_list == []
cell.card_id = '{{ cards|objects:"card_model_1"|last|get:"id" }}'
cell.save()
result = cell.render(context)
assert '/api/cards/card_model_1/list' in mock_send.call_args_list[0][0][0].url
assert '/api/cards/card_model_1/13/' in mock_send.call_args_list[1][0][0].url
def test_tracking_code_cell(app, nocache):
page = Page(title='One', slug='index', template_name='standard')