Skip to content

Commit

Permalink
Json list response workaround
Browse files Browse the repository at this point in the history
Flask won't let you jsonify lists, so we'll just do it ourselves
  • Loading branch information
rossjones committed Sep 4, 2015
1 parent 358646a commit 54a9382
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ckan/controllers/flapi.py
@@ -1,9 +1,10 @@
import logging
import json

import ckan.model as model
import ckan.logic as logic

from flask import abort, jsonify, request
from flask import abort, jsonify, request, Response
from flask.views import MethodView

log = logging.getLogger(__name__)
Expand All @@ -24,8 +25,9 @@ def get(self, func_name):

# TODO: Check and pop callback

context = {'model':model, 'session': model.Session, 'user': ''}
try:
response = fn({'model':model, 'session': model.Session}, params)
response = fn(context, params)
except logic.ValidationError, e:
error_dict = e.error_dict
error_dict['__type'] = 'Validation Error'
Expand All @@ -37,6 +39,10 @@ def get(self, func_name):
log.info('Validation error (Action API): %r' % str(e.error_dict))
return jsonify(return_dict), 409

if isinstance(response, list):
# Flask won't allow jsonifying lists because it's unsafe. Apparently.
return Response(json.dumps(response), mimetype='application/json')

return jsonify(response)

def post(self, func_name):
Expand Down

0 comments on commit 54a9382

Please sign in to comment.