[ckan-changes] commit/ckanext-datapreview: John Glover: Update preview to work with latest webstore, and tidy up config vars

Bitbucket commits-noreply at bitbucket.org
Tue Aug 9 16:28:11 UTC 2011


1 new changeset in ckanext-datapreview:

http://bitbucket.org/okfn/ckanext-datapreview/changeset/bfb7048d3091/
changeset:   bfb7048d3091
user:        John Glover
date:        2011-08-09 18:27:06
summary:     Update preview to work with latest webstore, and tidy up config vars
affected #:  2 files (622 bytes)

--- a/ckanext/datapreview/plugin.py	Sun Jul 24 11:45:30 2011 +0100
+++ b/ckanext/datapreview/plugin.py	Tue Aug 09 17:27:06 2011 +0100
@@ -1,4 +1,5 @@
-'''CKAN Extension Data Preview - preview package resources.
+"""
+CKAN Extension Data Preview - preview package resources.
 
 Data Preview is a CKAN extension - http://ckan.org/wiki/Extensions.
 
@@ -8,14 +9,18 @@
 
 Optional configuration options::
 
-    ## a webstore URL
-    ## Default webstore URL is http://127.0.0.1:8080/ofkn
-    ckanext.dataapi.webstore_url = http://...
+    # a webstore URL
+    # default: http://test-webstore.ckan.net
+    ckan.webstore_url = http://...
+
+    # the webstore user name used by the plugin
+    # default: okfn
+    ckan.webstore_user = okfn
     
 This extension provides preview functionality (using javascript) in relation to
 Packge Resources (i.e. urls pointing to datasets).
-'''
-__version__ = '0.1'
+"""
+__version__ = '0.2'
 
 import os
 from logging import getLogger
@@ -27,8 +32,25 @@
 
 log = getLogger(__name__)
 
-default_webstore_url = 'http://127.0.0.1:8080/okfn'
+DEFAULT_WEBSTORE_URL = 'http://127.0.0.1:8080/okfn'
+DEFAULT_WEBSTORE_USER = 'okfn'
 
+BOTTOM_CODE = """
+<div id="ckanext-datapreview-dialog"></div>
+<script type="text/javascript" src="/ckanext/datapreview/data-preview.js"></script>
+<script type="text/javascript">
+  jQuery('document').ready(function($) {
+    var webstoreUrl = '%(webstore_url)s';
+    var dialogId = 'ckanext-datapreview-dialog';
+    CKANEXT.DATAPREVIEW.initialize(webstoreUrl, dialogId);
+  });
+</script>
+"""
+    
+HEAD_CODE = """
+<link rel="stylesheet" href="/ckanext/datapreview/data-preview.css" 
+      class="ckanext-datapreview-stylesheet" />
+"""
 
 class DataPreviewPlugin(SingletonPlugin):
     """Insert javascript fragments into package pages to allow users to preview
@@ -42,8 +64,14 @@
         plugin to read custom options.
         """
         self.webstore_url = config.get(
-            'ckanext.dataapi.webstore_url',
-            default_webstore_url
+            'ckan.webstore_url',
+            DEFAULT_WEBSTORE_URL
+        )
+        if not self.webstore_url[-1] == '/':
+            self.webstore_url += '/'
+        self.webstore_user = config.get(
+            'ckan.webstore_user',
+            DEFAULT_WEBSTORE_USER
         )
 
     def update_config(self, config):
@@ -53,21 +81,6 @@
         config['extra_public_paths'] = \
             ','.join([our_public_dir, config.get('extra_public_paths', '')])
 
-    BOTTOM_CODE = """
-<div id="ckanext-datapreview-dialog"></div>
-<script type="text/javascript" src="/ckanext/datapreview/data-preview.js"></script>
-<script type="text/javascript">
-  jQuery('document').ready(function($) {
-    var webstoreUrl = '%(webstore_url)s';
-    var dialogId = 'ckanext-datapreview-dialog';
-    CKANEXT.DATAPREVIEW.initialize(webstoreUrl, dialogId);
-  });
-</script>
-"""
-    
-    HEAD_CODE = '''
-<link rel="stylesheet" href="/ckanext/datapreview/data-preview.css" class="ckanext-datapreview-stylesheet" />
-'''
         
     def filter(self, stream):
         """Required to implement IGenshiStreamFilter; will apply some HTML
@@ -77,10 +90,12 @@
         
         if routes.get('controller') == 'package' and \
             routes.get('action') == 'read' and c.pkg.id:
-            data = {'webstore_url': self.webstore_url}
+            data = {
+                'webstore_url': self.webstore_url + self.webstore_user
+            }
             stream = stream | Transformer('body')\
-                .append(HTML(self.BOTTOM_CODE % data))
+                .append(HTML(BOTTOM_CODE % data))
             stream = stream | Transformer('head')\
-                .append(HTML(self.HEAD_CODE))
+                .append(HTML(HEAD_CODE))
         
         return stream


--- a/public/ckanext/datapreview/data-preview.js	Sun Jul 24 11:45:30 2011 +0100
+++ b/public/ckanext/datapreview/data-preview.js	Tue Aug 09 17:27:06 2011 +0100
@@ -374,19 +374,32 @@
   dp.showData = function(preview, data) {
     dp.setupFullscreenDialog(preview);
 
-    if(data.error) {
+    if (data.error) {
       return dp.showError(data.error);
     }
 
-    var columns = $.map(data.fields || [], function (column, i) {
+    if (data.length == 0) {
+      return dp.showError("No data received from the server");
+    }
+
+    // get column names
+    var keys = [];
+    for (var key in data[0]) {
+        if (key !== '__id__') {
+            keys.push(key);
+        }
+    }
+
+    var columns = $.map(keys || [], function (column, i) {
       return {id: 'header-' + i, name: column, field: 'column-' + i, sortable: true};
     });
 
-    var data = $.map(data.data || [], function (row, id) {
+    var data = $.map(data || [], function (row, id) {
       var cells = {id: id};
-      for (var i = 0, c = row.length; i < c; i++) {
-        var isNumeric = (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/).test(row[i]);
-        cells['column-' + i] = isNumeric ? parseFloat(row[i]) : row[i];
+      // for (var i = 0, c = row.length; i < c; i++) {
+      for (var i in keys) {
+        var isNumeric = (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/).test(row[keys[i]]);
+        cells['column-' + i] = isNumeric ? parseFloat(row[keys[i]]) : row[keys[i]];
       }
       return cells;
     });
@@ -550,7 +563,7 @@
       }
 
       // TODO: add ability to change the limit in this url
-      var _url = dp.webstore + "/" + _hash + "/resource.jsonp?_limit=30";
+      var _url = dp.webstore + "/" + _hash + "/resource.json?_limit=30";
 
       // The API enpoint for the current package.
       var _datasetUri = $('.api code:first a').attr('href');

Repository URL: https://bitbucket.org/okfn/ckanext-datapreview/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list