From 6dc017c9dd790c3177a3aa58066b1348cb0e1886 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Mon, 16 Nov 2020 15:21:23 +0100
Subject: [PATCH 2/2] cards: add user support (#48392)

---
 tests/admin_pages/test_card.py          | 21 ++++++++++++++++++
 tests/backoffice_pages/test_carddata.py | 29 +++++++++++++++++++++++++
 wcs/admin/forms.py                      | 13 +++++++++--
 wcs/backoffice/cards.py                 |  5 +++++
 wcs/backoffice/data_management.py       |  5 +++++
 wcs/formdef.py                          |  3 ++-
 6 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/tests/admin_pages/test_card.py b/tests/admin_pages/test_card.py
index 94207933..82bc381d 100644
--- a/tests/admin_pages/test_card.py
+++ b/tests/admin_pages/test_card.py
@@ -278,6 +278,27 @@ def test_card_category(pub, studio):
     assert '<span class="label">Category</span> <span class="value">Bar</span>' in resp.text
 
 
+def test_card_user_support(pub, studio):
+    create_superuser(pub)
+
+    CardDef.wipe()
+    carddef = CardDef()
+    carddef.name = 'card title'
+    carddef.fields = []
+    carddef.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/cards/1/')
+    assert '<span class="label">User support</span> <span class="value">No</span>' in resp.text
+    resp = resp.click(href='user_support')
+    resp.forms[0]['user_support'] = 'optional'
+    resp.forms[0].submit('submit')
+    assert CardDef.get(carddef.id).user_support == 'optional'
+
+    resp = app.get('/backoffice/cards/1/')
+    assert '<span class="label">User support</span> <span class="value">Optional</span>' in resp.text
+
+
 def test_card_custom_view_data_source(pub, studio):
     user = create_superuser(pub)
     Role.wipe()
diff --git a/tests/backoffice_pages/test_carddata.py b/tests/backoffice_pages/test_carddata.py
index 4ddd591e..0548227f 100644
--- a/tests/backoffice_pages/test_carddata.py
+++ b/tests/backoffice_pages/test_carddata.py
@@ -169,6 +169,35 @@ def test_carddata_management_categories(pub, studio):
     assert '<h3>Bar</h3>' in resp.text
 
 
+def test_carddata_management_user_support(pub, studio):
+    user = create_user(pub)
+
+    CardDef.wipe()
+    carddef = CardDef()
+    carddef.name = 'foo'
+    carddef.fields = []
+    carddef.backoffice_submission_roles = user.roles
+    carddef.workflow_roles = {'_editor': user.roles[0]}
+    carddef.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/data/foo/add/')
+    assert 'Associated User' not in resp.text
+    assert 'user_id' not in resp.form.fields
+
+    carddef.user_support = 'foobar'
+    carddef.store()
+    resp = app.get('/backoffice/data/foo/add/')
+    assert 'Associated User' not in resp.text
+    assert 'user_id' not in resp.form.fields
+
+    carddef.user_support = 'optional'
+    carddef.store()
+    resp = app.get('/backoffice/data/foo/add/')
+    assert 'Associated User' in resp.text
+    assert 'user_id' in resp.form.fields
+
+
 def test_studio_card_item_link(pub, studio):
     user = create_user(pub)
     CardDef.wipe()
diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index c1a6a1c3..aa6a10e0 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -171,7 +171,7 @@ class OptionsDirectory(Directory):
     _q_exports = ['confirmation', 'only_allow_one',
             'always_advertise', 'tracking_code', 'online_status', 'captcha',
             'description', 'keywords', 'category', 'management',
-            'geolocations', 'appearance', 'templates']
+            'geolocations', 'appearance', 'templates', 'user_support']
 
     def __init__(self, formdef):
         self.formdef = formdef
@@ -310,6 +310,15 @@ class OptionsDirectory(Directory):
                         _('Existing forms will be updated in the background.'))
         return result
 
+    def user_support(self):
+        form = Form(enctype='multipart/form-data')
+        form.add(
+            SingleSelectWidget, 'user_support', title=_('User support'),
+            value=self.formdef.user_support,
+            options=[(None, C_('user_support|No'), ''), ('optional', C_('user_support|Optional'), 'optional')]
+        )
+        return self.handle(form, _('User support'))
+
     def handle(self, form, title):
         if not self.formdef.is_readonly():
             form.add_submit('submit', _('Submit'))
@@ -325,7 +334,7 @@ class OptionsDirectory(Directory):
                     'description', 'keywords', 'category_id',
                     'skip_from_360_view', 'geoloc_label', 'appearance_keywords',
                     'include_download_all_button',
-                    'digest_template', 'drafts_lifespan']
+                    'digest_template', 'drafts_lifespan', 'user_support']
             for attr in attrs:
                 widget = form.get_widget(attr)
                 if widget:
diff --git a/wcs/backoffice/cards.py b/wcs/backoffice/cards.py
index 75d02460..84a0fa3d 100644
--- a/wcs/backoffice/cards.py
+++ b/wcs/backoffice/cards.py
@@ -171,6 +171,11 @@ class CardDefPage(FormDefPage):
             digest_template_status = C_('template|None')
         r += add_option_line('options/templates',
                 _('Digest Template'), digest_template_status)
+        if self.formdef.user_support == 'optional':
+            user_support_status = C_('user_support|Optional')
+        else:
+            user_support_status = C_('user_support|No')
+        r += add_option_line('options/user_support', _('User support'), user_support_status)
 
         r += htmltext('</ul>')
         r += htmltext('</div>')
diff --git a/wcs/backoffice/data_management.py b/wcs/backoffice/data_management.py
index 031f09ba..e3e562a2 100644
--- a/wcs/backoffice/data_management.py
+++ b/wcs/backoffice/data_management.py
@@ -316,6 +316,11 @@ class CardFillPage(FormFillPage):
     has_channel_support = False
     has_user_support = False
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        if self.formdef.user_support == 'optional':
+            self.has_user_support = True
+
     def submitted(self, form, *args):
         super(CardFillPage, self).submitted(form, *args)
         if get_response().get_header('location').endswith('/backoffice/submission/'):
diff --git a/wcs/formdef.py b/wcs/formdef.py
index 50fc8843..5955cc8e 100644
--- a/wcs/formdef.py
+++ b/wcs/formdef.py
@@ -118,6 +118,7 @@ class FormDef(StorableObject):
     appearance_keywords = None
     digest_template = None
     drafts_lifespan = None
+    user_support = None
 
     geolocations = None
 
@@ -136,7 +137,7 @@ class FormDef(StorableObject):
     TEXT_ATTRIBUTES = ['name', 'url_name', 'description', 'keywords',
             'publication_date', 'expiration_date', 'internal_identifier',
             'disabled_redirection', 'appearance_keywords',
-            'digest_template', 'drafts_lifespan']
+            'digest_template', 'drafts_lifespan', 'user_support']
     BOOLEAN_ATTRIBUTES = ['discussion', 'detailed_emails', 'disabled',
             'only_allow_one', 'enable_tracking_codes', 'confirmation',
             'always_advertise', 'include_download_all_button',
-- 
2.28.0

