diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py index 3e30a058..f59e93cf 100644 --- a/combo/apps/wcs/models.py +++ b/combo/apps/wcs/models.py @@ -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)) diff --git a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html index 3dec7166..30cfd8be 100644 --- a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html +++ b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html @@ -2,6 +2,7 @@ {% block cell-content %} {% block cell-header %} +{% if forms or combo_display_even_empty_categories %}

{{ title }}

{% include "combo/asset_picture_fragment.html" %} {% if description %} @@ -9,8 +10,10 @@ {{ description|safe }} {% endif %} +{% endif %} {% endblock %} +{% if forms or combo_display_even_empty_categories %}
+{% endif %} {% endblock %} diff --git a/tests/test_wcs.py b/tests/test_wcs.py index e92e7e2b..e28171a5 100644 --- a/tests/test_wcs.py +++ b/tests/test_wcs.py @@ -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 '

' not in result + context['combo_display_even_empty_categories'] = True + result = cell.render(context) + assert '

' 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()]