wcs: mark cell as invalid if category is empty (#40564)

This commit is contained in:
Lauréline Guérin 2020-03-16 15:29:11 +01:00
parent 2dc8ffb13f
commit 74f5c41b57
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 57 additions and 2 deletions

View File

@ -598,6 +598,27 @@ class WcsFormsOfCategoryCell(WcsCommonCategoryCell, WcsBlurpMixin):
def is_relevant(self, context):
return bool(self.category_reference)
def check_validity(self):
if not self.category_reference:
return
wcs_key, category_slug = self.category_reference.split(':')
wcs_site = get_wcs_services().get(wcs_key)
categories_response_json = get_wcs_json(wcs_site, 'api/categories/')
if not categories_response_json:
# can not retrieve data, don't report cell as invalid
return
category_found = any(
[c.get('slug') == category_slug
for c in categories_response_json.get('data', [])])
if not category_found:
self.mark_as_invalid('wcs_category_not_found')
return
self.mark_as_valid()
def get_cell_extra_context(self, context):
extra_context = super(WcsFormsOfCategoryCell, self).get_cell_extra_context(context)
extra_context.update(WcsBlurpMixin.get_cell_extra_context(self, context))

View File

@ -2,6 +2,7 @@
{% block cell-content %}
{% block cell-header %}
{% if forms or combo_display_even_empty_categories %}
<h2>{{ title }}</h2>
{% include "combo/asset_picture_fragment.html" %}
{% if description %}
@ -9,8 +10,10 @@
{{ description|safe }}
</div>
{% endif %}
{% endif %}
{% endblock %}
{% if forms or combo_display_even_empty_categories %}
<div class="wcs-forms-of-category-{{slug}}">
<ul>
{% for form in forms %}
@ -38,4 +41,5 @@
</ul>
</div>
{% endif %}
{% endblock %}

View File

@ -724,7 +724,7 @@ def test_forms_of_category_cell_render(context):
cell.category_reference = 'default:test-9'
cell.ordering = 'alpha'
cell.save()
context['synchronous'] = True # to get fresh content
context['synchronous'] = True # to get fresh content
result = cell.render(context)
assert 'form title' in result and 'a second form title' in result
assert result.index('form title') > result.index('a second form title')
@ -762,6 +762,16 @@ def test_forms_of_category_cell_render(context):
assert cell.render_for_search() == ''
assert len(list(cell.get_external_links_data())) == 2
# existing category, but empty
cell.category_reference = 'default:test-1'
cell.save()
result = cell.render(context)
assert '<h2>' not in result
context['combo_display_even_empty_categories'] = True
result = cell.render(context)
assert '<h2>' in result
context.pop('combo_display_even_empty_categories')
@wcs_present
def test_forms_of_category_cell_validity(context):
@ -804,6 +814,26 @@ def test_forms_of_category_cell_validity(context):
assert validity_info.invalid_since is not None
@wcs_present
def test_forms_of_category_cell_check_validity(context):
page = Page.objects.create(title='xxx', slug='test_forms_of_category_cell_render', template_name='standard')
cell = WcsFormsOfCategoryCell.objects.create(page=page, placeholder='content', order=0)
# valid category
cell.category_reference = 'default:test-9'
cell.save()
cell.check_validity()
assert ValidityInfo.objects.exists() is False
# valid category but empty
cell.category_reference = 'default:test-1'
cell.save()
cell.check_validity()
validity_info = ValidityInfo.objects.latest('pk')
assert validity_info.invalid_reason_code == 'wcs_category_not_found'
assert validity_info.invalid_since is not None
@wcs_present
def test_current_drafts_cell_render_unlogged(context):
page = Page(title='xxx', slug='test_current_drafts_cell_render', template_name='standard')
@ -1258,7 +1288,7 @@ def test_hourly():
for klass in cell_classes:
klass.objects.create(page=page, placeholder='content', order=0)
for klass in cell_classes:
if klass in [WcsCurrentFormsCell, WcsCurrentDraftsCell]:
if klass in [WcsCurrentFormsCell, WcsCurrentDraftsCell, WcsFormsOfCategoryCell]:
with mock.patch('combo.apps.wcs.models.%s.check_validity' % klass.__name__) as check_validity:
appconfig.hourly()
assert check_validity.call_args_list == [mock.call()]