wcs: select categories for backoffice submission cell (#52055)
This commit is contained in:
parent
49fc5fb3df
commit
3b17fdace8
|
@ -137,7 +137,7 @@ class WcsCurrentFormsCellForm(WcsFormsMixin, forms.ModelForm):
|
|||
fields = ['wcs_site', 'categories', 'current_forms', 'done_forms', 'include_drafts']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(WcsCurrentFormsCellForm, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
self._init_wcs_site()
|
||||
self._init_categories()
|
||||
|
||||
|
@ -148,6 +148,17 @@ class WcsCurrentDraftsCellForm(WcsFormsMixin, forms.ModelForm):
|
|||
fields = ['wcs_site', 'categories']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(WcsCurrentDraftsCellForm, self).__init__(*args, **kwargs)
|
||||
super().__init__(*args, **kwargs)
|
||||
self._init_wcs_site()
|
||||
self._init_categories()
|
||||
|
||||
|
||||
class BackofficeSubmissionCellForm(WcsFormsMixin, forms.ModelForm):
|
||||
class Meta:
|
||||
model = WcsCurrentDraftsCell
|
||||
fields = ['wcs_site', 'categories']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._init_wcs_site()
|
||||
self._init_categories()
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
import jsonfield.fields
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('wcs', '0024_card_info_user'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='backofficesubmissioncell',
|
||||
name='categories',
|
||||
field=jsonfield.fields.JSONField(blank=True, default=dict, verbose_name='Categories'),
|
||||
),
|
||||
]
|
|
@ -977,13 +977,15 @@ class TrackingCodeInputCell(CellBase):
|
|||
|
||||
|
||||
@register_cell_class
|
||||
class BackofficeSubmissionCell(WcsDataBaseCell):
|
||||
class BackofficeSubmissionCell(WcsDataBaseCell, CategoriesValidityMixin):
|
||||
api_url = '/api/formdefs/?backoffice-submission=on'
|
||||
variable_name = 'all_formdefs'
|
||||
template_name = 'combo/wcs/backoffice_submission.html'
|
||||
cache_duration = 600
|
||||
user_dependant = True
|
||||
|
||||
categories = JSONField(_('Categories'), blank=True)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('Backoffice Submission')
|
||||
|
||||
|
@ -993,14 +995,29 @@ class BackofficeSubmissionCell(WcsDataBaseCell):
|
|||
'js/combo.submission.js',
|
||||
)
|
||||
|
||||
def get_default_form_class(self):
|
||||
from .forms import BackofficeSubmissionCellForm
|
||||
|
||||
return BackofficeSubmissionCellForm
|
||||
|
||||
def get_cell_extra_context(self, context):
|
||||
context = super(BackofficeSubmissionCell, self).get_cell_extra_context(context)
|
||||
all_formdefs = context.pop('all_formdefs')
|
||||
formdefs = {}
|
||||
|
||||
categories_filter = {}
|
||||
if self.categories:
|
||||
for category in self.categories.get('data', []):
|
||||
categories_filter[tuple(category.split(':'))] = True
|
||||
|
||||
# add a fake category where it's missing
|
||||
for key, site_formdefs in all_formdefs.items():
|
||||
if not site_formdefs.get('data'):
|
||||
continue
|
||||
if categories_filter:
|
||||
site_formdefs['data'] = [
|
||||
x for x in site_formdefs['data'] if (key, x.get('category_slug')) in categories_filter
|
||||
]
|
||||
formdefs[key] = site_formdefs
|
||||
for formdef in site_formdefs['data']:
|
||||
if 'category' not in formdef:
|
||||
|
|
|
@ -1822,9 +1822,75 @@ def test_backoffice_submission_cell_render(context):
|
|||
)
|
||||
)
|
||||
result = cell.render(context)
|
||||
assert '/backoffice/submission/a-private-form/' in result
|
||||
assert list(context['all_formdefs'].keys()) == ['default']
|
||||
assert 'h2' in result
|
||||
assert '/backoffice/submission/a-private-form/' in result
|
||||
assert list(context['all_formdefs'].keys()) == ['default']
|
||||
assert 'h2' in result
|
||||
|
||||
# limit to a category
|
||||
cell.categories = {'data': ['default:test-3']}
|
||||
|
||||
result = cell.render(context)
|
||||
assert '/backoffice/submission/a-private-form/' not in result
|
||||
|
||||
requests_get.return_value = MockedRequestResponse(
|
||||
content=json.dumps(
|
||||
{
|
||||
'data': [
|
||||
{
|
||||
'backoffice_submission_url': '/backoffice/submission/a-private-form/',
|
||||
'title': 'Foo',
|
||||
'category_slug': 'test-3',
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
)
|
||||
result = cell.render(context)
|
||||
assert '/backoffice/submission/a-private-form/' in result
|
||||
|
||||
|
||||
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
||||
def test_backoffice_submission_cell_check_validity(mock_send, context):
|
||||
page = Page.objects.create(title='xxx', slug='test_current_forms_cell_render', template_name='standard')
|
||||
cell = BackofficeSubmissionCell.objects.create(page=page, placeholder='content', order=0)
|
||||
|
||||
# no category
|
||||
cell.check_validity()
|
||||
assert ValidityInfo.objects.exists() is False
|
||||
|
||||
# valid categories
|
||||
cell.categories = {'data': ['default:test-3', 'default:test-9']}
|
||||
cell.save()
|
||||
cell.check_validity()
|
||||
assert ValidityInfo.objects.exists() is False
|
||||
|
||||
# can not retrieve data, don't set cell as invalid
|
||||
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
||||
mock_resp = Response()
|
||||
mock_resp.status_code = 500
|
||||
requests_get.return_value = mock_resp
|
||||
cell.check_validity()
|
||||
assert ValidityInfo.objects.exists() is False
|
||||
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
||||
requests_get.side_effect = ConnectionError()
|
||||
cell.check_validity()
|
||||
assert ValidityInfo.objects.exists() is False
|
||||
|
||||
# can not retrieve categories, don't set cell as invalid
|
||||
with mock.patch('combo.apps.wcs.models.requests.get') as requests_get:
|
||||
mock_resp = Response()
|
||||
mock_resp.status_code = 404
|
||||
requests_get.return_value = mock_resp
|
||||
cell.check_validity()
|
||||
assert ValidityInfo.objects.exists() is False
|
||||
|
||||
# invalid category
|
||||
cell.categories = {'data': ['default:foobar', 'default:test-9']}
|
||||
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
|
||||
|
||||
|
||||
def test_manager_link_list_cell_duplicate():
|
||||
|
@ -1954,7 +2020,12 @@ 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, WcsFormsOfCategoryCell]:
|
||||
if klass in [
|
||||
WcsCurrentFormsCell,
|
||||
WcsCurrentDraftsCell,
|
||||
WcsFormsOfCategoryCell,
|
||||
BackofficeSubmissionCell,
|
||||
]:
|
||||
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()]
|
||||
|
|
Loading…
Reference in New Issue