From 8bb5e63c9c950c70634bee5eca8f6fec79662d8e Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 9 Apr 2020 16:17:29 +0200
Subject: [PATCH] ics: add formdata digest to ics export (#33152)

---
 tests/test_api.py            | 9 +++++++++
 wcs/backoffice/management.py | 7 ++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/tests/test_api.py b/tests/test_api.py
index 0d0407a8..25cd3a71 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -2361,16 +2361,17 @@ def ics_data(local_user):
     formdef = FormDef()
     formdef.url_name = 'test'
     formdef.name = 'testé'
     formdef.workflow_roles = {'_receiver': role.id}
     formdef.fields = [
         fields.StringField(id='0', label='foobar', varname='foobar'),
         fields.StringField(id='1', label='foobar2', varname='foobar2'),
         ]
+    formdef.digest_template = 'plöp {{ form_var_foobar }} plÔp'
     formdef.store()
 
     data_class = formdef.data_class()
     data_class.wipe()
 
     date = datetime.datetime(2014, 1, 20, 12, 00)
     for i in range(30):
         formdata = data_class()
@@ -2424,16 +2425,24 @@ def test_api_ics_formdata(pub, local_user, ics_data):
     # check that description contains form name, display id, workflow status,
     # backoffice url and attached user
     pattern = re.compile(u'DESCRIPTION:testé \| 1-\d+ \| New', re.MULTILINE)
     m = pattern.findall(resp.text)
     assert len(m) == 10
     assert resp.text.count('Jean Darmette') == 10
     assert resp.text.count('DTSTART') == 10
 
+    # check formdata digest summary and description contains the formdata digest
+    pattern = re.compile(
+        r'SUMMARY:testé #1-\d+ - plöp \d{4}-\d{2}-\d{2} \d{2}:\d{2} plÔp',
+        re.MULTILINE)
+    m = pattern.findall(resp.text)
+    assert len(m) == 10
+    assert resp.text.count(r'plöp') == 20
+
     # check with a filter
     resp = get_app(pub).get(sign_uri('/api/forms/test/ics/foobar?filter=done', user=local_user))
     assert resp.text.count('BEGIN:VEVENT') == 20
     pattern = re.compile(u'DESCRIPTION:testé \| 1-\d+ \| Finished', re.MULTILINE)
     m = pattern.findall(resp.text)
     assert len(m) == 20
     assert resp.text.count('Jean Darmette') == 20
 
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 317fd4b1..5bb44def 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -2198,26 +2198,31 @@ class FormPage(Directory):
                     except ValueError:
                         continue
 
                     vevent = vobject.newFromBehavior('vevent')
                     vevent.add('uid').value = '%s-%s-%s' % (
                             get_request().get_server().lower(),
                             formdef.url_name,
                             formdata.id)
-                    vevent.add('summary').value = force_text(formdata.get_display_name(), charset)
+                    summary = force_text(formdata.get_display_name(), charset)
+                    if formdata.digest:
+                        summary += ' - %s' % force_text(formdata.digest, charset)
+                    vevent.add('summary').value = summary
                     vevent.add('dtstart').value = dtstart
                     if dtend:
                             vevent.add('dtend').value = dtend
                     vevent.dtstart.value_param = 'DATE'
                     backoffice_url = formdata.get_url(backoffice=True)
                     vevent.add('url').value = backoffice_url
                     form_name = force_text(formdef.name, charset)
                     status_name = force_text(formdata.get_status_label(), charset)
                     description = '%s | %s | %s\n'  % (form_name, formdata.get_display_id(), status_name)
+                    if formdata.digest:
+                        description += '%s\n' % force_text(formdata.digest, charset)
                     description += backoffice_url
                     # TODO: improve performance by loading all users in one
                     # single query before the loop
                     if formdata.user:
                         description += '\n%s' % force_text(formdata.user.get_display_name(), charset)
                     vevent.add('description').value = description
                     cal.add(vevent)
 
-- 
2.26.2

