manage: add cell template selection in options dialog (#55792)

This commit is contained in:
Frédéric Péters 2021-07-23 13:53:31 +02:00
parent b19d5bcd19
commit 8fe4109d3f
4 changed files with 42 additions and 1 deletions

View File

@ -1006,7 +1006,16 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
)
def get_options_form_class(self):
return model_forms.modelform_factory(self.__class__, fields=['slug', 'extra_css_class'])
fields = ['slug', 'extra_css_class']
widgets = None
extra_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str())
if extra_templates:
fields = ['template_name'] + fields
template_names = [('', _('Default Value'))] + [
(k, v['label']) for k, v in extra_templates.items()
]
widgets = {'template_name': forms.Select(choices=template_names)}
return model_forms.modelform_factory(self.__class__, fields=fields, widgets=widgets)
def get_extra_manager_context(self):
return {}
@ -1115,6 +1124,11 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
def get_cell_extra_context(self, context):
return {'cell': self}
def get_template_label(self):
cell_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str()) or {}
selected_template_infos = cell_templates.get(self.template_name) or {}
return selected_template_infos.get('label')
def render(self, context):
context.update(self.get_cell_extra_context(context))
template_names = ['combo/' + self._meta.model_name + '.html']

View File

@ -145,6 +145,7 @@
<h3><span class="handle"></span>
<span class="group1">
{{ cell.get_label }}
{% if cell.template_name %} ({{cell.get_template_label}}){% endif %}
{% if cell.slug %} [{{cell.slug}}] {% endif %}
{% if cell.extra_css_class %}
<span class="extra-css-class">[{{ cell.extra_css_class }}]</span>

View File

@ -342,6 +342,8 @@ COMBO_CELL_ASSET_SLOTS = {
},
}
COMBO_CELL_TEMPLATES = {}
COMBO_MAP_LAYER_ASSET_SLOTS = {}
# known services

View File

@ -1358,6 +1358,30 @@ def test_edit_cell_options(app, admin_user):
assert '[CSS]' in app.get('/manage/pages/%s/' % page.id)
def test_edit_cell_options_template(app, admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')
page.save()
cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
cell.save()
app = login(app)
resp = app.get('/manage/pages/%s/' % page.id)
resp = resp.click(href='/data_textcell-%s/options' % cell.id)
assert 'cdata_textcell-%s-template_name' % cell.id not in resp.form.fields
assert resp.form['cdata_textcell-%s-slug' % cell.id].value == ''
assert resp.form['cdata_textcell-%s-extra_css_class' % cell.id].value == ''
with override_settings(COMBO_CELL_TEMPLATES={'data_textcell': {'extra': {'label': 'Extra'}}}):
resp = app.get('/manage/pages/%s/' % page.id)
resp = resp.click(href='/data_textcell-%s/options' % cell.id)
resp.form['cdata_textcell-%s-template_name' % cell.id].value = 'extra'
resp = resp.form.submit('submit')
assert TextCell.objects.get(id=cell.id).template_name == 'extra'
assert '(Extra)' in app.get('/manage/pages/%s/' % page.id)
def test_edit_cell_order(app, admin_user):
Page.objects.all().delete()
page = Page(title='One', slug='one', template_name='standard')