From e105b5473437df93b61615f3eedf6120c47963b1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 23 Jul 2021 13:53:31 +0200
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(-)

diff --git a/combo/data/models.py b/combo/data/models.py
index c219564b..374b6e53 100644
--- a/combo/data/models.py
+++ b/combo/data/models.py
@@ -1006,7 +1006,16 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
         )
 
     def get_options_form_class(self):
-        return model_forms.modelform_factory(self.__class__, fields=['slug', 'extra_css_class'])
+        fields = ['slug', 'extra_css_class']
+        widgets = None
+        extra_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str())
+        if extra_templates:
+            fields = ['template_name'] + fields
+            template_names = [('', _('Default Value'))] + [
+                (k, v['label']) for k, v in extra_templates.items()
+            ]
+            widgets = {'template_name': forms.Select(choices=template_names)}
+        return model_forms.modelform_factory(self.__class__, fields=fields, widgets=widgets)
 
     def get_extra_manager_context(self):
         return {}
@@ -1115,6 +1124,11 @@ class CellBase(six.with_metaclass(CellMeta, models.Model)):
     def get_cell_extra_context(self, context):
         return {'cell': self}
 
+    def get_template_label(self):
+        cell_templates = settings.COMBO_CELL_TEMPLATES.get(self.get_cell_type_str()) or {}
+        selected_template_infos = cell_templates.get(self.template_name) or {}
+        return selected_template_infos.get('label')
+
     def render(self, context):
         context.update(self.get_cell_extra_context(context))
         template_names = ['combo/' + self._meta.model_name + '.html']
diff --git a/combo/manager/templates/combo/page_view.html b/combo/manager/templates/combo/page_view.html
index 412b12bd..fdd91129 100644
--- a/combo/manager/templates/combo/page_view.html
+++ b/combo/manager/templates/combo/page_view.html
@@ -145,6 +145,7 @@
         <h3><span class="handle">⣿</span>
                 <span class="group1">
                         {{ cell.get_label }}
+                        {% if cell.template_name %} ({{cell.get_template_label}}){% endif %}
                         {% if cell.slug %} [{{cell.slug}}] {% endif %}
                   {% if cell.extra_css_class %}
                   <span class="extra-css-class">[{{ cell.extra_css_class }}]</span>
diff --git a/combo/settings.py b/combo/settings.py
index b20f36fe..bb86cc43 100644
--- a/combo/settings.py
+++ b/combo/settings.py
@@ -342,6 +342,8 @@ COMBO_CELL_ASSET_SLOTS = {
     },
 }
 
+COMBO_CELL_TEMPLATES = {}
+
 COMBO_MAP_LAYER_ASSET_SLOTS = {}
 
 # known services
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 151939fd..6fd9e5b3 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -1358,6 +1358,30 @@ def test_edit_cell_options(app, admin_user):
     assert '[CSS]' in app.get('/manage/pages/%s/' % page.id)
 
 
+def test_edit_cell_options_template(app, admin_user):
+    Page.objects.all().delete()
+    page = Page(title='One', slug='one', template_name='standard')
+    page.save()
+    cell = TextCell(page=page, placeholder='content', text='Foobar', order=0)
+    cell.save()
+
+    app = login(app)
+    resp = app.get('/manage/pages/%s/' % page.id)
+    resp = resp.click(href='/data_textcell-%s/options' % cell.id)
+    assert 'cdata_textcell-%s-template_name' % cell.id not in resp.form.fields
+    assert resp.form['cdata_textcell-%s-slug' % cell.id].value == ''
+    assert resp.form['cdata_textcell-%s-extra_css_class' % cell.id].value == ''
+
+    with override_settings(COMBO_CELL_TEMPLATES={'data_textcell': {'extra': {'label': 'Extra'}}}):
+        resp = app.get('/manage/pages/%s/' % page.id)
+        resp = resp.click(href='/data_textcell-%s/options' % cell.id)
+        resp.form['cdata_textcell-%s-template_name' % cell.id].value = 'extra'
+
+        resp = resp.form.submit('submit')
+        assert TextCell.objects.get(id=cell.id).template_name == 'extra'
+        assert '(Extra)' in app.get('/manage/pages/%s/' % page.id)
+
+
 def test_edit_cell_order(app, admin_user):
     Page.objects.all().delete()
     page = Page(title='One', slug='one', template_name='standard')
-- 
2.32.0

