Skip to content

Commit

Permalink
[#1251] add default views for basic types
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Nov 8, 2013
1 parent c682c40 commit 3f1f484
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ckan/lib/datapreview.py
Expand Up @@ -140,3 +140,13 @@ def get_allowed_view_plugins(data_dict):
if plugin.can_view(data_dict):
can_view.append(plugin)
return can_view

def get_new_resources(context, data_dict):
''' Get out all new resources in this commit,
needs to be run in extension point after_create and after_update '''
import ckan.lib.dictization.model_dictize as model_dictize
model = context['model']
new_objs = model.Session()._object_cache['new']

This comment has been minimized.

Copy link
@vitorbaptista

vitorbaptista Dec 18, 2013

Contributor

@kindly There're many tests failing because there's no model.Session()._object_cache. Do you think it's safe to test if an _object_cache exists and, if not, simply return an empty list? If there's no cache, I think it's safe to assume that there're no new resources, and so an empty list would be the correct value for this.

Am I wrong?

This comment has been minimized.

Copy link
@kindly

kindly Dec 19, 2013

Author Contributor

Yes it looks fine todo the check. I have not done test cleanup on this branch. I am happy you are doing it though!

new_resources = [obj for obj in new_objs
if isinstance(obj, model.Resource)]
return model_dictize.resource_list_dictize(new_resources, context)
21 changes: 21 additions & 0 deletions ckanext/imageview/plugin.py
@@ -1,5 +1,6 @@
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 @@ -12,6 +13,7 @@ class ImageView(p.SingletonPlugin):

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 @@ -30,3 +32,22 @@ def view_template(self, context, data_dict):

def form_template(self, context, data_dict):
return 'image_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 DEFAULT_IMAGE_FORMATS:
view = {'title': 'Resource Image',
'description': 'View of the Image',
'resource_id': resource['id'],
'view_type': 'image'}
context['defer_commit'] = True
p.toolkit.get_action('resource_view_create')(context, view)
context.pop('defer_commit')


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)
20 changes: 20 additions & 0 deletions ckanext/pdfpreview/plugin.py
Expand Up @@ -16,6 +16,7 @@ class PdfPreview(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IResourceView, inherit=True)
p.implements(p.IPackageController, inherit=True)

PDF = ['pdf', 'x-pdf', 'acrobat', 'vnd.pdf']
proxy_is_enabled = False
Expand Down Expand Up @@ -46,3 +47,22 @@ def can_view(self, data_dict):

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

def add_default_views(self, context, data_dict):
resources = datapreview.get_new_resources(context, data_dict)
for resource in resources:
if self.can_view({'package':data_dict, 'resource':resource}):
view = {'title': 'PDF View',
'description': 'PDF view of the resource.',
'resource_id': resource['id'],
'view_type': 'pdf'}
context['defer_commit'] = True
p.toolkit.get_action('resource_view_create')(context, view)
context.pop('defer_commit')


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)
29 changes: 28 additions & 1 deletion ckanext/textpreview/plugin.py
Expand Up @@ -7,7 +7,7 @@

log = logging.getLogger(__name__)

DEFAULT_TEXT_FORMATS = ['text/plain', 'txt', 'plain']
DEFAULT_TEXT_FORMATS = ['text/plain', 'txt', 'plain', 'csv', 'tsv']
DEFAULT_XML_FORMATS = ['xml', 'rdf', 'rdf+xm', 'owl+xml', 'atom', 'rss']
DEFAULT_JSON_FORMATS = ['json', 'gjson', 'geojson']
DEFAULT_JSONP_FORMATS = ['jsonp']
Expand All @@ -19,6 +19,7 @@ class TextPreview(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IResourceView, inherit=True)
p.implements(p.IPackageController, inherit=True)

proxy_is_enabled = False

Expand Down Expand Up @@ -78,3 +79,29 @@ def view_template(self, context, data_dict):

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

def add_default_views(self, context, data_dict):
resources = datapreview.get_new_resources(context, data_dict)
for resource in resources:
if self.can_view({'package':data_dict, 'resource':resource}):
format = resource.get('format', '')
if format.lower() in ['csv', 'tsv']:
continue
view = {
'title': 'Text View',
'description': 'View of the {format} file'.format(
format=format
),
'resource_id': resource['id'],
'view_type': 'text'
}
context['defer_commit'] = True
p.toolkit.get_action('resource_view_create')(context, view)
context.pop('defer_commit')


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)
22 changes: 22 additions & 0 deletions ckanext/webpageview/plugin.py
@@ -1,5 +1,6 @@
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 @@ -10,6 +11,7 @@ class WebPageView(p.SingletonPlugin):

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 @@ -28,3 +30,23 @@ def view_template(self, context, data_dict):

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'}
context['defer_commit'] = True
p.toolkit.get_action('resource_view_create')(context, view)
context.pop('defer_commit')


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)

0 comments on commit 3f1f484

Please sign in to comment.