api: add endpoint to get list of cards (#41844)

This commit is contained in:
Frédéric Péters 2020-04-27 14:00:06 +02:00
parent f854b0586a
commit 69a3409b8d
2 changed files with 24 additions and 0 deletions

View File

@ -3012,6 +3012,11 @@ def test_cards(pub, local_user):
formdata.just_created()
formdata.store()
resp = get_app(pub).get('/api/cards/@list', status=403)
resp = get_app(pub).get(sign_uri('/api/cards/@list'))
assert len(resp.json['data']) == 1
assert resp.json['data'][0]['slug'] == 'test'
resp = get_app(pub).get(sign_uri('/api/cards/test/list'), status=403)
resp = get_app(pub).get(sign_uri(

View File

@ -312,6 +312,25 @@ class ApiFormsDirectory(Directory):
class ApiCardsDirectory(Directory):
_q_exports = [('@list', 'list')]
def list(self):
get_response().set_content_type('application/json')
if not (is_url_signed() or (get_request().user and get_request().user.can_go_in_admin())):
raise AccessForbiddenError('unsigned request or user is not admin')
carddefs = CardDef.select(order_by='name', ignore_errors=True, lightweight=True)
data = [
{
'id': x.url_name,
'text': x.name,
'title': x.name,
'slug': x.url_name,
'url': x.get_url(),
'description': x.description or '',
'keywords': x.keywords_list,
} for x in carddefs]
return json.dumps({'data': data, 'err': 0})
def _q_lookup(self, component):
return ApiCardPage(component)