Projet

Général

Profil

0002-assets-compatibility-for-wcs-assets-40223.patch

Lauréline Guérin, 12 mars 2020 11:53

Télécharger (8,72 ko)

Voir les différences:

Subject: [PATCH 2/3] assets: compatibility for wcs assets (#40223)

 combo/apps/wcs/models.py                      | 62 +++++++++-------
 .../combo/wcs/forms_of_category.html          |  7 +-
 combo/settings.py                             | 13 ++--
 tests/test_wcs.py                             | 73 ++++++++++++-------
 4 files changed, 89 insertions(+), 66 deletions(-)
combo/apps/wcs/models.py
144 144
            'text': text,
145 145
            }]
146 146

  
147
    def get_asset_slots(self):
148
        slots = {}
149
        for slot_template_key, slot_template_data in settings.WCS_FORM_ASSET_SLOTS.items():
150
            suffix = ''
151
            if slot_template_data.get('suffix'):
152
                suffix = ' (%s)' % slot_template_data['suffix']
153
            slot_key = 'wcs:form:%s:%s' % (slot_template_key, self.formdef_reference)
154
            slots[slot_key] = {
155
                    'label': u'%(prefix)s — %(label)s%(suffix)s' % {
156
                        'prefix': slot_template_data['prefix'],
157
                        'label': self.cached_title,
158
                        'suffix': suffix}}
159
            slots[slot_key].update(slot_template_data)
160
        return slots
147
    def get_slug_for_asset(self):
148
        return self.formdef_reference
149

  
150
    def get_label_for_asset(self):
151
        return str(self)
152

  
153
    def get_asset_slot_key(self, key):
154
        # for legacy
155
        return 'wcs:form:%s:%s' % (
156
            key,
157
            self.get_slug_for_asset())
158

  
159
    def get_asset_slot_templates(self):
160
        # for legacy
161
        if settings.WCS_FORM_ASSET_SLOTS:
162
            return settings.WCS_FORM_ASSET_SLOTS
163
        return super().get_asset_slot_templates()
161 164

  
162 165

  
163 166
class WcsCommonCategoryCell(CellBase):
......
219 222
            return
220 223
        return self.cached_title
221 224

  
222
    def get_asset_slots(self):
223
        slots = {}
224
        for slot_template_key, slot_template_data in settings.WCS_CATEGORY_ASSET_SLOTS.items():
225
            suffix = ''
226
            if slot_template_data.get('suffix'):
227
                suffix = ' (%s)' % slot_template_data['suffix']
228
            slot_key = 'wcs:category:%s:%s' % (slot_template_key, self.category_reference)
229
            slots[slot_key] = {
230
                    'label': u'%(prefix)s — %(label)s%(suffix)s' % {
231
                        'prefix': slot_template_data['prefix'],
232
                        'label': self.cached_title,
233
                        'suffix': suffix}}
234
            slots[slot_key].update(slot_template_data)
235
        return slots
225
    def get_slug_for_asset(self):
226
        return self.category_reference
227

  
228
    def get_label_for_asset(self):
229
        return str(self)
230

  
231
    def get_asset_slot_key(self, key):
232
        # for legacy
233
        return 'wcs:category:%s:%s' % (
234
            key,
235
            self.get_slug_for_asset())
236

  
237
    def get_asset_slot_templates(self):
238
        # for legacy
239
        if settings.WCS_CATEGORY_ASSET_SLOTS:
240
            return settings.WCS_CATEGORY_ASSET_SLOTS
241
        return super().get_asset_slot_templates()
236 242

  
237 243

  
238 244
@register_cell_class
combo/apps/wcs/templates/combo/wcs/forms_of_category.html
3 3

  
4 4
{% block cell-header %}
5 5
<h2>{{ title }}</h2>
6
{% get_asset "wcs:category:picture:"|add:cell.category_reference as asset %}
7
{% if asset %}
8
  <picture>
9
  <img src="{% asset_url asset size="660x360" crop="center" upscale=False %}" alt="">
10
  </picture>
11
{% endif %}
6
{% include "combo/asset_picture_fragment.html" %}
12 7
{% if description %}
13 8
<div class="intro">
14 9
{{ description|safe }}
combo/settings.py
316 316

  
317 317
# dynamic slots created for wcs category/form cells
318 318
# example: {'picture': {'prefix': 'Picture'}}
319
WCS_CATEGORY_ASSET_SLOTS = {
320
    'picture': {
321
        'prefix': _('Picture'),
322
    },
323
}
319
# XXX deprecated
320
WCS_CATEGORY_ASSET_SLOTS = {}
324 321

  
322
# XXX deprecated
325 323
WCS_FORM_ASSET_SLOTS = {}
326 324

  
327 325
COMBO_CELL_ASSET_SLOTS = {
......
350 348
            'prefix': _('Picture'),
351 349
        },
352 350
    },
351
    'wcs_wcsformsofcategorycell': {
352
        'picture': {
353
            'prefix': _('Picture'),
354
        },
355
    }
353 356
}
354 357

  
355 358
# known services
tests/test_wcs.py
1007 1007

  
1008 1008
    resp = app.post(reverse('wcs-tracking-code'), params={'cell': cell.id}, status=400)
1009 1009

  
1010

  
1010 1011
@wcs_present
1011
def test_cell_assets(app, admin_user):
1012
    page = Page(title='xxx', slug='test_cell_assets', template_name='standard')
1013
    page.save()
1014
    cell = WcsFormCell(page=page, placeholder='content', order=0)
1015
    cell.formdef_reference = u'default:form-title'
1016
    cell.save()
1012
def test_cell_assets(settings, app, admin_user):
1013
    page = Page.objects.create(title='xxx', slug='test_cell_assets', template_name='standard')
1014
    cell1 = WcsFormCell.objects.create(page=page, placeholder='content', order=0, formdef_reference=u'default:form-title')
1017 1015

  
1018
    cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
1019
    cell.category_reference = 'default:test-9'
1020
    cell.ordering = 'alpha'
1021
    cell.save()
1016
    cell2 = WcsFormsOfCategoryCell.objects.create(
1017
        page=page, placeholder='content', order=0,
1018
        category_reference='default:test-9',
1019
        ordering='alpha')
1022 1020

  
1023 1021
    app = login(app)
1024
    with override_settings(WCS_CATEGORY_ASSET_SLOTS={}):
1025
        resp = app.get('/manage/assets/')
1026
        assert 'have any asset yet.' in resp.text
1027

  
1028
    with override_settings(
1029
            WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo'}},
1030
            WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture'}}):
1031
        resp = app.get('/manage/assets/')
1032
        assert u'>Logo — Test 9<' in resp.text
1033
        assert u'>Picture — form title<' in resp.text
1034

  
1035
    with override_settings(
1036
            WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo', 'suffix': 'test'}},
1037
            WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture', 'suffix': 'test'}}):
1038
        resp = app.get('/manage/assets/')
1039
        assert u'>Logo — Test 9 (test)<' in resp.text
1040
        assert u'>Picture — form title (test)<' in resp.text
1022
    settings.WCS_CATEGORY_ASSET_SLOTS = {}
1023
    settings.WCS_FORM_ASSET_SLOTS = {}
1024
    settings.COMBO_CELL_ASSET_SLOTS = {}
1025
    resp = app.get('/manage/assets/')
1026
    assert 'have any asset yet.' in resp.text
1027

  
1028
    # Old settings have priority
1029
    settings.WCS_CATEGORY_ASSET_SLOTS = {'logo': {'prefix': 'Logo'}}
1030
    settings.WCS_FORM_ASSET_SLOTS = {'picture': {'prefix': 'Picture'}}
1031
    settings.COMBO_CELL_ASSET_SLOTS = {
1032
        'wcs_wcsformcell': {'picture': {'prefix': 'Picture blabla', 'suffix': 'test'}},
1033
        'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo blabla', 'suffix': 'test'}},
1034
    }
1035
    resp = app.get('/manage/assets/')
1036
    assert u'>Logo — %s<' % cell2.get_label_for_asset() in resp.text
1037
    assert u'>Logo blabla — %s<' % cell2.get_label_for_asset() not in resp.text
1038
    assert u'>Picture — %s<' % cell1.get_label_for_asset() in resp.text
1039
    assert u'>Picture blabla — %s<' % cell1.get_label_for_asset() not in resp.text
1040
    # New settings
1041
    settings.WCS_CATEGORY_ASSET_SLOTS = {}
1042
    settings.WCS_FORM_ASSET_SLOTS = {}
1043
    settings.COMBO_CELL_ASSET_SLOTS = {
1044
        'wcs_wcsformcell': {'picture': {'prefix': 'Picture'}},
1045
        'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo'}},
1046
    }
1047
    resp = app.get('/manage/assets/')
1048
    assert u'>Logo — %s<' % cell2.get_label_for_asset() in resp.text
1049
    assert u'>Picture — %s<' % cell1.get_label_for_asset() in resp.text
1050

  
1051
    # test suffix
1052
    settings.COMBO_CELL_ASSET_SLOTS = {
1053
        'wcs_wcsformcell': {'picture': {'prefix': 'Picture', 'suffix': 'test'}},
1054
        'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo', 'suffix': 'test'}},
1055
    }
1056
    resp = app.get('/manage/assets/')
1057
    assert u'>Logo — %s (test)<' % cell2.get_label_for_asset() in resp.text
1058
    assert u'>Picture — %s (test)<' % cell1.get_label_for_asset() in resp.text
1059

  
1041 1060

  
1042 1061
@wcs_present
1043 1062
def test_tracking_code_search(app, nocache):
1044
-