wcs: add fields to customize Card display (#54259)
This commit is contained in:
parent
18bb48c5b3
commit
9761e251c5
|
@ -71,10 +71,14 @@ class WcsCardsCellForm(forms.ModelForm):
|
|||
|
||||
class WcsCardInfoCellForm(forms.ModelForm):
|
||||
with_user = forms.BooleanField(label=_('Restrict to cards accessible to the user'), required=False)
|
||||
customize_display = forms.BooleanField(label=_('Customize display'), required=False)
|
||||
|
||||
class Meta:
|
||||
model = WcsCardInfosCell
|
||||
fields = ('carddef_reference', 'custom_title', 'card_id')
|
||||
fields = ('carddef_reference', 'custom_title', 'card_id', 'custom_schema')
|
||||
widgets = {
|
||||
'custom_schema': forms.HiddenInput(),
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
instance = kwargs['instance']
|
||||
|
@ -83,9 +87,16 @@ class WcsCardInfoCellForm(forms.ModelForm):
|
|||
super().__init__(initial=initial, *args, **kwargs)
|
||||
card_models = get_wcs_options('/api/cards/@list')
|
||||
self.fields['carddef_reference'].widget = forms.Select(choices=card_models)
|
||||
if self.instance.custom_schema:
|
||||
self.initial['customize_display'] = True
|
||||
if not self.instance.cached_json:
|
||||
del self.fields['customize_display']
|
||||
del self.fields['custom_schema']
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
if not self.cleaned_data.get('customize_display'):
|
||||
self.instance.custom_schema = {}
|
||||
self.instance.without_user = not self.cleaned_data['with_user']
|
||||
self.instance.save()
|
||||
return self.instance
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.21 on 2021-07-24 12:20
|
||||
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('wcs', '0032_forms_user_can_access'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='wcscardinfoscell',
|
||||
name='custom_schema',
|
||||
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=dict),
|
||||
),
|
||||
]
|
|
@ -927,6 +927,7 @@ class WcsCardInfosCell(CardMixin, CellBase):
|
|||
carddef_reference = models.CharField(_('Card Model'), max_length=150)
|
||||
card_id = models.CharField(_('Card Identifier'), max_length=150, blank=True)
|
||||
without_user = models.BooleanField(_('Ignore the logged-in user'), default=False)
|
||||
custom_schema = JSONField(blank=True, default=dict)
|
||||
|
||||
cached_title = models.CharField(_('Title'), max_length=150)
|
||||
custom_title = models.CharField(_('Custom Title'), max_length=150, blank=True)
|
||||
|
|
|
@ -1710,8 +1710,9 @@ def test_cards_cell_render_user(mock_send, context, nocache):
|
|||
|
||||
|
||||
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
||||
def test_card_cell_setup(mock_send):
|
||||
cell = WcsCardInfosCell()
|
||||
def test_card_cell_setup(mock_send, app, admin_user):
|
||||
page = Page.objects.create(title='xxx', slug='test_card_cell_save_cache', template_name='standard')
|
||||
cell = WcsCardInfosCell(page=page, placeholder='content', order=0)
|
||||
form_class = cell.get_default_form_class()
|
||||
form = form_class(instance=cell)
|
||||
assert form.fields['carddef_reference'].widget.choices == [
|
||||
|
@ -1722,6 +1723,36 @@ def test_card_cell_setup(mock_send):
|
|||
('other:card_model_2', 'test2 : Card Model 2'),
|
||||
('other:card_model_3', 'test2 : Card Model 3'),
|
||||
]
|
||||
assert 'customize_display' not in form.fields
|
||||
assert 'custom_schema' not in form.fields
|
||||
|
||||
cell.save()
|
||||
assert 'customize_display' not in form.fields
|
||||
assert 'custom_schema' not in form.fields
|
||||
|
||||
cell.carddef_reference = 'default:card_model_1'
|
||||
cell.save()
|
||||
form = form_class(instance=cell)
|
||||
assert 'customize_display' in form.fields
|
||||
assert 'custom_schema' in form.fields
|
||||
assert 'customize_display' not in form.initial
|
||||
assert form.initial['custom_schema'] == {}
|
||||
|
||||
cell.custom_schema = {'foo': 'bar'}
|
||||
cell.save()
|
||||
form = form_class(instance=cell)
|
||||
assert 'customize_display' in form.fields
|
||||
assert 'custom_schema' in form.fields
|
||||
assert form.initial['customize_display'] is True
|
||||
assert form.initial['custom_schema'] == {'foo': 'bar'}
|
||||
|
||||
app = login(app)
|
||||
resp = app.get('/manage/pages/%s/' % page.pk)
|
||||
assert resp.forms[0]['c%s-customize_display' % cell.get_reference()].value == 'on'
|
||||
resp.forms[0]['c%s-customize_display' % cell.get_reference()].value = False
|
||||
resp.forms[0].submit().follow()
|
||||
cell.refresh_from_db()
|
||||
assert cell.custom_schema == {}
|
||||
|
||||
|
||||
@mock.patch('combo.apps.wcs.utils.requests.send', side_effect=mocked_requests_send)
|
||||
|
@ -1810,7 +1841,7 @@ def test_manager_card_cell(mock_send, app, admin_user):
|
|||
assert cell.without_user is False
|
||||
assert resp.forms[0]['c%s-with_user' % cell.get_reference()].value == 'on'
|
||||
resp.forms[0]['c%s-with_user' % cell.get_reference()].value = False
|
||||
resp.forms[0].submit().follow()
|
||||
resp = resp.forms[0].submit().follow()
|
||||
cell.refresh_from_db()
|
||||
assert cell.without_user is True
|
||||
assert resp.forms[0]['c%s-with_user' % cell.get_reference()].value is None
|
||||
|
|
Loading…
Reference in New Issue