Skip to content

Commit

Permalink
Very basic json(p) preview
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Sep 21, 2012
1 parent 4e03370 commit 86ce9c9
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ckan/config/routing.py
Expand Up @@ -217,6 +217,8 @@ def make_map():
action='resource_datapreview')
m.connect('/dataset/{id}/resource/{resource_id}/pdfpreview',
action='resource_pdfpreview')
m.connect('/dataset/{id}/resource/{resource_id}/jsonpreview',
action='resource_jsonpreview')

# group
map.redirect('/groups', '/group')
Expand Down
18 changes: 18 additions & 0 deletions ckan/controllers/package.py
Expand Up @@ -1314,3 +1314,21 @@ def resource_pdfpreview(self, id, resource_id):
except NotAuthorized:
abort(401, _('Unauthorized to read resource %s') % id)
return render('package/resource_pdfpreview.html')

def resource_jsonpreview(self, id, resource_id):
'''
Embeded page for a resource json-preview.
'''
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}

try:
c.resource = get_action('resource_show')(context,
{'id': resource_id})
c.package = get_action('package_show')(context, {'id': id})
c.resource_json = json.dumps(c.resource)
except NotFound:
abort(404, _('Resource not found'))
except NotAuthorized:
abort(401, _('Unauthorized to read resource %s') % id)
return render('package/resource_jsonpreview.html')
3 changes: 3 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -1328,6 +1328,9 @@ def resource_preview(resource, pkg_id):
elif format_lower == 'pdf':
url = url_for(controller='package', action='resource_pdfpreview',
resource_id=resource['id'], id=pkg_id, qualified=True)
elif format_lower == 'jsonp':
url = url_for(controller='package', action='resource_jsonpreview',
resource_id=resource['id'], id=pkg_id, qualified=True)
elif format_lower in LOADABLE:
url = resource['url']
elif format_lower in DIRECT_EMBEDS:
Expand Down
31 changes: 31 additions & 0 deletions ckan/public/base/datapreview/jsonpreview.js
@@ -0,0 +1,31 @@
var CKAN = CKAN || {};

(function ($) {
$(document).ready(function () {
CKAN.JsonPreview.loadPreview(preload_resource);
});
}(jQuery));

/* ==================== */
/* == JSON Previewer == */
/* ==================== */
CKAN.JsonPreview = function ($, my) {
my.dialogId = 'ckanext-jsonpreview';

// **Public: Loads the json preview **
//
// Returns nothing.
my.loadPreview = function(resourceData) {

$.getJSON(resourceData['url'], function(data) {
var html = JSON.stringify(data, null, 4);
console.log(html);
$('#'+my.dialogId).html(html);
});
};

// Export the CKANEXT object onto the window.
$.extend(true, window, {CKANEXT: {}});
CKANEXT.JSONPREVIEW = my;
return my;
}(jQuery, CKAN.JsonPreview || {});
5 changes: 0 additions & 5 deletions ckan/public/base/datapreview/pdfpreview.js
@@ -1,10 +1,5 @@
var CKAN = CKAN || {};

CKAN.View = CKAN.View || {};
CKAN.Model = CKAN.Model || {};
CKAN.Utils = CKAN.Utils || {};
CKAN.Strings = CKAN.Strings || {};

(function ($) {
$(document).ready(function () {
CKAN.PdfPreview.loadPreview(preload_resource);
Expand Down
4 changes: 4 additions & 0 deletions ckan/public/base/datapreview/resource.config
Expand Up @@ -15,6 +15,7 @@ pdf_src_inline =
datapreview = vendor/jquery.js
datapreview-frame = vendor/jquery.js
pdfpreview = vendor/jquery.js
jsonpreview = vendor/jquery.js

[groups]

Expand Down Expand Up @@ -55,3 +56,6 @@ pdfpreview =
vendor/pdfviewer/viewer.css

css/pdfpreview.css

jsonpreview =
jsonpreview.js
26 changes: 26 additions & 0 deletions ckan/templates/package/resource_jsonpreview.html
@@ -0,0 +1,26 @@
{% extends "base.html" %}

{% set res = c.resource %}

{% block subtitle %}{{ h.dataset_display_name(c.package) }} - {{ h.resource_display_name(res) }}{% endblock %}

{% block page %}
<div id="ckanext-jsonpreview"></div>
{% endblock %}

{% block scripts %}
{{ super() }}
{% resource 'datapreview/datapreview' %}

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


{# remove any ckan styles #}
{% block styles %}
{% endblock %}

{% block custom_styles %}
{% endblock %}

0 comments on commit 86ce9c9

Please sign in to comment.