From f82c5d105f84e65d030574f1b5fcb3c082458e1d Mon Sep 17 00:00:00 2001 From: kindly Date: Thu, 23 Aug 2012 12:29:55 +0100 Subject: [PATCH] [#2733] turn echo is off --- ckan/config/routing.py | 3 +++ ckan/controllers/package.py | 15 +++++++++++++ ckan/public/scripts/application.js | 5 +++++ ckanext/datastore/db.py | 12 +++++++++- ckanext/datastore/tests/test_datastore.py | 27 +++++++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) 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/ckanext/datastore/db.py b/ckanext/datastore/db.py index 3c645f280b4..041fe1830e5 100644 --- a/ckanext/datastore/db.py +++ b/ckanext/datastore/db.py @@ -45,7 +45,7 @@ def _get_engine(context, data_dict): engine = _engines.get(connection_url) if not engine: - engine = sqlalchemy.create_engine(connection_url, echo=True) + engine = sqlalchemy.create_engine(connection_url) _engines[connection_url] = engine return engine @@ -429,6 +429,13 @@ def search_data(context, data_dict): _validate_int(limit, 'limit') _validate_int(offset, 'offset') + #convert to ints so that return dict looks correct + data_dict['limit'] = int(limit) + data_dict['offset'] = int(offset) + + ##pretend there is a limit so we get a count + if limit == 0: + limit = 1 sort = _sort(context, data_dict.get('sort'), field_ids) @@ -453,6 +460,9 @@ def search_data(context, data_dict): converted_row = {} if not data_dict['total']: data_dict['total'] = row['_full_count'] + # we have the total now so we do not need any records + if data_dict['limit'] == 0: + break for field in result_fields: converted_row[field['id']] = convert(row[field['id']], field['type']) 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'}