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

This commit is contained in:
Frédéric Péters 2015-11-15 18:06:19 +01:00
parent 4df04e0fa4
commit 80e71f1ff4
3 changed files with 39 additions and 19 deletions

View File

@ -96,6 +96,12 @@ La liste des catégories est disponible à l'URL <code>/api/categories/</code>.
</output>
</screen>
<p>
Il est possible de passer un paramètre <code>full=on</code> dans l'adresse pour
obtenir pour chacune des catégories la liste des formulaires qu'elle contient,
dans une clé supplémentaire, <code>forms</code>.
</p>
<p>
Les formulaires d'une catégorie précise sont disponibles à l'URL
<code>/api/categories/<var>slug</var>/formdefs/</code>.

View File

@ -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()

View File

@ -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})