Skip to content

Commit

Permalink
[#1251] Set iframe src URL in resource_view snippet.
Browse files Browse the repository at this point in the history
Update textpreview plugin.
  • Loading branch information
johnglover committed Nov 5, 2013
1 parent b20bbf7 commit ede2589
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 54 deletions.
5 changes: 1 addition & 4 deletions ckan/controllers/package.py
Expand Up @@ -1556,10 +1556,7 @@ def resource_view(self, id, resource_id, view_id):
except NotAuthorized:
abort(401, _('Unauthorized to read resource view %s') % view_id)

vars = {'resource_view': view,
'resource': resource,
'package': package}
return render('package/snippets/resource_view.html', extra_vars=vars)
return h.rendered_resource_view(view, resource, package)

def resource_datapreview(self, id, resource_id):
'''
Expand Down
5 changes: 4 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -1639,7 +1639,9 @@ def rendered_resource_view(resource_view, resource, package):
vars = view_plugin.setup_template_variables(context, data_dict) or {}
template = view_plugin.view_template(context, data_dict)
data_dict.update(vars)
return snippet(template, **data_dict)

import ckan.lib.base as base
return literal(base.render(template, extra_vars=data_dict))


def view_resource_url(resource_view, resource, package, **kw):
Expand All @@ -1649,6 +1651,7 @@ def view_resource_url(resource_view, resource, package, **kw):
'''
return resource['url']


def resource_view_is_iframed(resource_view):
'''
Returns true if the given resource view should be displayed in an iframe.
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/resource_view.html
Expand Up @@ -21,7 +21,7 @@ <h1 class="resource-view-title">{{resource_view['title']}}</h1>
</a>
</p>
</div>
<iframe src="" frameborder="0" width="100%" data-module="data-viewer">
<iframe src="{{ h.url(controller='package', action='resource_view', id=package['name'], resource_id=resource['id'], view_id=resource_view['id']) }}" frameborder="0" width="100%" data-module="data-viewer">
<p>{{ _('Your browser does not support iframes.') }}</p>
</iframe>
{% endif %}
Expand Down
80 changes: 34 additions & 46 deletions ckanext/textpreview/plugin.py
@@ -1,39 +1,23 @@
import logging

import ckan.plugins as p

from ckan.common import json
import ckan.plugins as p
import ckanext.resourceproxy.plugin as proxy

log = logging.getLogger(__name__)

try:
import ckanext.resourceproxy.plugin as proxy
except ImportError:
pass


DEFAULT_TEXT_FORMATS = ['text/plain', 'txt', 'plain']
DEFAULT_XML_FORMATS = ['xml', 'rdf', 'rdf+xm', 'owl+xml', 'atom', 'rss']
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,
'xml': 2,
'json': 2,
'jsonp': 2,
}


class TextPreview(p.SingletonPlugin):
'''This extension previews JSON(P).'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IConfigurable, inherit=True)
p.implements(p.IResourcePreview, inherit=True)
p.implements(p.IResourceView, inherit=True)

proxy_is_enabled = False

Expand Down Expand Up @@ -61,37 +45,41 @@ def update_config(self, config):
def configure(self, config):
self.proxy_is_enabled = config.get('ckan.resource_proxy_enabled')

def can_preview(self, data_dict):
def info(self):
return {'name': 'text', 'title': 'Text'}

def can_view(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}
return True
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}
return True
# 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 False

def setup_template_variables(self, context, data_dict):
assert self.can_preview(data_dict)
p.toolkit.c.preview_metadata = json.dumps({
'text_formats': self.text_formats,
'json_formats': self.json_formats,
'jsonp_formats': self.jsonp_formats,
'xml_formats': self.xml_formats
})
resource = data_dict['resource']
format_lower = resource['format'].lower()
if (format_lower in self.no_jsonp_formats and
self.proxy_is_enabled and not resource['on_same_domain']):
url = proxy.get_proxified_resource_url(data_dict)
p.toolkit.c.resource['url'] = url

def preview_template(self, context, data_dict):
return 'text.html'
metadata = {'text_formats': self.text_formats,
'json_formats': self.json_formats,
'jsonp_formats': self.jsonp_formats,
'xml_formats': self.xml_formats}
return {'preview_metadata': json.dumps(metadata),
'resource_json': json.dumps(data_dict['resource'])}
# resource = data_dict['resource']
# format_lower = resource['format'].lower()
# if (format_lower in self.no_jsonp_formats and
# self.proxy_is_enabled and not resource['on_same_domain']):
# url = proxy.get_proxified_resource_url(data_dict)
# p.toolkit.c.resource['url'] = url

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

def form_template(self, context, data_dict):
return 'text_form.html'
Empty file.
@@ -1,5 +1,7 @@
{% extends 'dataviewer/base.html' %}

{% block subtitle %}{% endblock %}

{% block page %}
<div>
<pre data-module="textpreview">
Expand All @@ -10,9 +12,9 @@
</div>

{% block scripts %}
{{ super() }}
<script>
var preview_metadata = {{ h.literal(c.preview_metadata) }};
var preload_resource = {{ h.literal(resource_json) }};
var preview_metadata = {{ h.literal(preview_metadata) }};
</script>
{% endblock %}

Expand Down

0 comments on commit ede2589

Please sign in to comment.