From 7a6e8e768f2c16fce48f122b11e510d9b8f240ca Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Tue, 18 Aug 2015 09:42:19 -0400 Subject: [PATCH 1/4] [#2581] fixes for lazyjson --- ckan/lib/lazyjson.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ckan/lib/lazyjson.py b/ckan/lib/lazyjson.py index 6305cb7d894..c4c29160b49 100644 --- a/ckan/lib/lazyjson.py +++ b/ckan/lib/lazyjson.py @@ -28,7 +28,7 @@ def method(self, *args, **kwargs): return getattr(self._loads(), name)(*args, **kwargs) return method -for fn in ['__cmp__', '__contains__', '__delitem__', '__eq__', '__ge__', +for fn in ['__contains__', '__delitem__', '__eq__', '__ge__', '__getitem__', '__gt__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__setitem__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', @@ -47,9 +47,13 @@ class JSONString(int): 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 __new__(cls, s): + obj = super(JSONString, cls).__new__(cls, -1) + obj.s = s + return obj def __str__(self): - return s + return self.s + + def __repr__(self): + return "JSONString(%r)" % self.s From b160cae1d5c8d0ca8514533bc346b3d6dddd7a3a Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Thu, 3 Sep 2015 17:31:29 -0400 Subject: [PATCH 2/4] [#2581] allow lazyjson to be lazy in package_show --- ckan/logic/action/get.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ckan/logic/action/get.py b/ckan/logic/action/get.py index 8e789159299..17614b64562 100644 --- a/ckan/logic/action/get.py +++ b/ckan/logic/action/get.py @@ -978,9 +978,9 @@ def package_show(context, data_dict): for item in plugins.PluginImplementations(plugins.IPackageController): item.read(pkg) - for resource_dict in package_dict['resources']: - for item in plugins.PluginImplementations(plugins.IResourceController): - resource_dict = item.before_show(resource_dict) + for item in plugins.PluginImplementations(plugins.IResourceController): + for resource_dict in package_dict['resources']: + item.before_show(resource_dict) if not package_dict_validated: package_plugin = lib_plugins.lookup_package_plugin( From 54e05dee877ce4c114880bf4e8b8de5efd218514 Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Thu, 3 Sep 2015 17:41:33 -0400 Subject: [PATCH 3/4] [#2581] test that package_show can be lazy --- ckan/tests/logic/action/test_get.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ckan/tests/logic/action/test_get.py b/ckan/tests/logic/action/test_get.py index f722713e279..d37918ac751 100644 --- a/ckan/tests/logic/action/test_get.py +++ b/ckan/tests/logic/action/test_get.py @@ -35,6 +35,15 @@ def foo(key, data, errors, context): eq(dataset2['new_field'], 'foo') + def test_package_show_is_lazy(self): + dataset1 = factories.Dataset() + + dataset2 = helpers.call_action('package_show', id=dataset1['id'], + context=dict(return_type='LazyJSONObject')) + + # LazyJSONObject passed through without being expanded + assert dataset2._json_dict is None + class TestGroupList(helpers.FunctionalTestBase): From 51b5eaf3354ef7f929d16d2b81cd89d16071907c Mon Sep 17 00:00:00 2001 From: Ian Ward Date: Fri, 4 Sep 2015 09:23:17 -0400 Subject: [PATCH 4/4] [#2581] pep8 --- ckan/tests/logic/action/test_get.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ckan/tests/logic/action/test_get.py b/ckan/tests/logic/action/test_get.py index d37918ac751..6f9490f1cb2 100644 --- a/ckan/tests/logic/action/test_get.py +++ b/ckan/tests/logic/action/test_get.py @@ -38,7 +38,9 @@ def foo(key, data, errors, context): def test_package_show_is_lazy(self): dataset1 = factories.Dataset() - dataset2 = helpers.call_action('package_show', id=dataset1['id'], + dataset2 = helpers.call_action( + 'package_show', + id=dataset1['id'], context=dict(return_type='LazyJSONObject')) # LazyJSONObject passed through without being expanded