diff --git a/lib/client.js b/lib/client.js index 502bf0d..d2ac7a3 100644 --- a/lib/client.js +++ b/lib/client.js @@ -170,6 +170,36 @@ this.CKAN.Client = (function (CKAN, $, _, Backbone, undefined) { }, this); return new CKAN.Model.SearchCollection(models, {total: json.count}); + }, + + // Performs a query on CKAN API. + // The `options` argument can contain any keys supported by jQuery.ajax(). + // In addition it should contain either a url or offset variable. If + // offset provided it will be used to construct the full api url by + // prepending the endpoint plus 'api' (i.e. offset of '/2/rest/package' + // will become '{endpoint}/api/2/rest'. + // + // The `options.success` method (and any other success callbacks) will + // recieve a `SearchCollection` containing `Dataset` models. The method + // returns a jqXHR object so that additional callbacks can be registered + // with .success() and .error(). + apiCall: function (options) { + options = options || {}; + // Add additional request options. + options = _.extend({}, { + url: this.environment('endpoint') + '/api' + options.offset, + headers: { + 'X-CKAN-API-KEY': this.environment('apiKey') + } + }, options); + + return $.ajax(options); + }, + + // wrap CKAN /api/storage/auth/form - see http://packages.python.org/ckanext-storage + // params and returns value are as for that API + // key is file label/path + getStorageAuthForm: function(key, options) { } }); diff --git a/lib/template/resource-upload.js b/lib/template/resource-upload.js new file mode 100644 index 0000000..0d666b6 --- /dev/null +++ b/lib/template/resource-upload.js @@ -0,0 +1,25 @@ +CKAN.Templates.resourceUpload = ' \ +
\ + \ +
\ +
\ + \ +
\ + \ +
\ +
\ + \ +
\ +
\ +
\ + \ +
\ +
\ +'; diff --git a/lib/view/resource-upload.js b/lib/view/resource-upload.js new file mode 100644 index 0000000..424e398 --- /dev/null +++ b/lib/view/resource-upload.js @@ -0,0 +1,41 @@ +this.CKAN || (this.CKAN = {}); +this.CKAN.View || (this.CKAN.View = {}); + +(function (CKAN, $, _, Backbone, undefined) { + CKAN.View.ResourceUpload = Backbone.View.extend({ + tagName: 'div', + + // expects a client arguments in its options + initialize: function(options) { + this.el = $(this.el); + this.client = options.client; + _.bindAll(this, 'update', 'render'); + }, + + render: function () { + this.el.empty(); + tmplData = { + } + var tmpl = $.tmpl(CKAN.Templates.resourceUpload, tmplData); + this.el.html(tmpl); + return this; + }, + + // update with data from backend storage + update: function() { + var self = this; + var key = 'xyz/abc'; + this.client.getStorageAuthForm(key, { + success: function(data) { + _tmpl = ''; + self.el.find('form').attr('action', data.action); + var $hidden = $(self.el.find('form div.hidden-inputs')[0]); + $.each(data.fields, function(idx, item) { + $hidden.append($.tmpl(_tmpl, item)); + }); + } + }); + } + }); + +})(CKAN, $, _, Backbone, undefined); diff --git a/test/fixtures.js b/test/fixtures.js index b671e2d..81a95bd 100644 --- a/test/fixtures.js +++ b/test/fixtures.js @@ -101,4 +101,31 @@ var datasets = [ } ] } -] +]; + +var FIXTURES = { + 'apiStorageAuthForm': [ + { + "action": "http://ckantest.commondatastorage.googleapis.com/", + "fields": [ + { + "name": "policy", + "value": "eyJleHBpcmF0aW9uIjogIjIwMTEtMDgtMDhUMTA6MTA6MTRaIiwKImNvbmRpdGlvbnMiOiBbeyJidWNrZXQiOiAiY2thbnRlc3QifSx7ImtleSI6ICJ4eXovYWJjIn1dfQ==" + }, + { + "name": "AWSAccessKeyId", + "value": "GOOGC6OU3AYPNY47B66M" + }, + { + "name": "signature", + "value": "3lqm6s3tsxlBhTeYfY7kYBGNB+Q=" + }, + { + "name": "key", + "value": "xyz/abc" + } + ] + } + ] +}; + diff --git a/test/index.html b/test/index.html index 26b51ff..03acf78 100644 --- a/test/index.html +++ b/test/index.html @@ -68,10 +68,15 @@

Results

+ + + + + diff --git a/test/view-test.js b/test/view-test.js index 5a7e3f2..75dc648 100644 --- a/test/view-test.js +++ b/test/view-test.js @@ -100,3 +100,25 @@ test("ResourceEditView", function() { equal(url, res.get('url')); }); +test("ResourceUpload", function() { + var client = new CKAN.Client(); + sinon.stub(client, 'getStorageAuthForm', function(key, options) { + options.success(FIXTURES.apiStorageAuthForm[0]); + }); + + var view = new CKAN.View.ResourceUpload({ + client: client + }); + var $el = view.render().el; + ok($el); + $('.fixture').append($el); + + view.update() + equals($el.find('form').attr('action'), 'http://ckantest.commondatastorage.googleapis.com/'); + var expectedFields = ['signature', 'policy']; + $.each(expectedFields, function(idx, fieldName) { + var _found = $el.find('input[name="' + fieldName + '"]'); + equals(_found.length, 1, 'Failed to find input ' + fieldName); + }); +}); +