From 3259c694c4cae363d3f7ab0fe90b755fc600f082 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 10 Mar 2020 14:12:49 +0100
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(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index ce7a805..3e30a05 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -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
diff --git a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
index c58f86c..3dec716 100644
--- a/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
+++ b/combo/apps/wcs/templates/combo/wcs/forms_of_category.html
@@ -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 }}
diff --git a/combo/settings.py b/combo/settings.py
index e998799..f91700d 100644
--- a/combo/settings.py
+++ b/combo/settings.py
@@ -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 = {
@@ -350,6 +348,11 @@ COMBO_CELL_ASSET_SLOTS = {
             'prefix': _('Picture'),
         },
     },
+    'wcs_wcsformsofcategorycell': {
+        'picture': {
+            'prefix': _('Picture'),
+        },
+    }
 }
 
 # known services
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 3c0ae74..529e3cf 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1007,37 +1007,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()
+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')
 
-    cell = WcsFormsOfCategoryCell(page=page, placeholder='content', order=0)
-    cell.category_reference = 'default:test-9'
-    cell.ordering = 'alpha'
-    cell.save()
+    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
-
-    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
-
-    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
+    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
+
+    # 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
+
 
 @wcs_present
 def test_tracking_code_search(app, nocache):
-- 
2.20.1

