Skip to content

Commit

Permalink
Enabled sharing for data sets. Still need to implement an auto refres…
Browse files Browse the repository at this point in the history
…h of the list of data sets.
  • Loading branch information
flekschas committed Jun 29, 2015
1 parent ca67ae2 commit ac40d8b
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 56 deletions.
4 changes: 2 additions & 2 deletions refinery/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def res_sharing(self, request, **kwargs):
if request.method == 'GET':
kwargs['sharing'] = True
return self.process_get(request, res, **kwargs)
elif request.method == 'PATCH':
data = json.loads(request.raw_post_data)
elif request.method == 'PUT':
data = json.loads(request.body)
new_share_list = data['share_list']

groups_shared_with = map(
Expand Down
8 changes: 8 additions & 0 deletions refinery/static/source/styles/less/refinery-style.less
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,14 @@ button.icon-only {
}
}

.float-left {
float: left;
}

.float-right {
float: right;
}

.no-list-style {
margin: 0;
padding: 0;
Expand Down
26 changes: 26 additions & 0 deletions refinery/ui/src/js/commons/services/sharing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
angular
.module('refineryApp')
.factory('sharingService', ['$resource', 'settings',
function ($resource, settings) {

var sharing = $resource(
settings.appRoot + settings.refineryApi + '/:model/:uuid/sharing',
{
model: '@model',
uuid: '@uuid',
format: 'json'
},
{
query: {
method: 'GET',
isArray: false,
},
update: {
method: 'PUT'
}
}
);

return sharing;
}
]);
6 changes: 3 additions & 3 deletions refinery/ui/src/js/dashboard/controllers/LaunchPad.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function LaunchPadCtrl ($modal) {
var that = this;

that.openPermissionEditor = function (api, uuid) {
that.openPermissionEditor = function (model, uuid) {
var modalInstance = $modal.open({
templateUrl: '/static/partials/dashboard/partials/permission.modal.html',
controller: 'PermissionEditorCtrl as modal',
resolve: {
params: function () {
config: function () {
return {
api: api,
model: model,
uuid: uuid
};
}
Expand Down
152 changes: 126 additions & 26 deletions refinery/ui/src/js/dashboard/controllers/PermissionEditor.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,141 @@
function PermissionEditorCtrl ($scope, $http, $modalInstance, params) {
var ctrl = this;
function PermissionEditorCtrl ($http, $modalInstance, _, sharingService, config) {
var that = this;

// loadResource(config.api, config.uuid);
this._ = _;
this.config = config;
this.$modalInstance = $modalInstance;
this.sharingService = sharingService;

console.log($modalInstance);
this.getPermissions(
this.config.model,
this.config.uuid
).then(function (data) {
that.permissions = data;
}).catch(function (error) {
console.error(error);
});

ctrl.save = function () {
var pTable = document.getElementById('permission-table');
var cells = pTable.getElementsByTagName('td');
// Data is clustered into sets of 4 -- i is name, i+1 is noperm, i+2 is readonly, i+3 is edit.
for (var i = 0; i < cells.length; i += 4) {
// Add the group.
var name = cells[i].innerText;
var id = cells[i].children[0].innerText;
this.permissionLevel = {
none: {
read: false,
change: false
},
read: {
read: true,
change: false
},
edit: {
read: true,
change: true
},
};
}

var canRead = cells[i+2].children[0].checked || cells[i+3].children[0].checked;
var canChange = cells[i+3].children[0].checked;
/**
* Cancel permission editing.
* @type {function}
*/
PermissionEditorCtrl.prototype.cancel = function () {
this.$modalInstance.dismiss('cancel');
};

var data = '{"read": ' + canRead + ', "change": ' + canChange + '}';
/**
* [getPermissions description]
* @type {function}
* @param {string} model Model which permissions are to be edited.
* @param {string} uuid UUID of the exact model entity.
* @return {object} Angular promise.
*/
PermissionEditorCtrl.prototype.getPermissions = function (model, uuid) {
var that = this,
permissions;

// need to somehow store the group id in the cells as a hidden thing
$http({method: 'PATCH', url: 'api/v1/' + config.api + '/' + config.uuid + '_' + id + '/', data: data});
}
$modalInstance.dismiss('saved');
};
permissions = this.sharingService.get({
model: model,
uuid: uuid
});

ctrl.cancel = function () {
$modalInstance.dismiss('cancel');
};
}
return permissions
.$promise
.then(function (data) {
groups = [];
for (var i = 0, len = data.share_list.length; i < len; i++) {
groups.push({
id: data.share_list[i].group_id,
name: data.share_list[i].group_name,
permission: that.getPermissionLevel(data.share_list[i].perms)
});
}
return {
isOwner: data.is_owner,
groups: groups
};
})
.catch(function (error) {
return error;
});
};

/**
* Turns permission object into a simple string.
* @type {function}
* @param {object} perms Object of the precise permissions.
* @return {string} Permission's name.
*/
PermissionEditorCtrl.prototype.getPermissionLevel = function (perms) {
if (perms.read === false) {
return 'none';
}
if (perms.change === true) {
return 'edit';
}
return 'read';
};

/**
* Save permissions
* @type {function}
*/
PermissionEditorCtrl.prototype.save = function () {
var that = this,
accessList = [];

this.isSaving = true;

for (var i = 0, len = this.permissions.groups.length; i < len; i++) {
accessList.push(this._.assign({
id: this.permissions.groups[i].id
}, this.permissionLevel[this.permissions.groups[i].permission]));
}

this
.sharingService
.update({
model: this.config.model,
uuid: this.config.uuid
}, {
'share_list': accessList
})
.$promise
.then(function () {
console.log('Sweet permissions are saved');
that.$modalInstance.dismiss('saved');
})
.catch(function (error) {
console.error(error);
})
.finally(function () {
that.isSaving = false;
});
};

angular
.module('refineryDashboard')
.controller('PermissionEditorCtrl', [
'$scope',
'$http',
'$modalInstance',
'params',
'_',
'sharingService',
'config',
PermissionEditorCtrl
]);
16 changes: 0 additions & 16 deletions refinery/ui/src/js/dashboard/directives/permissionTable.js

This file was deleted.

Empty file.
45 changes: 37 additions & 8 deletions refinery/ui/src/js/dashboard/partials/permission.modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,48 @@ <h3>Permission Editor</h3>
<h4 id="modal-description"></h4>
</div>
<div class="modal-body">
<!--
<table class="table" id="permission-table">
<table class="table table-hover" id="permission-table">
<thead>
<tr>
<th>Group</th>
<th>None</th>
<th>Readonly</th>
<th>Read-only</th>
<th>Edit</th>
</tr>
</table>
-->
<p><em><h2>Coming back soon.</h2></em></p>
</thead>
<tbody>
<tr ng-repeat="group in modal.permissions.groups track by $index">
<td>
{{ group.name }}
</td>
<td>
<input
type="radio"
value="none"
ng-model="group.permission">
</td>
<td>
<input
type="radio"
value="read"
ng-model="group.permission">
</td>
<td>
<input
type="radio"
value="edit"
ng-model="group.permission">
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button class="btn" ng-click="modal.cancel()">Cancel</button>
<!-- <button class="btn btn-primary" ng-click="modal.save()">Save</button> -->
<div class="float-left">
<div class="refinery-spinner" ng-if="modal.isSaving"></div>
</div>
<div class="float-right">
<button class="btn" ng-click="modal.cancel()">Cancel</button>
<button class="btn btn-primary" ng-click="modal.save()">Save</button>
</div>
</div>
2 changes: 1 addition & 1 deletion refinery/ui/src/js/dashboard/views/launchPad.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h4>Name</h4>
<ul class="controls no-list-style">
<li>
<button
ng-click="launchPad.openPermissionEditor('dataset_sharing', dataSet.uuid)">
ng-click="launchPad.openPermissionEditor('data_sets', dataSet.uuid)">
<i class="icon-edit"></i>
</button>
</li>
Expand Down

0 comments on commit ac40d8b

Please sign in to comment.