assets: compatibility for wcs assets (#40223)

This commit is contained in:
Lauréline Guérin 2020-03-10 14:12:49 +01:00
parent 5d6d0a86fc
commit 624120dedf
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
4 changed files with 88 additions and 65 deletions

View File

@ -144,20 +144,23 @@ class WcsFormCell(CellBase):
'text': text,
}]
def get_asset_slots(self):
slots = {}
for slot_template_key, slot_template_data in settings.WCS_FORM_ASSET_SLOTS.items():
suffix = ''
if slot_template_data.get('suffix'):
suffix = ' (%s)' % slot_template_data['suffix']
slot_key = 'wcs:form:%s:%s' % (slot_template_key, self.formdef_reference)
slots[slot_key] = {
'label': u'%(prefix)s%(label)s%(suffix)s' % {
'prefix': slot_template_data['prefix'],
'label': self.cached_title,
'suffix': suffix}}
slots[slot_key].update(slot_template_data)
return slots
def get_slug_for_asset(self):
return self.formdef_reference
def get_label_for_asset(self):
return str(self)
def get_asset_slot_key(self, key):
# for legacy
return 'wcs:form:%s:%s' % (
key,
self.get_slug_for_asset())
def get_asset_slot_templates(self):
# for legacy
if settings.WCS_FORM_ASSET_SLOTS:
return settings.WCS_FORM_ASSET_SLOTS
return super().get_asset_slot_templates()
class WcsCommonCategoryCell(CellBase):
@ -219,20 +222,23 @@ class WcsCommonCategoryCell(CellBase):
return
return self.cached_title
def get_asset_slots(self):
slots = {}
for slot_template_key, slot_template_data in settings.WCS_CATEGORY_ASSET_SLOTS.items():
suffix = ''
if slot_template_data.get('suffix'):
suffix = ' (%s)' % slot_template_data['suffix']
slot_key = 'wcs:category:%s:%s' % (slot_template_key, self.category_reference)
slots[slot_key] = {
'label': u'%(prefix)s%(label)s%(suffix)s' % {
'prefix': slot_template_data['prefix'],
'label': self.cached_title,
'suffix': suffix}}
slots[slot_key].update(slot_template_data)
return slots
def get_slug_for_asset(self):
return self.category_reference
def get_label_for_asset(self):
return str(self)
def get_asset_slot_key(self, key):
# for legacy
return 'wcs:category:%s:%s' % (
key,
self.get_slug_for_asset())
def get_asset_slot_templates(self):
# for legacy
if settings.WCS_CATEGORY_ASSET_SLOTS:
return settings.WCS_CATEGORY_ASSET_SLOTS
return super().get_asset_slot_templates()
@register_cell_class

View File

@ -3,12 +3,7 @@
{% block cell-header %}
<h2>{{ title }}</h2>
{% get_asset "wcs:category:picture:"|add:cell.category_reference as asset %}
{% if asset %}
<picture>
<img src="{% asset_url asset size="660x360" crop="center" upscale=False %}" alt="">
</picture>
{% endif %}
{% include "combo/asset_picture_fragment.html" %}
{% if description %}
<div class="intro">
{{ description|safe }}

View File

@ -316,12 +316,10 @@ COMBO_ASSET_SLOTS = {}
# dynamic slots created for wcs category/form cells
# example: {'picture': {'prefix': 'Picture'}}
WCS_CATEGORY_ASSET_SLOTS = {
'picture': {
'prefix': _('Picture'),
},
}
# XXX deprecated
WCS_CATEGORY_ASSET_SLOTS = {}
# XXX deprecated
WCS_FORM_ASSET_SLOTS = {}
COMBO_CELL_ASSET_SLOTS = {
@ -335,6 +333,11 @@ COMBO_CELL_ASSET_SLOTS = {
'prefix': _('Picture'),
},
},
'wcs_wcsformsofcategorycell': {
'picture': {
'prefix': _('Picture'),
},
}
}
# known services

View File

@ -1009,37 +1009,56 @@ def test_tracking_code_cell(app, nocache):
resp = app.post(reverse('wcs-tracking-code'), params={'cell': cell.id}, status=400)
@wcs_present
def test_cell_assets(app, admin_user):
page = Page(title='xxx', slug='test_cell_assets', template_name='standard')
page.save()
cell = WcsFormCell(page=page, placeholder='content', order=0)
cell.formdef_reference = u'default:form-title'
cell.save()
cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
cell.category_reference = 'default:test-9'
cell.ordering = 'alpha'
cell.save()
@wcs_present
def test_cell_assets(settings, app, admin_user):
page = Page.objects.create(title='xxx', slug='test_cell_assets', template_name='standard')
cell1 = WcsFormCell.objects.create(page=page, placeholder='content', order=0, formdef_reference=u'default:form-title')
cell2 = WcsFormsOfCategoryCell.objects.create(
page=page, placeholder='content', order=0,
category_reference='default:test-9',
ordering='alpha')
app = login(app)
with override_settings(WCS_CATEGORY_ASSET_SLOTS={}):
resp = app.get('/manage/assets/')
assert 'have any asset yet.' in resp.text
settings.WCS_CATEGORY_ASSET_SLOTS = {}
settings.WCS_FORM_ASSET_SLOTS = {}
settings.COMBO_CELL_ASSET_SLOTS = {}
resp = app.get('/manage/assets/')
assert 'have any asset yet.' in resp.text
with override_settings(
WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo'}},
WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture'}}):
resp = app.get('/manage/assets/')
assert u'>Logo — Test 9<' in resp.text
assert u'>Picture — form title<' in resp.text
# Old settings have priority
settings.WCS_CATEGORY_ASSET_SLOTS = {'logo': {'prefix': 'Logo'}}
settings.WCS_FORM_ASSET_SLOTS = {'picture': {'prefix': 'Picture'}}
settings.COMBO_CELL_ASSET_SLOTS = {
'wcs_wcsformcell': {'picture': {'prefix': 'Picture blabla', 'suffix': 'test'}},
'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo blabla', 'suffix': 'test'}},
}
resp = app.get('/manage/assets/')
assert u'>Logo — %s<' % cell2.get_label_for_asset() in resp.text
assert u'>Logo blabla — %s<' % cell2.get_label_for_asset() not in resp.text
assert u'>Picture — %s<' % cell1.get_label_for_asset() in resp.text
assert u'>Picture blabla — %s<' % cell1.get_label_for_asset() not in resp.text
# New settings
settings.WCS_CATEGORY_ASSET_SLOTS = {}
settings.WCS_FORM_ASSET_SLOTS = {}
settings.COMBO_CELL_ASSET_SLOTS = {
'wcs_wcsformcell': {'picture': {'prefix': 'Picture'}},
'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo'}},
}
resp = app.get('/manage/assets/')
assert u'>Logo — %s<' % cell2.get_label_for_asset() in resp.text
assert u'>Picture — %s<' % cell1.get_label_for_asset() in resp.text
# test suffix
settings.COMBO_CELL_ASSET_SLOTS = {
'wcs_wcsformcell': {'picture': {'prefix': 'Picture', 'suffix': 'test'}},
'wcs_wcsformsofcategorycell': {'logo': {'prefix': 'Logo', 'suffix': 'test'}},
}
resp = app.get('/manage/assets/')
assert u'>Logo — %s (test)<' % cell2.get_label_for_asset() in resp.text
assert u'>Picture — %s (test)<' % cell1.get_label_for_asset() in resp.text
with override_settings(
WCS_CATEGORY_ASSET_SLOTS={'logo': {'prefix': 'Logo', 'suffix': 'test'}},
WCS_FORM_ASSET_SLOTS={'picture': {'prefix': 'Picture', 'suffix': 'test'}}):
resp = app.get('/manage/assets/')
assert u'>Logo — Test 9 (test)<' in resp.text
assert u'>Picture — form title (test)<' in resp.text
@wcs_present
def test_tracking_code_search(app, nocache):