Projet

Général

Profil

0001-api-add-full-on-support-on-categories-to-include-for.patch

Frédéric Péters, 15 novembre 2015 18:06

Télécharger (5,74 ko)

Voir les différences:

Subject: [PATCH] api: add ?full=on support on categories, to include formdefs
 (#8972)

 help/fr/api-schema.page |  6 ++++++
 tests/test_api.py       |  7 +++++++
 wcs/api.py              | 45 ++++++++++++++++++++++++++-------------------
 3 files changed, 39 insertions(+), 19 deletions(-)
help/fr/api-schema.page
97 97
</screen>
98 98

  
99 99
<p>
100
Il est possible de passer un paramètre <code>full=on</code> dans l'adresse pour
101
obtenir pour chacune des catégories la liste des formulaires qu'elle contient,
102
dans une clé supplémentaire, <code>forms</code>.
103
</p>
104

  
105
<p>
100 106
Les formulaires d'une catégorie précise sont disponibles à l'URL
101 107
<code>/api/categories/<var>slug</var>/formdefs/</code>.
102 108
</p>
tests/test_api.py
389 389
    assert resp.json['data'][0]['url'] == 'http://example.net/category/'
390 390
    assert resp.json['data'][0]['description'] == 'hello world'
391 391
    assert set(resp.json['data'][0]['keywords']) == set(['foobar', 'mobile', 'test'])
392
    assert not 'forms' in resp.json['data'][0]
392 393

  
393 394
def test_categories_formdefs(pub):
394 395
    test_categories(pub)
......
408 409
    assert resp.json[0]['count'] == 0
409 410
    assert resp.json[0]['redirection'] == False
410 411

  
412
def test_categories_full(pub):
413
    test_categories(pub)
414
    resp = get_app(pub).get('/api/categories/?full=on')
415
    assert len(resp.json['data'][0]['forms']) == 2
416
    assert resp.json['data'][0]['forms'][0]['title'] == 'test'
417
    assert resp.json['data'][0]['forms'][1]['title'] == 'test 2'
411 418

  
412 419
def test_formdata(pub, local_user):
413 420
    NamedDataSource.wipe()
wcs/api.py
258 258
    def __init__(self, category=None):
259 259
        self.category = category
260 260

  
261
    def _q_index(self):
262
        try:
263
            user = get_user_from_api_query_string() or get_request().user
264
        except UnknownNameIdAccessForbiddenError:
265
            # if authenticating the user via the query string failed, return
266
            # results for the anonymous case; user is set to 'False' as a
267
            # signed URL with a None user is considered like an appropriate
268
            # webservice call.
269
            user = False
270
        list_all_forms = (user and user.is_admin) or (is_url_signed() and user is None)
271

  
261
    def get_list_forms(self, user, list_all_forms=False):
272 262
        list_forms = []
273

  
274 263
        if self.category:
275 264
            formdefs = FormDef.select(lambda x: (
276 265
                        str(x.category_id) == str(self.category.id) and (
......
333 322

  
334 323
            list_forms.append(formdict)
335 324

  
325
        return list_forms
326

  
327
    def _q_index(self):
328
        try:
329
            user = get_user_from_api_query_string() or get_request().user
330
        except UnknownNameIdAccessForbiddenError:
331
            # if authenticating the user via the query string failed, return
332
            # results for the anonymous case; user is set to 'False' as a
333
            # signed URL with a None user is considered like an appropriate
334
            # webservice call.
335
            user = False
336
        list_all_forms = (user and user.is_admin) or (is_url_signed() and user is None)
337

  
338
        list_forms = self.get_list_forms(user, list_all_forms)
339

  
336 340
        list_forms.sort(lambda x, y: cmp(x['category_position'], y['category_position']))
337 341
        for formdict in list_forms:
338 342
            del formdict['category_position']
......
352 356
        self.formdefs = ApiFormdefsDirectory(category)
353 357

  
354 358

  
355
class ApiCategoriesDirectory(RootDirectory):
359
class ApiCategoriesDirectory(Directory):
356 360
    _q_exports = ['']
357 361

  
358 362
    def __init__(self):
......
367 371
            user = None
368 372
        list_categories = []
369 373
        charset = get_publisher().site_charset
370
        categories = self.get_categories(user)
371
        formdefs = [x for x in FormDef.select(ignore_errors=True)
372
                    if (not x.is_disabled() or x.disabled_redirection)]
374
        categories = Category.select()
373 375
        Category.sort_by_position(categories)
374 376
        for category in categories:
375 377
            d = {}
......
378 380
            d['url'] = category.get_url()
379 381
            if category.description:
380 382
                d['description'] = unicode(category.description, charset)
383
            formdefs = ApiFormdefsDirectory(category).get_list_forms(user)
384
            if not formdefs:
385
                # don't advertise empty categories
386
                continue
381 387
            keywords = {}
382 388
            for formdef in formdefs:
383
                if str(formdef.category_id) == str(category.id):
384
                    for keyword in formdef.keywords_list:
385
                        keywords[keyword] = True
389
                for keyword in formdef['keywords']:
390
                    keywords[keyword] = True
386 391
            d['keywords'] = keywords.keys()
392
            if get_request().form.get('full') == 'on':
393
                d['forms'] = formdefs
387 394
            list_categories.append(d)
388 395
        get_response().set_content_type('application/json')
389 396
        return json.dumps({'data': list_categories})
390
-