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 |
|
-
|