From 35316a0820bd20f733afab383b288c3967db109f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 12 Nov 2015 20:44:54 +0100 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(-) diff --git a/tests/test_api.py b/tests/test_api.py index f6046c7..4934309 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -372,6 +372,14 @@ def test_categories(pub): formdef.name = 'test' formdef.category_id = category.id formdef.fields = [] + formdef.keywords = 'mobile, test' + formdef.store() + + formdef = FormDef() + formdef.name = 'test 2' + formdef.category_id = category.id + formdef.fields = [] + formdef.keywords = 'foobar' formdef.store() resp = get_app(pub).get('/api/categories/') @@ -380,6 +388,7 @@ def test_categories(pub): assert resp.json['data'][0]['title'] == 'category' assert resp.json['data'][0]['url'] == 'http://example.net/category/' assert resp.json['data'][0]['description'] == 'hello world' + assert set(resp.json['data'][0]['keywords']) == set(['foobar', 'mobile', 'test']) def test_categories_formdefs(pub): test_categories(pub) @@ -393,7 +402,7 @@ def test_categories_formdefs(pub): resp = get_app(pub).get('/api/categories/category/formdefs/') resp2 = get_app(pub).get('/category/json') assert resp.json == resp2.json - assert len(resp.json) == 1 + 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 diff --git a/wcs/api.py b/wcs/api.py index 197ad70..8136340 100644 --- a/wcs/api.py +++ b/wcs/api.py @@ -368,6 +368,8 @@ class ApiCategoriesDirectory(RootDirectory): list_categories = [] charset = get_publisher().site_charset categories = self.get_categories(user) + formdefs = [x for x in FormDef.select(ignore_errors=True) + if (not x.is_disabled() or x.disabled_redirection)] Category.sort_by_position(categories) for category in categories: d = {} @@ -376,6 +378,12 @@ class ApiCategoriesDirectory(RootDirectory): d['url'] = category.get_url() if category.description: d['description'] = unicode(category.description, charset) + keywords = {} + for formdef in formdefs: + if str(x.category_id) == str(category.id): + for keyword in formdef.keywords_list: + keywords[keyword] = True + d['keywords'] = keywords.keys() list_categories.append(d) get_response().set_content_type('application/json') return json.dumps({'data': list_categories}) -- 2.6.2