Projet

Général

Profil

0003-manage-add-cell-template-selection-in-options-dialog.patch

Frédéric Péters, 23 juillet 2021 16:14

Télécharger (4,58 ko)

Voir les différences:

Subject: [PATCH 3/5] manage: add cell template selection in options dialog
 (#55792)

 combo/data/models.py                         | 16 ++++++++++++-
 combo/manager/templates/combo/page_view.html |  1 +
 combo/settings.py                            |  2 ++
 tests/test_manager.py                        | 24 ++++++++++++++++++++
 4 files changed, 42 insertions(+), 1 deletion(-)
combo/data/models.py
1006 1006
        )
1007 1007

  
1008 1008
    def get_options_form_class(self):
1009
        return model_forms.modelform_factory(self.__class__, fields=['slug', 'extra_css_class'])
1009
        fields = ['slug', 'extra_css_class']
1010
        widgets = None
1011
        extra_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str())
1012
        if extra_templates:
1013
            fields = ['template_name'] + fields
1014
            template_names = [('', _('Default Value'))] + [
1015
                (k, v['label']) for k, v in extra_templates.items()
1016
            ]
1017
            widgets = {'template_name': forms.Select(choices=template_names)}
1018
        return model_forms.modelform_factory(self.__class__, fields=fields, widgets=widgets)
1010 1019

  
1011 1020
    def get_extra_manager_context(self):
1012 1021
        return {}
......
1115 1124
    def get_cell_extra_context(self, context):
1116 1125
        return {'cell': self}
1117 1126

  
1127
    def get_template_label(self):
1128
        cell_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str()) or {}
1129
        selected_template_infos = cell_templates.get(self.template_name) or {}
1130
        return selected_template_infos.get('label')
1131

  
1118 1132
    def render(self, context):
1119 1133
        context.update(self.get_cell_extra_context(context))
1120 1134
        template_names = ['combo/' + self._meta.model_name + '.html']
combo/manager/templates/combo/page_view.html
145 145
        <h3><span class="handle">⣿</span>
146 146
                <span class="group1">
147 147
                        {{ cell.get_label }}
148
                        {% if cell.template_name %} ({{cell.get_template_label}}){% endif %}
148 149
                        {% if cell.slug %} [{{cell.slug}}] {% endif %}
149 150
                  {% if cell.extra_css_class %}
150 151
                  <span class="extra-css-class">[{{ cell.extra_css_class }}]</span>
combo/settings.py
342 342
    },
343 343
}
344 344

  
345
COMBO_CELL_TEMPLATES = {}
346

  
345 347
COMBO_MAP_LAYER_ASSET_SLOTS = {}
346 348

  
347 349
# known services
tests/test_manager.py
1358 1358
    assert '[CSS]' in app.get('/manage/pages/%s/' % page.id)
1359 1359

  
1360 1360

  
1361
def test_edit_cell_options_template(app, admin_user):
1362
    Page.objects.all().delete()
1363
    page = Page(title='One', slug='one', template_name='standard')
1364
    page.save()
1365
    cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
1366
    cell.save()
1367

  
1368
    app = login(app)
1369
    resp = app.get('/manage/pages/%s/' % page.id)
1370
    resp = resp.click(href='/data_textcell-%s/options' % cell.id)
1371
    assert 'cdata_textcell-%s-template_name' % cell.id not in resp.form.fields
1372
    assert resp.form['cdata_textcell-%s-slug' % cell.id].value == ''
1373
    assert resp.form['cdata_textcell-%s-extra_css_class' % cell.id].value == ''
1374

  
1375
    with override_settings(COMBO_CELL_TEMPLATES={'data_textcell': {'extra': {'label': 'Extra'}}}):
1376
        resp = app.get('/manage/pages/%s/' % page.id)
1377
        resp = resp.click(href='/data_textcell-%s/options' % cell.id)
1378
        resp.form['cdata_textcell-%s-template_name' % cell.id].value = 'extra'
1379

  
1380
        resp = resp.form.submit('submit')
1381
        assert TextCell.objects.get(id=cell.id).template_name == 'extra'
1382
        assert '(Extra)' in app.get('/manage/pages/%s/' % page.id)
1383

  
1384

  
1361 1385
def test_edit_cell_order(app, admin_user):
1362 1386
    Page.objects.all().delete()
1363 1387
    page = Page(title='One', slug='one', template_name='standard')
1364
-