Skip to content

Commit

Permalink
[ckan#1852] Update image and web view plugins
Browse files Browse the repository at this point in the history
Refactor them to work under the new default views behaviour. Basically
remove all `IPackageController` stuff and move the logic to `can_view`
for deciding if a view should be created.
  • Loading branch information
amercader committed Nov 4, 2014
1 parent d1516e4 commit 337d5df
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 47 deletions.
25 changes: 3 additions & 22 deletions ckanext/imageview/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import ckan.plugins as p
import ckan.lib.datapreview as datapreview

log = logging.getLogger(__name__)
ignore_empty = p.toolkit.get_validator('ignore_empty')
Expand All @@ -9,11 +8,10 @@


class ImageView(p.SingletonPlugin):
'''This extension makes views of images'''
'''This plugin makes views of image resources, using an <img> tag'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)
p.implements(p.IPackageController, inherit=True)

def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
Expand All @@ -26,28 +24,11 @@ def info(self):
'iframed': False}

def can_view(self, data_dict):
return True
return (data_dict['resource'].get('format', '').lower()
in DEFAULT_IMAGE_FORMATS)

def view_template(self, context, data_dict):
return 'image_view.html'

def form_template(self, context, data_dict):
return 'image_form.html'

def add_default_views(self, context, data_dict):
resources = p.toolkit.get_new_resources(context, data_dict)
for resource in resources:
if resource.get('format', '').lower() in DEFAULT_IMAGE_FORMATS:
view = {'title': 'Resource Image',
'description': 'View of the Image',
'resource_id': resource['id'],
'view_type': 'image'}
p.toolkit.get_action('resource_view_create')(
{'defer_commit': True}, view
)

def after_update(self, context, data_dict):
self.add_default_views(context, data_dict)

def after_create(self, context, data_dict):
self.add_default_views(context, data_dict)
28 changes: 5 additions & 23 deletions ckanext/webpageview/plugin.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import logging
import ckan.plugins as p
import ckan.lib.datapreview as datapreview

log = logging.getLogger(__name__)
ignore_empty = p.toolkit.get_validator('ignore_empty')


class WebPageView(p.SingletonPlugin):
'''This extension makes views of webpage'''
'''This plugin makes views of web pages, using an <iframe> tag'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)
p.implements(p.IPackageController, inherit=True)

def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
Expand All @@ -24,29 +22,13 @@ def info(self):
'icon': 'link'}

def can_view(self, data_dict):
return True

resource = data_dict['resource']
return (resource.get('format', '').lower() in ['html', 'htm'] or
resource['url'].split('.')[-1] in ['html', 'htm'])

def view_template(self, context, data_dict):
return 'webpage_view.html'

def form_template(self, context, data_dict):
return 'webpage_form.html'

def add_default_views(self, context, data_dict):
resources = datapreview.get_new_resources(context, data_dict)
for resource in resources:
if (resource.get('format', '').lower() in ['html', 'htm'] or
resource['url'].split('.')[-1] in ['html', 'htm']):
view = {'title': 'Web Page View',
'description': 'View of the webpage',
'resource_id': resource['id'],
'view_type': 'webpage'}
p.toolkit.get_action('resource_view_create')(
{'defer_commit': True, 'ignore_auth': True}, view
)

def after_update(self, context, data_dict):
self.add_default_views(context, data_dict)

def after_create(self, context, data_dict):
self.add_default_views(context, data_dict)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
'recline_grid_view = ckanext.reclineview.plugin:ReclineGridView',
'recline_graph_view = ckanext.reclineview.plugin:ReclineGraphView',
'recline_map_view = ckanext.reclineview.plugin:ReclineMapView',
'image_view = ckanext.imageview.plugin:ImageView',
'webpage_view = ckanext.webpageview.plugin:WebPageView',
# FIXME: Remove deprecated resource previews below. You should use the
# versions as *_view instead.
'text_preview = ckanext.textview.plugin:TextView',
Expand Down Expand Up @@ -126,8 +128,6 @@
],
'ckan.system_plugins': [
'domain_object_mods = ckan.model.modification:DomainObjectModificationExtension',
'image_view = ckanext.imageview.plugin:ImageView',
'webpage_view = ckanext.webpageview.plugin:WebPageView',
],
'ckan.test_plugins': [
'routes_plugin = tests.ckantestplugins:RoutesPlugin',
Expand Down

0 comments on commit 337d5df

Please sign in to comment.