Commit 5705a39a by alexandre

Simplified permissions

parent 37da53a8
......@@ -32,6 +32,7 @@ class PermissionListSerializer(serializers.ListSerializer):
return [{"username": k.username, "permissions": v} for k, v in perms.items()]
def to_internal_value(self, data):
print(data)
# return data
# I don't understand why I can't just return data
return {"permissions": data}
......@@ -58,17 +59,17 @@ class ScoreSerializer(TaggitSerializer, serializers.HyperlinkedModelSerializer):
def update(self, instance, validated_data):
instance = super(ScoreSerializer, self).update(instance, validated_data)
permissions = validated_data["permissions"]
for i in permissions:
username = i.get("username")
perms = i.get("permissions")
user = User.objects.get(username=username)
for perm in get_user_perms(user, instance):
for user, perms in get_users_with_perms(instance, attach_perms=True).items():
for perm in perms:
remove_perm(perm, user, instance)
for i in instance.permissions:
user = User.objects.get(username=i.get('username'))
perms = i.get('permissions')
for perm in perms:
assign_perm(perm, user, instance)
return instance
......
......@@ -1869,11 +1869,26 @@ window.W = window.W || {};
template: '#permission-item-template',
ui: {
'checkbox': 'input',
'remove': 'span',
},
triggers: {
'change @ui.checkbox': 'change:permissions',
'click @ui.remove': 'remove:permissions',
},
onRemovePermissions: function() {
var score = this.model.collection.score;
this.model.destroy();
// Backbone.Relational.store.unregister(this.model);
console.log(score);
// // var collection = this.model.collection;
// var that = this;
// this.model.destroy({
// success: function() {
// console.log(that.parent.collection);
// }
// });
score.save(null, {silent: true});
},
initialize: function () {
......@@ -1889,27 +1904,27 @@ window.W = window.W || {};
childView: W.PermissionItemView,
childViewEvents: {
'change:permissions': 'changePermissions',
},
// childViewEvents: {
// 'remove:permissions': 'removePermissions',
// },
changePermissions: function(event) {
var perms = [];
// changePermissions: function(event) {
// var perms = [];
this.$el.find("li").each(function() {
var data = {};
data.username = $(this).attr("data-username");
data.permissions = [];
// this.$el.find("li").each(function() {
// var data = {};
// data.username = $(this).attr("data-username");
// data.permissions = [];
$(this).find('[type="checkbox"]:checked').each(function(){
data.permissions.push($(this).val());
});
// $(this).find('[type="checkbox"]:checked').each(function(){
// data.permissions.push($(this).val());
// });
perms.push(data);
});
// perms.push(data);
// });
this.collection.score.save({permissions: perms}, {silent: true})
},
// this.collection.score.save({permissions: perms}, {silent: true})
// },
});
......@@ -1937,7 +1952,9 @@ window.W = window.W || {};
// Prevents adding non-existing or duplicate users
if (this.collection.where({username: username}).length == 0
&& this.users.where({username: username}).length != 0) {
this.collection.add({username: username, permissions: []})
this.collection.add({username: username, permissions: ["view_score", "change_score"]})
this.collection.score.save(null, {silent: true});
}
},
......
<%- username %><br>
<input type="checkbox" value="view_score" name="view_score"
<% if (_.indexOf(permissions, "view_score") != -1) { %> checked <% } %>
/>
<label>Peut voir</label>
<input type="checkbox" value="change_score" name="change_score"
<% if (_.indexOf(permissions, "change_score") != -1) { %> checked <% } %>
/>
<label>Peut éditer</label>
<span>ne plus partager</span>
<br>
<br>
<%- username %> <span>[x]</span>
<p>Partager avec</p>
<input class="search-username" list="users">
<input class="submit-username" type="submit" value="ok">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment