Skip to content

Commit

Permalink
[view/resource-upload][m]: very basic (but working and tested) upload…
Browse files Browse the repository at this point in the history
… form (refs #29).
  • Loading branch information
rgrp committed Aug 8, 2011
1 parent 0791071 commit 0c1ebfc
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
30 changes: 30 additions & 0 deletions lib/client.js
Expand Up @@ -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) {
}
});

Expand Down
25 changes: 25 additions & 0 deletions lib/template/resource-upload.js
@@ -0,0 +1,25 @@
CKAN.Templates.resourceUpload = ' \
<form action="" \
enctype="multipart/form-data" \
method="POST"> \
\
<div class="hidden-inputs"></div> \
<dl> \
<!--dt> \
<label>File name:</label> \
</dt> \
<dd> \
<input type="text" name="key" value="" /> \
</dd--> \
<dt> \
<label>File:</label> \
</dt> \
<dd> \
<input id="file" name="file" type="file" size="50" /> \
</dd> \
</dl> \
<div class="submit"> \
<input type="submit" value="Upload &raquo;" /> \
</div> \
</form> \
';
41 changes: 41 additions & 0 deletions 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 = '<input type="hidden" name="${name}" value="${value}" />';
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);
29 changes: 28 additions & 1 deletion test/fixtures.js
Expand Up @@ -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"
}
]
}
]
};

5 changes: 5 additions & 0 deletions test/index.html
Expand Up @@ -68,10 +68,15 @@ <h3>Results</h3>
<script src="../lib/template/dataset-form.js"></script>
<script src="../lib/template/resource-form.js"></script>
<script src="../lib/template/resource-view.js"></script>

<script src="../lib/client.js"></script>
<script src="../lib/model.js"></script>
<script src="../lib/view.js"></script>
<script src="../lib/view/dataset-listing.js"></script>

<script src="../lib/template/resource-upload.js"></script>
<script src="../lib/view/resource-upload.js"></script>

<script src="../lib/ui.js"></script>
<script src="../widgets/search/js/search.js"></script>

Expand Down
22 changes: 22 additions & 0 deletions test/view-test.js
Expand Up @@ -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);
});
});

0 comments on commit 0c1ebfc

Please sign in to comment.