From 1959757e6845bb7efdc89cd080aae6c48e728e12 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 24 Jun 2021 16:37:26 +0200
Subject: [PATCH 3/4] custom view: possibility to set a digest (#45633)

---
 tests/backoffice_pages/test_custom_view.py | 54 ++++++++++++++++++++++
 wcs/backoffice/management.py               | 30 ++++++++++++
 2 files changed, 84 insertions(+)

diff --git a/tests/backoffice_pages/test_custom_view.py b/tests/backoffice_pages/test_custom_view.py
index ac088b28..875fb46a 100644
--- a/tests/backoffice_pages/test_custom_view.py
+++ b/tests/backoffice_pages/test_custom_view.py
@@ -782,6 +782,60 @@ def test_carddata_custom_view_is_default(pub):
     assert pub.custom_view_class.get(1).is_default is False  # not for datasource
 
 
+def test_carddata_custom_view_digest_template(pub):
+    user = create_superuser(pub)
+    pub.role_class.wipe()
+    role = pub.role_class(name='foobar')
+    role.store()
+    user.roles = [role.id]
+    user.store()
+
+    CardDef.wipe()
+    pub.custom_view_class.wipe()
+    carddef = CardDef()
+    carddef.name = 'foo'
+    carddef.fields = [
+        fields.StringField(id='1', label='Test', type='string', varname='foo'),
+    ]
+    carddef.backoffice_submission_roles = user.roles
+    carddef.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/data/foo/')
+    resp = resp.forms['listing-settings'].submit()
+    assert resp.forms['save-custom-view']['digest_template'].value == ''
+    resp.forms['save-custom-view']['title'] = 'some view'
+    resp.forms['save-custom-view']['visibility'] = 'any'
+    resp.forms['save-custom-view']['digest_template'] = 'FOO {{ form_var_foo }} bar'
+    resp = resp.forms['save-custom-view'].submit()
+    assert CardDef.get(1).digest_templates == {'custom-view:some-view': 'FOO {{ form_var_foo }} bar'}
+    assert resp.location.endswith('/some-view/')
+
+    resp = resp.follow()
+    resp = resp.forms['listing-settings'].submit()
+    assert resp.forms['save-custom-view']['digest_template'].value == 'FOO {{ form_var_foo }} bar'
+    assert resp.forms['save-custom-view']['update'].checked is True
+    resp.forms['save-custom-view']['digest_template'] = 'FOO {{ form_var_foo }}'
+    resp = resp.forms['save-custom-view'].submit()
+    assert CardDef.get(1).digest_templates == {'custom-view:some-view': 'FOO {{ form_var_foo }}'}
+
+    carddef = CardDef.get(1)
+    carddef.digest_templates['default'] = 'plop'
+    carddef.store()
+    resp = app.get('/backoffice/data/foo/')
+    resp = resp.forms['listing-settings'].submit()
+    assert resp.forms['save-custom-view']['digest_template'].value == 'plop'
+    resp.forms['save-custom-view']['title'] = 'another view'
+    resp.forms['save-custom-view']['visibility'] = 'any'
+    resp.forms['save-custom-view']['digest_template'] = '{{ form_var_foo }}'
+    resp = resp.forms['save-custom-view'].submit()
+    assert CardDef.get(1).digest_templates == {
+        'default': 'plop',
+        'custom-view:some-view': 'FOO {{ form_var_foo }}',
+        'custom-view:another-view': '{{ form_var_foo }}',
+    }
+
+
 def test_backoffice_custom_view_keep_filters(pub):
     user = create_superuser(pub)
 
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index c38ae458..56ed923d 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -31,6 +31,7 @@ from quixote.directory import Directory
 from quixote.html import TemplateIO, htmlescape, htmltext
 from quixote.http_request import parse_query
 
+from wcs.admin.forms import UpdateDigestAfterJob
 from wcs.admin.settings import UserFieldsFormDef
 from wcs.api_utils import get_user_from_api_query_string
 from wcs.carddef import CardDef
@@ -1681,6 +1682,22 @@ class FormPage(Directory):
                     'data-dynamic-display-value-in': 'owner|any',
                 },
             )
+            if isinstance(self.formdef, CardDef):
+                digest_template = None
+                if self.view:
+                    templates = self.formdef.digest_templates or {}
+                    digest_template = templates.get('custom-view:%s' % self.view.get_url_slug())
+                form.add(
+                    StringWidget,
+                    'digest_template',
+                    title=_('Digest'),
+                    value=digest_template or self.formdef.default_digest_template,
+                    size=50,
+                    attrs={
+                        'data-dynamic-display-child-of': 'visibility',
+                        'data-dynamic-display-value-in': 'datasource|any',
+                    },
+                )
         else:
             form.add(
                 CheckboxWidget,
@@ -1721,6 +1738,19 @@ class FormPage(Directory):
                 custom_view.is_default = False
         custom_view.store()
 
+        if form.get_widget('digest_template'):
+            if not self.formdef.digest_templates:
+                self.formdef.digest_templates = {}
+            old_value = self.formdef.digest_templates.get('custom-view:%s' % custom_view.get_url_slug())
+            new_value = form.get_widget('digest_template').parse()
+            if old_value != new_value:
+                self.formdef.digest_templates[
+                    'custom-view:%s' % custom_view.get_url_slug()
+                ] = form.get_widget('digest_template').parse()
+                self.formdef.store()
+                if self.formdef.data_class().count():
+                    get_response().add_after_job(UpdateDigestAfterJob(formdef=self.formdef))
+
         if custom_view.is_default and custom_view.visibility != 'datasource':
             # need to clean other views to have only one default per owner/any visibility
             for view in self.get_custom_views():
-- 
2.30.2

