api: only return formdef count if it is requested (#21166)

This commit is contained in:
Frédéric Péters 2018-01-13 22:24:23 +01:00
parent e66a4d0a8e
commit b6e5b36890
3 changed files with 17 additions and 6 deletions

View File

@ -75,6 +75,11 @@ saisie backoffice est disponible, sous le même format, via l'URL
<code>/api/formdefs/?backoffice-submission=on</code>.
</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>
</section>

View File

@ -318,7 +318,6 @@ def test_formdef_list(pub):
assert resp1.json == resp2.json == resp3.json
assert resp1.json[0]['title'] == 'test'
assert resp1.json[0]['url'] == 'http://example.net/test/'
assert resp1.json[0]['count'] == 0
assert resp1.json[0]['redirection'] == False
assert resp1.json[0]['description'] == 'plop'
assert resp1.json[0]['keywords'] == ['mobile', 'test']
@ -401,7 +400,6 @@ def test_formdef_list_redirection(pub):
resp1 = get_app(pub).get('/json')
assert resp1.json[0]['title'] == 'test'
assert resp1.json[0]['url'] == 'http://example.net/test/'
assert resp1.json[0]['count'] == 0
assert resp1.json[0]['redirection'] == True
def test_backoffice_submission_formdef_list(pub, local_user):
@ -917,10 +915,15 @@ def test_categories_formdefs(pub, local_user):
assert len(resp.json) == 2
assert resp.json[0]['title'] == 'test'
assert resp.json[0]['url'] == 'http://example.net/test/'
assert resp.json[0]['count'] == 0
assert resp.json[0]['redirection'] == False
assert resp.json[0]['category'] == 'Category'
assert resp.json[0]['category_slug'] == 'category'
assert not 'count' in resp.json[0]
resp = get_app(pub).get('/api/categories/category/formdefs/?include-count=on')
assert resp.json[0]['title'] == 'test'
assert resp.json[0]['url'] == 'http://example.net/test/'
assert resp.json[0]['count'] == 0
get_app(pub).get('/api/categories/XXX/formdefs/', status=404)

View File

@ -362,6 +362,8 @@ class ApiFormdefsDirectory(Directory):
charset = get_publisher().site_charset
include_count = get_request().form.get('include-count') == 'on'
for formdef in formdefs:
authentication_required = False
if formdef.roles and not list_all_forms and not backoffice_submission:
@ -398,9 +400,10 @@ class ApiFormdefsDirectory(Directory):
formdict['redirection'] = bool(formdef.is_disabled() and
formdef.disabled_redirection)
# we include the count of submitted forms so it's possible to sort
# them by popularity
formdict['count'] = formdef.data_class().count()
if include_count:
# we include the count of submitted forms so it's possible to sort
# them by "popularity"
formdict['count'] = formdef.data_class().count()
formdict['functions'] = {}
formdef_workflow_roles = formdef.workflow_roles or {}