Projet

Général

Profil

0005-general-allow-custom-cell-templates-to-define-extra-.patch

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

Télécharger (5,59 ko)

Voir les différences:

Subject: [PATCH 5/5] general: allow custom cell templates to define extra
 classes (#55792)

 combo/data/models.py | 14 +++++++++++++-
 tests/test_cells.py  |  9 ++++++++-
 tests/test_public.py | 18 +++++++++++-------
 3 files changed, 32 insertions(+), 9 deletions(-)
combo/data/models.py
772 772

  
773 773
    @property
774 774
    def css_class_names(self):
775
        return ' '.join([self.class_name, self.legacy_class_name, self.extra_css_class])
775
        return ' '.join(
776
            [
777
                self.class_name,
778
                self.legacy_class_name,
779
                self.get_template_extra_css_classes(),
780
                self.extra_css_class,
781
            ]
782
        )
776 783

  
777 784
    @property
778 785
    def asset_css_classes(self):
......
1129 1136
        selected_template_infos = cell_templates.get(self.template_name) or {}
1130 1137
        return selected_template_infos.get('label')
1131 1138

  
1139
    def get_template_extra_css_classes(self):
1140
        cell_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str()) or {}
1141
        selected_template_infos = cell_templates.get(self.template_name) or {}
1142
        return selected_template_infos.get('extra-css-classes') or ''
1143

  
1132 1144
    def render(self, context):
1133 1145
        context.update(self.get_cell_extra_context(context))
1134 1146
        template_names = ['combo/' + self._meta.model_name + '.html']
tests/test_cells.py
437 437
    templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]
438 438
    with override_settings(
439 439
        COMBO_CELL_TEMPLATES={
440
            'data_textcell': {'extra': {'label': 'Extra', 'template': 'combo/cells/foobar/text-cell.html'}}
440
            'data_textcell': {
441
                'extra': {
442
                    'label': 'Extra',
443
                    'template': 'combo/cells/foobar/text-cell.html',
444
                    'extra-css-classes': 'plop',
445
                }
446
            }
441 447
        },
442 448
        TEMPLATES=templates_settings,
443 449
    ):
......
445 451
        cell.template_name = 'extra'
446 452
        cell.save()
447 453
        assert cell.render(ctx).strip() == '<div class="XXX"><p>foobar</p></div>'
454
        assert 'plop' in cell.css_class_names
448 455

  
449 456

  
450 457
def mocked_request(*args, **kwargs):
tests/test_public.py
1185 1185
    assert cell.asset_css_classes == ''
1186 1186
    # and test asset preload
1187 1187
    resp = app.get('/', status=200)
1188
    assert 'class="cell text-cell textcell   foo"' in resp.text
1188
    assert 'class="cell text-cell textcell foo"' in re.sub(r' +', ' ', resp.text)
1189 1189

  
1190 1190
    settings.COMBO_CELL_ASSET_SLOTS = {'data_textcell': {'picture': {'prefix': 'Picture'}}}
1191 1191
    cell = TextCell.objects.get(pk=cell.pk)
1192 1192
    assert cell.asset_css_classes == ''
1193 1193
    resp = app.get('/', status=200)
1194
    assert 'class="cell text-cell textcell   foo"' in resp.text
1194
    assert 'class="cell text-cell textcell foo"' in re.sub(r' +', ' ', resp.text)
1195 1195

  
1196 1196
    Asset.objects.create(key=cell.get_asset_slot_key('picture'), asset=File(StringIO('test'), 'test.png'))
1197 1197
    cell = TextCell.objects.get(pk=cell.pk)
1198 1198
    assert cell.asset_css_classes == 'has-asset-picture has-any-asset has-all-assets'
1199 1199
    resp = app.get('/', status=200)
1200
    assert 'class="cell text-cell textcell  has-asset-picture has-any-asset has-all-assets foo"' in resp.text
1200
    assert 'class="cell text-cell textcell has-asset-picture has-any-asset has-all-assets foo"' in re.sub(
1201
        r' +', ' ', resp.text
1202
    )
1201 1203

  
1202 1204
    settings.COMBO_CELL_ASSET_SLOTS = {
1203 1205
        'data_textcell': {'picture': {'prefix': 'Picture'}, 'foo': {'prefix': 'Foo'}}
......
1205 1207
    cell = TextCell.objects.get(pk=cell.pk)
1206 1208
    assert cell.asset_css_classes == 'has-asset-picture has-any-asset'
1207 1209
    resp = app.get('/', status=200)
1208
    assert 'class="cell text-cell textcell  has-asset-picture has-any-asset foo"' in resp.text
1210
    assert 'class="cell text-cell textcell has-asset-picture has-any-asset foo"' in re.sub(
1211
        r' +', ' ', resp.text
1212
    )
1209 1213

  
1210 1214
    Asset.objects.create(key=cell.get_asset_slot_key('foo'), asset=File(StringIO('test'), 'test.png'))
1211 1215
    cell = TextCell.objects.get(pk=cell.pk)
1212 1216
    assert cell.asset_css_classes == 'has-asset-foo has-asset-picture has-any-asset has-all-assets'
1213 1217
    resp = app.get('/', status=200)
1214 1218
    assert (
1215
        'class="cell text-cell textcell  has-asset-foo has-asset-picture has-any-asset has-all-assets foo"'
1216
        in resp.text
1219
        'class="cell text-cell textcell has-asset-foo has-asset-picture has-any-asset has-all-assets foo"'
1220
        in re.sub(r' +', ' ', resp.text)
1217 1221
    )
1218 1222

  
1219 1223
    Asset.objects.all().delete()
1220 1224
    cell = TextCell.objects.get(pk=cell.pk)
1221 1225
    assert cell.asset_css_classes == ''
1222 1226
    resp = app.get('/', status=200)
1223
    assert 'class="cell text-cell textcell   foo"' in resp.text
1227
    assert 'class="cell text-cell textcell foo"' in re.sub(r' +', ' ', resp.text)
1224
-