From 9098fb902e58d095217c85c8a6f0eff63f569033 Mon Sep 17 00:00:00 2001 From: tobes Date: Tue, 18 Jun 2013 17:29:22 +0100 Subject: [PATCH 1/9] [#1002] Allow can_preview to return a dict --- ckan/controllers/package.py | 53 +++++++++++++++++++++++++------------ ckan/plugins/interfaces.py | 16 +++++++++-- 2 files changed, 50 insertions(+), 19 deletions(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 65541ee33bf..3d6ea1fc2a8 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1338,27 +1338,46 @@ def resource_datapreview(self, id, resource_id): on_same_domain = datapreview.resource_is_on_same_domain(data_dict) data_dict['resource']['on_same_domain'] = on_same_domain - # FIXME this wants to not use plugins as it is an imported name - # and we already import it an p should really only be in - # extensu=ions in my opinion also just make it look nice and be - # readable grrrrrr - plugins = p.PluginImplementations(p.IResourcePreview) - plugins_that_can_preview = [plugin for plugin in plugins - if plugin.can_preview(data_dict)] - if len(plugins_that_can_preview) == 0: + plugins_that_can_preview = [] + plugins_fixable = [] + for plugin in p.PluginImplementations(p.IResourcePreview): + p_info = {'plugin': plugin, 'quality': 1} + data = plugin.can_preview(data_dict) + # old school plugins return true/False + if isinstance(data, bool): + p_info['can_preview'] = data + else: + # new school provide a dict + p_info.update(data) + # if we can preview + if p_info['can_preview']: + plugins_that_can_preview.append(p_info) + elif p_info.get('fixable'): + plugins_fixable.append(p_info) + num_plugins = len(plugins_that_can_preview) + if num_plugins == 0: + for plug in plugins_fixable: + log.info('%s would allow previews if %s' % ( + plug['plugin'], plugin['fixable'])) abort(409, _('No preview has been defined.')) - if len(plugins_that_can_preview) > 1: - log.warn('Multiple previews are possible. {0}'.format( - plugins_that_can_preview)) - - plugin = plugins_that_can_preview[0] - plugin.setup_template_variables(context, data_dict) - + elif num_plugins > 1: + preview_plugin = plugins_that_can_preview[0]['plugin'] + else: + # multiple plugins + plugs = [pl['plugin'] for pl in plugins_that_can_preview] + log.warn('Multiple previews are possible. {0}'.format(plugs)) + qual = max(plugins_that_can_preview, key=lambda x:x['quality']) + # we are just grabing one of the best quality here + preview_plugins = [pl['plugin'] for pl in + plugins_that_can_preview + if pl['quality'] == qual] + preview_plugin = preview_plugins[0] + + preview_plugin.setup_template_variables(context, data_dict) c.resource_json = json.dumps(c.resource) - except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % id) else: - return render(plugin.preview_template(context, data_dict)) + return render(preview_plugin.preview_template(context, data_dict)) diff --git a/ckan/plugins/interfaces.py b/ckan/plugins/interfaces.py index a37d3c96ae8..8acfc34aad6 100644 --- a/ckan/plugins/interfaces.py +++ b/ckan/plugins/interfaces.py @@ -202,8 +202,20 @@ class IResourcePreview(Interface): def can_preview(self, data_dict): ''' - Return True if the extension can preview the resource. The ``data_dict`` - contains the resource and the package. + Returns info on whether the plugin can preview the resource. + + This can be done in two ways. + The old way is to just return True or False. + The new way is to return a dict with the following + { + 'can_preview': bool - if the extension can preview the resource + 'fixable': string - if the extension cannot preview but could for + example if the resource_proxy was enabled. + 'quality': int - how good the preview is 1-poor, 2-average, 3-good + used if multiple extensions can preview + } + + The ``data_dict`` contains the resource and the package. Make sure to ckeck the ``on_same_domain`` value of the resource or the url if your preview requires the resource to be on From d779b485038d2027389ae9c907cbc143516cea61 Mon Sep 17 00:00:00 2001 From: tobes Date: Wed, 19 Jun 2013 10:19:43 +0100 Subject: [PATCH 2/9] [#1002] Better preview plugin selection --- ckan/controllers/package.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 3d6ea1fc2a8..30add28276c 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1366,12 +1366,8 @@ def resource_datapreview(self, id, resource_id): # multiple plugins plugs = [pl['plugin'] for pl in plugins_that_can_preview] log.warn('Multiple previews are possible. {0}'.format(plugs)) - qual = max(plugins_that_can_preview, key=lambda x:x['quality']) - # we are just grabing one of the best quality here - preview_plugins = [pl['plugin'] for pl in - plugins_that_can_preview - if pl['quality'] == qual] - preview_plugin = preview_plugins[0] + preview_plugin = max(plugins_that_can_preview, + key=lambda x: x['quality']) preview_plugin.setup_template_variables(context, data_dict) c.resource_json = json.dumps(c.resource) From 4426a87ff6ad128787074ce20eb700dc4cf966bc Mon Sep 17 00:00:00 2001 From: tobes Date: Wed, 19 Jun 2013 10:51:09 +0100 Subject: [PATCH 3/9] [#1002] Fix failing tests --- ckan/controllers/package.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 30add28276c..2fd1d67e414 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1367,7 +1367,7 @@ def resource_datapreview(self, id, resource_id): plugs = [pl['plugin'] for pl in plugins_that_can_preview] log.warn('Multiple previews are possible. {0}'.format(plugs)) preview_plugin = max(plugins_that_can_preview, - key=lambda x: x['quality']) + key=lambda x: x['quality'])['plugin'] preview_plugin.setup_template_variables(context, data_dict) c.resource_json = json.dumps(c.resource) From 5422237aa647bc8d778d5f4c3eff55314f400bc2 Mon Sep 17 00:00:00 2001 From: tobes Date: Wed, 19 Jun 2013 11:41:25 +0100 Subject: [PATCH 4/9] [#1002] Code cleanups to catch fixable plugins and reduce code duplication --- ckan/controllers/package.py | 42 ++++-------------------- ckan/lib/datapreview.py | 50 ++++++++++++++++++++++++----- ckan/lib/helpers.py | 2 +- ckan/tests/test_coding_standards.py | 1 - 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 2fd1d67e414..7cbc6c1f41f 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1319,9 +1319,9 @@ def resource_datapreview(self, id, resource_id): ''' Embeded page for a resource data-preview. - Depending on the type, different previews are loaded. - This could be an img tag where the image is loaded directly or an iframe that - embeds a webpage, recline or a pdf preview. + Depending on the type, different previews are loaded. This could be an + img tag where the image is loaded directly or an iframe that embeds a + webpage, recline or a pdf preview. ''' context = { 'model': model, @@ -1335,39 +1335,11 @@ def resource_datapreview(self, id, resource_id): c.package = get_action('package_show')(context, {'id': id}) data_dict = {'resource': c.resource, 'package': c.package} - on_same_domain = datapreview.resource_is_on_same_domain(data_dict) - data_dict['resource']['on_same_domain'] = on_same_domain - - plugins_that_can_preview = [] - plugins_fixable = [] - for plugin in p.PluginImplementations(p.IResourcePreview): - p_info = {'plugin': plugin, 'quality': 1} - data = plugin.can_preview(data_dict) - # old school plugins return true/False - if isinstance(data, bool): - p_info['can_preview'] = data - else: - # new school provide a dict - p_info.update(data) - # if we can preview - if p_info['can_preview']: - plugins_that_can_preview.append(p_info) - elif p_info.get('fixable'): - plugins_fixable.append(p_info) - num_plugins = len(plugins_that_can_preview) - if num_plugins == 0: - for plug in plugins_fixable: - log.info('%s would allow previews if %s' % ( - plug['plugin'], plugin['fixable'])) + + preview_plugin = datapreview.get_preview_plugin(data_dict) + + if not preview_plugin: abort(409, _('No preview has been defined.')) - elif num_plugins > 1: - preview_plugin = plugins_that_can_preview[0]['plugin'] - else: - # multiple plugins - plugs = [pl['plugin'] for pl in plugins_that_can_preview] - log.warn('Multiple previews are possible. {0}'.format(plugs)) - preview_plugin = max(plugins_that_can_preview, - key=lambda x: x['quality'])['plugin'] preview_plugin.setup_template_variables(context, data_dict) c.resource_json = json.dumps(c.resource) diff --git a/ckan/lib/datapreview.py b/ckan/lib/datapreview.py index 27e7804bc83..ae6d96b088c 100644 --- a/ckan/lib/datapreview.py +++ b/ckan/lib/datapreview.py @@ -6,6 +6,7 @@ """ import urlparse +import logging import pylons.config as config @@ -16,6 +17,8 @@ 'n3', 'n-triples', 'turtle', 'plain', 'atom', 'rss', 'txt'] +log = logging.getLogger(__name__) + def compare_domains(urls): ''' Return True if the domains of the provided are the same. @@ -41,7 +44,7 @@ def compare_domains(urls): return True -def resource_is_on_same_domain(data_dict): +def _on_same_domain(data_dict): # compare CKAN domain and resource URL ckan_url = config.get('ckan.site_url', '//localhost:5000') resource_url = data_dict['resource']['url'] @@ -49,14 +52,45 @@ def resource_is_on_same_domain(data_dict): return compare_domains([ckan_url, resource_url]) -def can_be_previewed(data_dict): - ''' - Determines whether there is an extension that can preview the resource. +def get_preview_plugin(data_dict): + '''Determines whether there is an extension that can preview the resource. :param data_dict: contains a resource and package dict. The resource dict has to have a value for ``on_same_domain`` :type data_dict: dictionary - ''' - data_dict['resource']['on_same_domain'] = resource_is_on_same_domain(data_dict) - plugins = p.PluginImplementations(p.IResourcePreview) - return any(plugin.can_preview(data_dict) for plugin in plugins) + + Returns a dict of plugins that can preview or ones that are fixable''' + + data_dict['resource']['on_same_domain'] = _on_same_domain(data_dict) + + plugins_that_can_preview = [] + plugins_fixable = [] + for plugin in p.PluginImplementations(p.IResourcePreview): + p_info = {'plugin': plugin, 'quality': 1} + data = plugin.can_preview(data_dict) + # old school plugins return true/False + if isinstance(data, bool): + p_info['can_preview'] = data + else: + # new school provide a dict + p_info.update(data) + # if we can preview + if p_info['can_preview']: + plugins_that_can_preview.append(p_info) + elif p_info.get('fixable'): + plugins_fixable.append(p_info) + + num_plugins = len(plugins_that_can_preview) + if num_plugins == 0: + for plug in plugins_fixable: + log.info('%s would allow previews if %s' % ( + plug['plugin'], plug['fixable'])) + elif num_plugins > 1: + preview_plugin = plugins_that_can_preview[0]['plugin'] + else: + # multiple plugins + plugs = [pl['plugin'] for pl in plugins_that_can_preview] + log.warn('Multiple previews are possible. {0}'.format(plugs)) + preview_plugin = max(plugins_that_can_preview, + key=lambda x: x['quality'])['plugin'] + return preview_plugin diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 891660f3b7e..ecc5a9310bf 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -1500,7 +1500,7 @@ def resource_preview(resource, pkg_id): if not loadable_in_iframe: loadable_in_iframe = datapreview.DEFAULT_LOADABLE_IFRAME - if datapreview.can_be_previewed(data_dict): + if datapreview.get_preview_plugin(data_dict): url = url_for(controller='package', action='resource_datapreview', resource_id=resource['id'], id=pkg_id, qualified=True) elif format_lower in direct_embed: diff --git a/ckan/tests/test_coding_standards.py b/ckan/tests/test_coding_standards.py index f5961074d32..96d9ace093f 100644 --- a/ckan/tests/test_coding_standards.py +++ b/ckan/tests/test_coding_standards.py @@ -505,7 +505,6 @@ class TestPep8(object): 'ckan/lib/captcha.py', 'ckan/lib/cli.py', 'ckan/lib/create_test_data.py', - 'ckan/lib/datapreview.py', 'ckan/lib/dictization/__init__.py', 'ckan/lib/dictization/model_dictize.py', 'ckan/lib/dictization/model_save.py', From 569121dea911cf282de75c50bc5d3170c36317fe Mon Sep 17 00:00:00 2001 From: tobes Date: Wed, 19 Jun 2013 12:11:24 +0100 Subject: [PATCH 5/9] [#1002] Code fixes to changes --- ckan/controllers/package.py | 2 +- ckan/lib/datapreview.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 7cbc6c1f41f..db7c24f1b68 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -1338,7 +1338,7 @@ def resource_datapreview(self, id, resource_id): preview_plugin = datapreview.get_preview_plugin(data_dict) - if not preview_plugin: + if preview_plugin is None: abort(409, _('No preview has been defined.')) preview_plugin.setup_template_variables(context, data_dict) diff --git a/ckan/lib/datapreview.py b/ckan/lib/datapreview.py index ae6d96b088c..7ac93e0a76e 100644 --- a/ckan/lib/datapreview.py +++ b/ckan/lib/datapreview.py @@ -82,13 +82,16 @@ def get_preview_plugin(data_dict): num_plugins = len(plugins_that_can_preview) if num_plugins == 0: + # we didn't find any. see if any could be made to work for plug in plugins_fixable: log.info('%s would allow previews if %s' % ( plug['plugin'], plug['fixable'])) - elif num_plugins > 1: + preview_plugin = None + elif num_plugins == 1: + # just one available preview_plugin = plugins_that_can_preview[0]['plugin'] else: - # multiple plugins + # multiple plugins so get the best one plugs = [pl['plugin'] for pl in plugins_that_can_preview] log.warn('Multiple previews are possible. {0}'.format(plugs)) preview_plugin = max(plugins_that_can_preview, From 09361a16dbb64f12fa15a8076768c31ca591139c Mon Sep 17 00:00:00 2001 From: tobes Date: Thu, 20 Jun 2013 11:16:21 +0100 Subject: [PATCH 6/9] [#1002] Update text/pdf previews to new method --- ckan/lib/datapreview.py | 2 +- ckan/tests/test_coding_standards.py | 1 - ckanext/pdfpreview/plugin.py | 18 ++++++++++++++---- ckanext/pdfpreview/tests/test_preview.py | 8 ++++---- ckanext/textpreview/plugin.py | 22 +++++++++++++++++----- ckanext/textpreview/tests/test_preview.py | 12 ++++++------ 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/ckan/lib/datapreview.py b/ckan/lib/datapreview.py index 7ac93e0a76e..6d9a5ef8ea9 100644 --- a/ckan/lib/datapreview.py +++ b/ckan/lib/datapreview.py @@ -84,7 +84,7 @@ def get_preview_plugin(data_dict): if num_plugins == 0: # we didn't find any. see if any could be made to work for plug in plugins_fixable: - log.info('%s would allow previews if %s' % ( + log.info('%s would allow previews to fix: %s' % ( plug['plugin'], plug['fixable'])) preview_plugin = None elif num_plugins == 1: diff --git a/ckan/tests/test_coding_standards.py b/ckan/tests/test_coding_standards.py index 96d9ace093f..35108dbaaf2 100644 --- a/ckan/tests/test_coding_standards.py +++ b/ckan/tests/test_coding_standards.py @@ -760,7 +760,6 @@ class TestPep8(object): 'ckanext/example_itemplatehelpers/plugin.py', 'ckanext/multilingual/plugin.py', 'ckanext/multilingual/tests/test_multilingual_plugin.py', - 'ckanext/pdfpreview/plugin.py', 'ckanext/reclinepreview/plugin.py', 'ckanext/reclinepreview/tests/test_preview.py', 'ckanext/resourceproxy/plugin.py', diff --git a/ckanext/pdfpreview/plugin.py b/ckanext/pdfpreview/plugin.py index 69cfc2c1181..7f88ae7f9be 100644 --- a/ckanext/pdfpreview/plugin.py +++ b/ckanext/pdfpreview/plugin.py @@ -37,16 +37,26 @@ def update_config(self, config): p.toolkit.add_resource('theme/public', 'ckanext-pdfpreview') def configure(self, config): - self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled', False) + enabled = config.get('ckan.resource_proxy_enabled', False) + self.proxy_is_enabled = enabled def can_preview(self, data_dict): resource = data_dict['resource'] format_lower = resource['format'].lower() - return format_lower in self.PDF and (resource['on_same_domain'] or self.proxy_is_enabled) + if format_lower in self.PDF: + if resource['on_same_domain'] or self.proxy_is_enabled: + return {'can_preview': True, 'quality': 2} + else: + return {'can_preview': False, + 'fixable': 'Enable resource_proxy', + 'quality': 2} + return {'can_preview': False} def setup_template_variables(self, context, data_dict): - if self.proxy_is_enabled and not data_dict['resource']['on_same_domain']: - base.c.resource['url'] = proxy.get_proxified_resource_url(data_dict) + if (self.proxy_is_enabled + and not data_dict['resource']['on_same_domain']): + url = proxy.get_proxified_resource_url(data_dict) + base.c.resource['url'] = url def preview_template(self, context, data_dict): return 'pdf.html' diff --git a/ckanext/pdfpreview/tests/test_preview.py b/ckanext/pdfpreview/tests/test_preview.py index fb6aa643332..9a8326ac78f 100644 --- a/ckanext/pdfpreview/tests/test_preview.py +++ b/ckanext/pdfpreview/tests/test_preview.py @@ -54,7 +54,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -62,7 +62,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -70,7 +70,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -78,7 +78,7 @@ def test_can_preview(self): 'on_same_domain': False } } - assert not self.p.can_preview(data_dict) + assert not self.p.can_preview(data_dict)['can_preview'] def test_js_included(self): res_id = self.resource['id'] diff --git a/ckanext/textpreview/plugin.py b/ckanext/textpreview/plugin.py index 6ae0a2d5cbb..20adc783b22 100644 --- a/ckanext/textpreview/plugin.py +++ b/ckanext/textpreview/plugin.py @@ -18,6 +18,13 @@ DEFAULT_JSON_FORMATS = ['json', 'gjson', 'geojson'] DEFAULT_JSONP_FORMATS = ['jsonp'] +# returned preview quality will be one but can be overridden here +QUALITY = { + 'text/plain': 2, + 'txt': 2, + 'plain': 2, +} + class TextPreview(p.SingletonPlugin): """This extension previews JSON(P) @@ -74,12 +81,17 @@ def configure(self, config): def can_preview(self, data_dict): resource = data_dict['resource'] format_lower = resource['format'].lower() + quality = QUALITY.get(format_lower, 1) if format_lower in self.jsonp_formats: - return True - elif format_lower in self.no_jsonp_formats and ( - self.proxy_is_enabled or resource['on_same_domain']): - return True - return False + return {'can_preview': True, 'quality': quality} + elif format_lower in self.no_jsonp_formats: + if self.proxy_is_enabled or resource['on_same_domain']: + return {'can_preview': True, 'quality': quality} + else: + return {'can_preview': False, + 'fixable': 'Enable resource_proxy', + 'quality': quality} + return {'can_preview': False} def setup_template_variables(self, context, data_dict): assert self.can_preview(data_dict) diff --git a/ckanext/textpreview/tests/test_preview.py b/ckanext/textpreview/tests/test_preview.py index 2c3ba26f52c..4b6079f0b12 100644 --- a/ckanext/textpreview/tests/test_preview.py +++ b/ckanext/textpreview/tests/test_preview.py @@ -53,7 +53,7 @@ def test_can_preview(self): 'format': 'jsonp' } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -61,7 +61,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -69,7 +69,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -77,7 +77,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert self.p.can_preview(data_dict) + assert self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -85,7 +85,7 @@ def test_can_preview(self): 'on_same_domain': True } } - assert not self.p.can_preview(data_dict) + assert not self.p.can_preview(data_dict)['can_preview'] data_dict = { 'resource': { @@ -93,7 +93,7 @@ def test_can_preview(self): 'on_same_domain': False } } - assert not self.p.can_preview(data_dict) + assert not self.p.can_preview(data_dict)['can_preview'] def test_js_included(self): res_id = self.resource['id'] From b3424e587e622ff1d345de8e6a80a1535bd6a514 Mon Sep 17 00:00:00 2001 From: tobes Date: Thu, 20 Jun 2013 12:01:22 +0100 Subject: [PATCH 7/9] [#1002] Some cleanups of the plugins since I was there --- ckanext/pdfpreview/plugin.py | 20 +++---------- ckanext/textpreview/plugin.py | 56 ++++++++++++----------------------- 2 files changed, 23 insertions(+), 53 deletions(-) diff --git a/ckanext/pdfpreview/plugin.py b/ckanext/pdfpreview/plugin.py index 7f88ae7f9be..f6be15d708c 100644 --- a/ckanext/pdfpreview/plugin.py +++ b/ckanext/pdfpreview/plugin.py @@ -1,11 +1,9 @@ -from logging import getLogger +import logging import ckan.plugins as p -import ckan.lib.base as base -log = getLogger(__name__) +log = logging.getLogger(__name__) -proxy = False try: import ckanext.resourceproxy.plugin as proxy except ImportError: @@ -13,14 +11,7 @@ class PdfPreview(p.SingletonPlugin): - """This extension previews PDFs - - This extension implements two interfaces - - - ``IConfigurer`` allows to modify the configuration - - ``IConfigurable`` get the configuration - - ``IResourcePreview`` allows to add previews - """ + '''This extension previews PDFs. ''' p.implements(p.IConfigurer, inherit=True) p.implements(p.IConfigurable, inherit=True) p.implements(p.IResourcePreview, inherit=True) @@ -29,9 +20,6 @@ class PdfPreview(p.SingletonPlugin): proxy_is_enabled = False def update_config(self, config): - ''' Set up the resource library, public directory and - template directory for the preview - ''' p.toolkit.add_public_directory(config, 'theme/public') p.toolkit.add_template_directory(config, 'theme/templates') p.toolkit.add_resource('theme/public', 'ckanext-pdfpreview') @@ -56,7 +44,7 @@ def setup_template_variables(self, context, data_dict): if (self.proxy_is_enabled and not data_dict['resource']['on_same_domain']): url = proxy.get_proxified_resource_url(data_dict) - base.c.resource['url'] = url + p.toolkit.c.resource['url'] = url def preview_template(self, context, data_dict): return 'pdf.html' diff --git a/ckanext/textpreview/plugin.py b/ckanext/textpreview/plugin.py index 20adc783b22..e43530d61b9 100644 --- a/ckanext/textpreview/plugin.py +++ b/ckanext/textpreview/plugin.py @@ -1,12 +1,11 @@ -from logging import getLogger +import logging import ckan.plugins as p from ckan.common import json -log = getLogger(__name__) +log = logging.getLogger(__name__) -proxy = False try: import ckanext.resourceproxy.plugin as proxy except ImportError: @@ -27,14 +26,8 @@ class TextPreview(p.SingletonPlugin): - """This extension previews JSON(P) + '''This extension previews JSON(P).''' - This extension implements two interfaces - - - ``IConfigurer`` allows to modify the configuration - - ``IConfigurable`` get the configuration - - ``IResourcePreview`` allows to add previews - """ p.implements(p.IConfigurer, inherit=True) p.implements(p.IConfigurable, inherit=True) p.implements(p.IResourcePreview, inherit=True) @@ -42,29 +35,17 @@ class TextPreview(p.SingletonPlugin): proxy_is_enabled = False def update_config(self, config): - ''' Set up the resource library, public directory and - template directory for the preview - ''' - - self.text_formats = config.get( - 'ckan.preview.text_formats', '').split() - if not self.text_formats: - self.text_formats = DEFAULT_TEXT_FORMATS - - self.xml_formats = config.get( - 'ckan.preview.xml_formats', '').split() - if not self.xml_formats: - self.xml_formats = DEFAULT_XML_FORMATS - - self.json_formats = config.get( - 'ckan.preview.json_formats', '').split() - if not self.json_formats: - self.json_formats = DEFAULT_JSON_FORMATS - - self.jsonp_formats = config.get( - 'ckan.preview.jsonp_formats', '').split() - if not self.jsonp_formats: - self.jsonp_formats = DEFAULT_JSONP_FORMATS + text_formats = config.get('ckan.preview.text_formats', '').split() + self.text_formats = text_formats or DEFAULT_TEXT_FORMATS + + xml_formats = config.get('ckan.preview.xml_formats', '').split() + self.xml_formats = xml_formats or DEFAULT_XML_FORMATS + + json_formats = config.get('ckan.preview.json_formats', '').split() + self.json_formats = json_formats or DEFAULT_JSON_FORMATS + + jsonp_formats = config.get('ckan.preview.jsonp_formats', '').split() + self.jsonp_formats = jsonp_formats or DEFAULT_JSONP_FORMATS self.no_jsonp_formats = (self.text_formats + self.xml_formats + @@ -75,13 +56,14 @@ def update_config(self, config): p.toolkit.add_resource('theme/public', 'ckanext-textpreview') def configure(self, config): - self.proxy_is_enabled = config.get( - 'ckan.resource_proxy_enabled', False) + self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled') def can_preview(self, data_dict): resource = data_dict['resource'] format_lower = resource['format'].lower() + quality = QUALITY.get(format_lower, 1) + if format_lower in self.jsonp_formats: return {'can_preview': True, 'quality': quality} elif format_lower in self.no_jsonp_formats: @@ -105,8 +87,8 @@ def setup_template_variables(self, context, data_dict): format_lower = resource['format'].lower() if (format_lower in self.no_jsonp_formats and self.proxy_is_enabled and not resource['on_same_domain']): - p.toolkit.c.resource['url'] = proxy.get_proxified_resource_url( - data_dict) + url = proxy.get_proxified_resource_url(data_dict) + p.toolkit.c.resource['url'] = url def preview_template(self, context, data_dict): return 'text.html' From 0fb774029dc32bb074a62ce9e9f9c5fd557d522f Mon Sep 17 00:00:00 2001 From: tobes Date: Mon, 24 Jun 2013 12:54:26 +0100 Subject: [PATCH 8/9] [#1036] Function was not defined --- ckan/controllers/organization.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ckan/controllers/organization.py b/ckan/controllers/organization.py index b411f8e2b31..b44d95e057f 100644 --- a/ckan/controllers/organization.py +++ b/ckan/controllers/organization.py @@ -19,7 +19,7 @@ def _group_form(self, group_type=None): return 'organization/new_organization_form.html' def _form_to_db_schema(self, group_type=None): - return lookup_group_plugin(group_type).form_to_db_schema() + return group.lookup_group_plugin(group_type).form_to_db_schema() def _db_to_form_schema(self, group_type=None): '''This is an interface to manipulate data from the database @@ -48,7 +48,7 @@ def _read_template(self, group_type): return 'organization/read.html' def _history_template(self, group_type): - return lookup_group_plugin(group_type).history_template() + return group.lookup_group_plugin(group_type).history_template() def _edit_template(self, group_type): return 'organization/edit.html' From 4dbebf9e27da44e18458bda185e837d9e3bdc5c4 Mon Sep 17 00:00:00 2001 From: tobes Date: Tue, 25 Jun 2013 12:47:12 +0100 Subject: [PATCH 9/9] [#1040] fix license translation --- ckan/controllers/group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index 310be042f1b..ccef46bfe78 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -281,7 +281,7 @@ def pager_url(q=None, page=None): default_facet_titles = {'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), - 'license': _('License')} + 'license_id': _('License')} for facet in g.facets: if facet in default_facet_titles: