Projet

Général

Profil

0001-api-add-list-of-formdef-keywords-to-api-categories-e.patch

Frédéric Péters, 12 novembre 2015 20:45

Télécharger (2,89 ko)

Voir les différences:

Subject: [PATCH] api: add list of formdef keywords to /api/categories endpoint
 (#8970)

 tests/test_api.py | 11 ++++++++++-
 wcs/api.py        |  8 ++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
tests/test_api.py
372 372
    formdef.name = 'test'
373 373
    formdef.category_id = category.id
374 374
    formdef.fields = []
375
    formdef.keywords = 'mobile, test'
376
    formdef.store()
377

  
378
    formdef = FormDef()
379
    formdef.name = 'test 2'
380
    formdef.category_id = category.id
381
    formdef.fields = []
382
    formdef.keywords = 'foobar'
375 383
    formdef.store()
376 384

  
377 385
    resp = get_app(pub).get('/api/categories/')
......
380 388
    assert resp.json['data'][0]['title'] == 'category'
381 389
    assert resp.json['data'][0]['url'] == 'http://example.net/category/'
382 390
    assert resp.json['data'][0]['description'] == 'hello world'
391
    assert set(resp.json['data'][0]['keywords']) == set(['foobar', 'mobile', 'test'])
383 392

  
384 393
def test_categories_formdefs(pub):
385 394
    test_categories(pub)
......
393 402
    resp = get_app(pub).get('/api/categories/category/formdefs/')
394 403
    resp2 = get_app(pub).get('/category/json')
395 404
    assert resp.json == resp2.json
396
    assert len(resp.json) == 1
405
    assert len(resp.json) == 2
397 406
    assert resp.json[0]['title'] == 'test'
398 407
    assert resp.json[0]['url'] == 'http://example.net/test/'
399 408
    assert resp.json[0]['count'] == 0
wcs/api.py
368 368
        list_categories = []
369 369
        charset = get_publisher().site_charset
370 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)]
371 373
        Category.sort_by_position(categories)
372 374
        for category in categories:
373 375
            d = {}
......
376 378
            d['url'] = category.get_url()
377 379
            if category.description:
378 380
                d['description'] = unicode(category.description, charset)
381
            keywords = {}
382
            for formdef in formdefs:
383
                if str(x.category_id) == str(category.id):
384
                    for keyword in formdef.keywords_list:
385
                        keywords[keyword] = True
386
            d['keywords'] = keywords.keys()
379 387
            list_categories.append(d)
380 388
        get_response().set_content_type('application/json')
381 389
        return json.dumps({'data': list_categories})
382
-