Skip to content

Commit

Permalink
Merge branch 'package_show-performance-pt3'
Browse files Browse the repository at this point in the history
Conflicts:
	ckan/logic/action/get.py
  • Loading branch information
joetsoi committed Jul 30, 2015
2 parents a286399 + 5bb1349 commit c259b34
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
7 changes: 5 additions & 2 deletions ckan/controllers/api.py
Expand Up @@ -83,7 +83,9 @@ def _finish(self, status_int, response_data=None,
if response_data is not None:
response.headers['Content-Type'] = CONTENT_TYPES[content_type]
if content_type == 'json':
response_msg = h.json.dumps(response_data)
response_msg = h.json.dumps(
response_data,
for_json=True) # handle objects with for_json methods
else:
response_msg = response_data
# Support "JSONP" callback.
Expand Down Expand Up @@ -164,7 +166,8 @@ def action(self, logic_function, ver=None):
_('Action name not known: %s') % logic_function)

context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version': ver, 'auth_user_obj': c.userobj}
'api_version': ver, 'return_type': 'LazyJSONObject',
'auth_user_obj': c.userobj}
model.Session()._context = context

return_dict = {'help': h.url_for(controller='api',
Expand Down
55 changes: 55 additions & 0 deletions ckan/lib/lazyjson.py
@@ -0,0 +1,55 @@
import simplejson as json
import simplejson.encoder as json_encoder


class LazyJSONObject(dict):
'''An object that behaves like a dict returned from json.loads'''
def __init__(self, json_string):
self._json_string = json_string
self._json_dict = None

def _loads(self):
if not self._json_dict:
self._json_dict = json.loads(self._json_string)
self._json_string = None
return self._json_dict

def __nonzero__(self):
return True

def for_json(self):
if self._json_string:
return JSONString(self._json_string)
return self._json_dict


def _loads_method(name):
def method(self, *args, **kwargs):
return getattr(self._loads(), name)(*args, **kwargs)
return method

for fn in ['__cmp__', '__contains__', '__delitem__', '__eq__', '__ge__',
'__getitem__', '__gt__', '__iter__', '__le__', '__len__', '__lt__',
'__ne__', '__setitem__', 'clear', 'copy', 'fromkeys', 'get',
'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',
'pop', 'popitem', 'setdefault', 'update', 'values']:
setattr(LazyJSONObject, fn, _loads_method(fn))


class JSONString(int):
'''
A type for already-encoded JSON
Fake-out simplejson by subclassing int so that simplejson calls
our __str__ method to produce JSON.
This trick is unpleasant, but significantly less fragile than
subclassing JSONEncoder and modifying its internal workings, or
monkeypatching the simplejson library.
'''
def __init__(self, s):
self.s = s
super(JSONString, self).__init__(-1)

def __str__(self):
return s
4 changes: 3 additions & 1 deletion ckan/logic/action/create.py
Expand Up @@ -285,7 +285,9 @@ def resource_create(context, data_dict):
package_id = _get_or_bust(data_dict, 'package_id')
_get_or_bust(data_dict, 'url')

pkg_dict = _get_action('package_show')(context, {'id': package_id})
pkg_dict = _get_action('package_show')(
dict(context, return_type='dict'),
{'id': package_id})

_check_access('resource_create', context, data_dict)

Expand Down
7 changes: 6 additions & 1 deletion ckan/logic/action/get.py
Expand Up @@ -24,6 +24,7 @@
import ckan.lib.activity_streams as activity_streams
import ckan.lib.datapreview as datapreview
import ckan.authz as authz
import ckan.lib.lazyjson as lazyjson

from ckan.common import _

Expand Down Expand Up @@ -942,7 +943,11 @@ def package_show(context, data_dict):
else:
use_validated_cache = 'schema' not in context
if use_validated_cache and 'validated_data_dict' in search_result:
package_dict = json.loads(search_result['validated_data_dict'])
package_json = search_result['validated_data_dict']
if context.get('return_type') == 'LazyJSONObject':
package_dict = lazyjson.LazyJSONObject(package_json)
else:
package_dict = json.loads(package_json)
package_dict_validated = True
else:
package_dict = json.loads(search_result['data_dict'])
Expand Down
3 changes: 2 additions & 1 deletion ckan/logic/action/update.py
Expand Up @@ -136,7 +136,8 @@ def resource_update(context, data_dict):
del context["resource"]

package_id = resource.package.id
pkg_dict = _get_action('package_show')(context, {'id': package_id})
pkg_dict = _get_action('package_show')(dict(context, return_type='dict'),
{'id': package_id})

for n, p in enumerate(pkg_dict['resources']):
if p['id'] == id:
Expand Down

0 comments on commit c259b34

Please sign in to comment.