From 3b482dcba6f98d26862f98d9180409497bc99fa3 Mon Sep 17 00:00:00 2001 From: kindly Date: Thu, 23 Aug 2012 19:17:14 +0100 Subject: [PATCH] [#2888] first attempt and datapreview on its own --- ckan/config/routing.py | 3 ++ ckan/controllers/package.py | 15 ++++++ ckan/public/scripts/application.js | 5 ++ ckan/templates/package/datapreview.html | 57 +++++++++++++++++++++++ ckanext/datastore/tests/test_datastore.py | 27 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 ckan/templates/package/datapreview.html diff --git a/ckan/config/routing.py b/ckan/config/routing.py index a2ab58634b0..c8fc7c9c1d6 100644 --- a/ckan/config/routing.py +++ b/ckan/config/routing.py @@ -194,6 +194,9 @@ def make_map(): ) m.connect('/dataset/{id}.{format}', action='read') m.connect('/dataset/{id}', action='read') + + m.connect('/datapreview/{resource_id}', action='data_preview') + m.connect('/dataset/{id}/resource/{resource_id}', action='resource_read') m.connect('/dataset/{id}/resource/{resource_id}/download', diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index dff9a15c8a2..ec5f69841e6 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -794,6 +794,21 @@ def resource_read(self, id, resource_id): c.related_count = c.pkg.related_count return render('package/resource_read.html') + def data_preview(self, resource_id): + context = {'model': model, 'session': model.Session, + 'user': c.user or c.author} + + try: + c.resource = get_action('resource_show')(context, + {'id': resource_id}) + c.resource_json = json.dumps(c.resource) + c.pkg_dict = c.package + except NotFound: + abort(404, _('Resource not found')) + except NotAuthorized: + abort(401, _('Unauthorized to read resource %s') % id) + return render('package/datapreview.html') + def resource_download(self, id, resource_id): """ Provides a direct download by redirecting the user to the url stored diff --git a/ckan/public/scripts/application.js b/ckan/public/scripts/application.js index 87cba35b006..34a78deb418 100644 --- a/ckan/public/scripts/application.js +++ b/ckan/public/scripts/application.js @@ -46,6 +46,11 @@ CKAN.Utils = CKAN.Utils || {}; CKAN.DataPreview.loadPreviewDialog(preload_resource); } + var isResourceViewNew = $('#ckanext-datapreview').length > 0; + if (isResourceViewNew) { + CKAN.DataPreview.loadPreviewDialog(preload_resource); + } + var isEmbededDataviewer = $('body.package.resource_embedded_dataviewer').length > 0; if (isEmbededDataviewer) { CKAN.DataPreview.loadEmbeddedPreview(preload_resource, reclineState); diff --git a/ckan/templates/package/datapreview.html b/ckan/templates/package/datapreview.html new file mode 100644 index 00000000000..36d6a99f9a2 --- /dev/null +++ b/ckan/templates/package/datapreview.html @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + diff --git a/ckanext/datastore/tests/test_datastore.py b/ckanext/datastore/tests/test_datastore.py index 4dd3fc43cb7..ed381fe6767 100644 --- a/ckanext/datastore/tests/test_datastore.py +++ b/ckanext/datastore/tests/test_datastore.py @@ -575,6 +575,33 @@ def test_search_limit(self): assert result['total'] == 2 assert result['records'] == [self.expected_records[0]] + + data = {'resource_id': self.data['resource_id'], + 'limit': 0} + postparams = '%s=1' % json.dumps(data) + auth = {'Authorization': str(self.sysadmin_user.apikey)} + res = self.app.post('/api/action/datastore_search', params=postparams, + extra_environ=auth) + res_dict = json.loads(res.body) + assert res_dict['success'] is True + result = res_dict['result'] + assert result['total'] == 2 + assert result['records'] == [] + + #filter returns no results with 0 limit + data = {'resource_id': self.data['resource_id'], + 'limit': 0, + 'filters': {u'b\xfck': 'annakar'}} + postparams = '%s=1' % json.dumps(data) + auth = {'Authorization': str(self.sysadmin_user.apikey)} + res = self.app.post('/api/action/datastore_search', params=postparams, + extra_environ=auth) + res_dict = json.loads(res.body) + assert res_dict['success'] is True + result = res_dict['result'] + assert result['total'] == 0 + assert result['records'] == [] + def test_search_invalid_limit(self): data = {'resource_id': self.data['resource_id'], 'limit': 'bad'}