From 077e56375c11dd4d6c72cee1ced4651de8170b85 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 14 Aug 2020 13:56:08 +0200
Subject: [PATCH] api: add parameter to include disabled formdefs (#43630)

---
 help/fr/api-schema.page |  5 +++++
 tests/test_api.py       | 12 ++++++++++++
 wcs/api.py              | 11 +++++++----
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/help/fr/api-schema.page b/help/fr/api-schema.page
index 110559d0..0dd254d7 100644
--- a/help/fr/api-schema.page
+++ b/help/fr/api-schema.page
@@ -76,16 +76,21 @@ saisie backoffice est disponible, sous le même format, via l'URL
 </p>
 
 <p>
 Il est également possible d'obtenir un nombre permettant de trier les résultats
 par « popularité » en ajoutant un paramètre <code>include-count=on</code>. Les
 différentes entrées disposeront alors d'une clé <code>count</code>.
 </p>
 
+<p>
+La liste retournée inclura les formulaires désactivés en ajoutant le paramètre
+<code>include-disabled=on</code>.
+</p>
+
 </section>
 
 
 <section id="categories">
    <title>Catégories</title>
 
 <p>
 La liste des catégories est disponible à l'URL <code>/api/categories/</code>.
diff --git a/tests/test_api.py b/tests/test_api.py
index 8e1dd3cb..4884d0b0 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1077,16 +1077,24 @@ def test_categories_formdefs(pub, local_user):
 
     formdef2 = FormDef()
     formdef2.name = 'other test'
     formdef2.category_id = None
     formdef2.fields = []
     formdef2.store()
     formdef2.data_class().wipe()
 
+    formdef2 = FormDef()
+    formdef2.name = 'test disabled'
+    formdef2.category_id = category.id
+    formdef2.fields = []
+    formdef2.disabled = True
+    formdef2.store()
+    formdef2.data_class().wipe()
+
     resp = get_app(pub).get('/api/categories/category/formdefs/')
     resp2 = get_app(pub).get('/category/json')
     assert resp.json == resp2.json
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 2
     assert resp.json['data'][0]['title'] == 'test'
     assert resp.json['data'][0]['url'] == 'http://example.net/test/'
     assert resp.json['data'][0]['redirection'] == False
@@ -1094,16 +1102,20 @@ def test_categories_formdefs(pub, local_user):
     assert resp.json['data'][0]['category_slug'] == 'category'
     assert 'count' not in resp.json['data'][0]
 
     resp = get_app(pub).get('/api/categories/category/formdefs/?include-count=on')
     assert resp.json['data'][0]['title'] == 'test'
     assert resp.json['data'][0]['url'] == 'http://example.net/test/'
     assert resp.json['data'][0]['count'] == 0
 
+    resp = get_app(pub).get('/api/categories/category/formdefs/?include-disabled=on')
+    assert len(resp.json['data']) == 3
+    assert resp.json['data'][2]['title'] == 'test disabled'
+
     get_app(pub).get('/api/categories/XXX/formdefs/', status=404)
 
     resp = get_app(pub).get('/api/categories/category/formdefs/?backoffice-submission=on')
     assert resp.json['err'] == 0
     assert len(resp.json['data']) == 0
 
     Role.wipe()
     role = Role(name='test')
diff --git a/wcs/api.py b/wcs/api.py
index eada5df4..2854b4ee 100644
--- a/wcs/api.py
+++ b/wcs/api.py
@@ -456,20 +456,23 @@ class ApiFormdefsDirectory(Directory):
         list_forms = []
 
         if not user and backoffice_submission:
             return list_forms
 
         if formdefs is None:
             formdefs = FormDef.select(order_by='name', ignore_errors=True, lightweight=True)
 
-        if backoffice_submission:
-            formdefs = [x for x in formdefs if not x.is_disabled()]
-        else:
-            formdefs = [x for x in formdefs if not x.is_disabled() or x.disabled_redirection]
+        include_disabled = get_query_flag('include-disabled')
+
+        if not include_disabled:
+            if backoffice_submission:
+                formdefs = [x for x in formdefs if not x.is_disabled()]
+            else:
+                formdefs = [x for x in formdefs if not x.is_disabled() or x.disabled_redirection]
 
         if self.category:
             formdefs = [x for x in formdefs if str(x.category_id) == str(self.category.id)]
 
         charset = get_publisher().site_charset
 
         include_count = get_query_flag('include-count')
 
-- 
2.28.0

