Projet

Général

Profil

0001-api-only-return-formdef-count-if-popularity-is-reque.patch

Frédéric Péters, 14 janvier 2018 09:30

Télécharger (3,73 ko)

Voir les différences:

Subject: [PATCH] api: only return formdef count if popularity is requested
 (#21166)

 help/fr/api-schema.page | 5 +++++
 tests/test_api.py       | 9 ++++++---
 wcs/api.py              | 9 ++++++---
 3 files changed, 17 insertions(+), 6 deletions(-)
help/fr/api-schema.page
75 75
<code>/api/formdefs/?backoffice-submission=on</code>.
76 76
</p>
77 77

  
78
<p>
79
Il est également possible d'obtenir un nombre permettant de trier les résultats
80
par popularité en ajoutant un paramètre <code>include-popularity=on</code>. Les
81
différentes entrées disposeront alors d'une clé <code>count</code>.
82
</p>
78 83

  
79 84
</section>
80 85

  
tests/test_api.py
318 318
    assert resp1.json == resp2.json == resp3.json
319 319
    assert resp1.json[0]['title'] == 'test'
320 320
    assert resp1.json[0]['url'] == 'http://example.net/test/'
321
    assert resp1.json[0]['count'] == 0
322 321
    assert resp1.json[0]['redirection'] == False
323 322
    assert resp1.json[0]['description'] == 'plop'
324 323
    assert resp1.json[0]['keywords'] == ['mobile', 'test']
......
401 400
    resp1 = get_app(pub).get('/json')
402 401
    assert resp1.json[0]['title'] == 'test'
403 402
    assert resp1.json[0]['url'] == 'http://example.net/test/'
404
    assert resp1.json[0]['count'] == 0
405 403
    assert resp1.json[0]['redirection'] == True
406 404

  
407 405
def test_backoffice_submission_formdef_list(pub, local_user):
......
917 915
    assert len(resp.json) == 2
918 916
    assert resp.json[0]['title'] == 'test'
919 917
    assert resp.json[0]['url'] == 'http://example.net/test/'
920
    assert resp.json[0]['count'] == 0
921 918
    assert resp.json[0]['redirection'] == False
922 919
    assert resp.json[0]['category'] == 'Category'
923 920
    assert resp.json[0]['category_slug'] == 'category'
921
    assert not 'count' in resp.json[0]
922

  
923
    resp = get_app(pub).get('/api/categories/category/formdefs/?include-popularity=on')
924
    assert resp.json[0]['title'] == 'test'
925
    assert resp.json[0]['url'] == 'http://example.net/test/'
926
    assert resp.json[0]['count'] == 0
924 927

  
925 928
    get_app(pub).get('/api/categories/XXX/formdefs/', status=404)
926 929

  
wcs/api.py
362 362

  
363 363
        charset = get_publisher().site_charset
364 364

  
365
        include_popularity = get_request().form.get('include-popularity') == 'on'
366

  
365 367
        for formdef in formdefs:
366 368
            authentication_required = False
367 369
            if formdef.roles and not list_all_forms and not backoffice_submission:
......
398 400
            formdict['redirection'] = bool(formdef.is_disabled() and
399 401
                    formdef.disabled_redirection)
400 402

  
401
            # we include the count of submitted forms so it's possible to sort
402
            # them by popularity
403
            formdict['count'] = formdef.data_class().count()
403
            if include_popularity:
404
                # we include the count of submitted forms so it's possible to sort
405
                # them by popularity
406
                formdict['count'] = formdef.data_class().count()
404 407

  
405 408
            formdict['functions'] = {}
406 409
            formdef_workflow_roles = formdef.workflow_roles or {}
407
-