Skip to content

Commit

Permalink
use IConfigurable to get the config in preview plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 25, 2012
1 parent fef11b9 commit 1c028a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 7 additions & 5 deletions ckanext/jsonpreview/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pylons
from logging import getLogger

import ckan.plugins as p
Expand All @@ -19,13 +18,16 @@ class JsonPreview(p.SingletonPlugin):
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)

JSON_FORMATS = ['json']
JSONP_FORMATS = ['jsonp']
proxy_is_enabled = False

def update_config(self, config):
''' Set up the resource library, public directory and
Expand All @@ -35,23 +37,23 @@ def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
p.toolkit.add_resource('theme/public', 'ckanext-jsonpreview')

def proxy_enabled(self):
return pylons.config.get('ckan.resource_proxy_enabled', False)
def configure(self, config):
self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled', False)

def can_preview(self, data_dict):
resource = data_dict['resource']
format_lower = resource['format'].lower()
if format_lower in self.JSONP_FORMATS:
return True
elif format_lower in self.JSON_FORMATS and (self.proxy_enabled() or resource['on_same_domain']):
elif format_lower in self.JSON_FORMATS and (self.proxy_is_enabled or resource['on_same_domain']):
return True
return False

def setup_template_variables(self, context, data_dict):
assert self.can_preview(data_dict)
resource = data_dict['resource']
format_lower = resource['format'].lower()
if format_lower in self.JSON_FORMATS and self.proxy_enabled() and not resource['on_same_domain']:
if format_lower in self.JSON_FORMATS and self.proxy_is_enabled and not resource['on_same_domain']:
base.c.resource['url'] = proxy.get_proxified_resource_url(data_dict)

def preview_template(self, context, data_dict):
Expand Down
10 changes: 8 additions & 2 deletions ckanext/pdfpreview/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ class PdfPreview(p.SingletonPlugin):
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)

PDF = ['pdf', 'x-pdf', 'acrobat', 'vnd.pdf']
proxy_is_enabled = False

def update_config(self, config):
''' Set up the resource library, public directory and
Expand All @@ -33,13 +36,16 @@ def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
p.toolkit.add_resource('theme/public', 'ckanext-pdfpreview')

def configure(self, config):
self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled', False)

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 proxy)
return format_lower in self.PDF and (resource['on_same_domain'] or self.proxy_is_enabled)

def setup_template_variables(self, context, data_dict):
if proxy and not data_dict['resource']['on_same_domain']:
if self.proxy_is_enabled and not data_dict['resource']['on_same_domain']:
base.c.resource['url'] = proxy.get_proxified_resource_url(data_dict)

def preview_template(self, context, data_dict):
Expand Down

0 comments on commit 1c028a1

Please sign in to comment.