From 3d7d090c91fd2de94f38eae519103b774f6d2741 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sun, 15 Nov 2015 18:06:19 +0100 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(-) diff --git a/help/fr/api-schema.page b/help/fr/api-schema.page index 121ac6f..81a4e1b 100644 --- a/help/fr/api-schema.page +++ b/help/fr/api-schema.page @@ -97,6 +97,12 @@ La liste des catégories est disponible à l'URL /api/categories/.

+Il est possible de passer un paramètre full=on dans l'adresse pour +obtenir pour chacune des catégories la liste des formulaires qu'elle contient, +dans une clé supplémentaire, forms. +

+ +

Les formulaires d'une catégorie précise sont disponibles à l'URL /api/categories/slug/formdefs/.

diff --git a/tests/test_api.py b/tests/test_api.py index 4934309..5b5c0bd 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -389,6 +389,7 @@ def test_categories(pub): 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']) + assert not 'forms' in resp.json['data'][0] def test_categories_formdefs(pub): test_categories(pub) @@ -408,6 +409,12 @@ def test_categories_formdefs(pub): assert resp.json[0]['count'] == 0 assert resp.json[0]['redirection'] == False +def test_categories_full(pub): + test_categories(pub) + resp = get_app(pub).get('/api/categories/?full=on') + assert len(resp.json['data'][0]['forms']) == 2 + assert resp.json['data'][0]['forms'][0]['title'] == 'test' + assert resp.json['data'][0]['forms'][1]['title'] == 'test 2' def test_formdata(pub, local_user): NamedDataSource.wipe() diff --git a/wcs/api.py b/wcs/api.py index ceb78bc..7092b9c 100644 --- a/wcs/api.py +++ b/wcs/api.py @@ -258,19 +258,8 @@ class ApiFormdefsDirectory(Directory): def __init__(self, category=None): self.category = category - def _q_index(self): - try: - user = get_user_from_api_query_string() or get_request().user - except UnknownNameIdAccessForbiddenError: - # if authenticating the user via the query string failed, return - # results for the anonymous case; user is set to 'False' as a - # signed URL with a None user is considered like an appropriate - # webservice call. - user = False - list_all_forms = (user and user.is_admin) or (is_url_signed() and user is None) - + def get_list_forms(self, user, list_all_forms=False): list_forms = [] - if self.category: formdefs = FormDef.select(lambda x: ( str(x.category_id) == str(self.category.id) and ( @@ -333,6 +322,21 @@ class ApiFormdefsDirectory(Directory): list_forms.append(formdict) + return list_forms + + def _q_index(self): + try: + user = get_user_from_api_query_string() or get_request().user + except UnknownNameIdAccessForbiddenError: + # if authenticating the user via the query string failed, return + # results for the anonymous case; user is set to 'False' as a + # signed URL with a None user is considered like an appropriate + # webservice call. + user = False + list_all_forms = (user and user.is_admin) or (is_url_signed() and user is None) + + list_forms = self.get_list_forms(user, list_all_forms) + list_forms.sort(lambda x, y: cmp(x['category_position'], y['category_position'])) for formdict in list_forms: del formdict['category_position'] @@ -352,7 +356,7 @@ class ApiCategoryDirectory(Directory): self.formdefs = ApiFormdefsDirectory(category) -class ApiCategoriesDirectory(RootDirectory): +class ApiCategoriesDirectory(Directory): _q_exports = [''] def __init__(self): @@ -367,9 +371,7 @@ class ApiCategoriesDirectory(RootDirectory): user = None 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)] + categories = Category.select() Category.sort_by_position(categories) for category in categories: d = {} @@ -378,12 +380,17 @@ class ApiCategoriesDirectory(RootDirectory): d['url'] = category.get_url() if category.description: d['description'] = unicode(category.description, charset) + formdefs = ApiFormdefsDirectory(category).get_list_forms(user) + if not formdefs: + # don't advertise empty categories + continue keywords = {} for formdef in formdefs: - if str(formdef.category_id) == str(category.id): - for keyword in formdef.keywords_list: - keywords[keyword] = True + for keyword in formdef['keywords']: + keywords[keyword] = True d['keywords'] = keywords.keys() + if get_request().form.get('full') == 'on': + d['forms'] = formdefs list_categories.append(d) get_response().set_content_type('application/json') return json.dumps({'data': list_categories}) -- 2.6.2