Commit d414b87f authored by alexandre's avatar alexandre

Cleaner way to handle score read-only perms

parent 89d75164
......@@ -52,11 +52,11 @@ class PermissionSerializer(serializers.Serializer):
list_serializer_class = PermissionListSerializer
class ScoreSerializer(TaggitSerializer, serializers.HyperlinkedModelSerializer):
id = serializers.ReadOnlyField()
tags = TagListSerializerField()
permissions = PermissionSerializer(source="*", many=True)
is_editable = serializers.SerializerMethodField()
class Meta:
model = Score
......@@ -68,6 +68,9 @@ class ScoreSerializer(TaggitSerializer, serializers.HyperlinkedModelSerializer):
assign_perm("view_score", self.context['request'].user, instance)
return instance
def get_is_editable(self, obj):
return self.context['request'].user.has_perm('edit_score', obj)
def update(self, instance, validated_data):
instance = super(ScoreSerializer, self).update(instance, validated_data)
......
......@@ -446,6 +446,10 @@ section.hidden { display: none; }
top: calc(30px + 1.25em);
}
.read-only .score-edit {
display: none;
}
#sidebar {
position: sticky;
top: 0;
......@@ -502,6 +506,8 @@ section.hidden { display: none; }
display: inline-block;
}
.read-only .score-actions { display: none; }
/* .score-actions { */
/* position: absolute; */
/* top: 1.25em; */
......@@ -1245,6 +1251,7 @@ li + li .alt-symbol { display: none; }
}
.axis-row:hover .axis-row--actions { display: inline-block; }
.read-only .axis-row:hover .axis-row--actions { display: none; }
.axis-actions [name="toggle"] {
position: absolute;
......
......@@ -7,21 +7,6 @@ window.W = window.W || {};
'use strict';
W.ScoreApp = Marionette.Application.extend({
channelName: 'user',
radioRequests: {
'user:loggedIn': 'isLoggedIn',
'user:username': 'username'
},
username: function() {
return this.model.get("username");
},
isLoggedIn: function() {
return this.model.get("pk");
},
region: 'body',
triggers: {
......@@ -29,6 +14,8 @@ window.W = window.W || {};
},
onStart: function(options) {
var channel = this.getChannel();
this.model = new W.UserAuthModel();
var baseView = new W.BaseView({model: this.model});
this.showView(baseView);
......
......@@ -205,25 +205,6 @@ window.W = window.W || {};
// console.log(data);
return data;
},
hasPerm: function(perm) {
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:loggedIn');
var username = channel.request('user:username');
var permissions = this.get("permissions");
var userPerm = permissions.findWhere({username: username}) || permissions.findWhere({username: "AnonymousUser"});
var hasPerm = false;
if (userPerm) {
hasPerm = _.contains(userPerm.get("permissions"), perm);
} else {
hasPerm = false;
}
return hasPerm;
}
});
......@@ -297,6 +278,16 @@ window.W = window.W || {};
return 1;
},
getEditable: function () {
var parent = this.get('parent');
if (parent && parent.getEditable) {
return parent.getEditable();
}
return this.get("score").get("is_editable");
},
getDepth: function () {
return _.reduce(this.get('sublines').models, function (depth, line) { return Math.max(line.getDepth(), depth); }, 0) + 1;
},
......
......@@ -304,7 +304,7 @@ window.W = window.W || {};
},
onHide: function () {
console.log("hiding");
// console.log("hiding");
this.$el.addClass('is-collapsed');
},
......@@ -1356,9 +1356,9 @@ window.W = window.W || {};
e.preventDefault();
if (e.shiftKey) {
this.showInlineForm();
this.model.getEditable() && this.showInlineForm();
} else if (e.ctrlKey || e.metaKey || e.altKey) {
this.toggleForm();
this.model.getEditable() && this.toggleForm();
} else {
this.toggle();
}
......@@ -1718,10 +1718,12 @@ window.W = window.W || {};
W.ScoreDetailView = Marionette.View.extend({
template: '#score-template-vue',
attributes: {
id: 'score',
class: "content",
tabindex: '1'
attributes: function () {
return {
id: 'score',
class: this.model.get('is_editable') ? "content" : "content read-only",
tabindex: '1'
};
},
regions: {
......@@ -1776,7 +1778,6 @@ window.W = window.W || {};
rangeType: rangeType,
formatTimestamp: formatTimestamp,
metaExpanded: this.metaExpanded,
canChangeScore: this.model.hasPerm('change_score')
};
},
......@@ -2619,16 +2620,6 @@ window.W = window.W || {};
W.HomeView = Marionette.View.extend({
template: '#home-template',
templateContext: function () {
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:isLoggedIn');
return {
isLoggedIn: isLoggedIn
};
},
regions: {
latest_updated: '#latest_updated',
user_scores: '#user_scores'
......@@ -2666,36 +2657,29 @@ window.W = window.W || {};
}
});
var channel = Backbone.Radio.channel('user');
var isLoggedIn = channel.request('user:loggedIn');
console.log("****************");
console.log(isLoggedIn);
if (isLoggedIn) {
var scoreCollection2 = new W.ScoreCollection([], {
// All the `state` and `queryParams` key value pairs are merged with
// the defaults too.
state: {
pageSize: 5,
firstPage: 1,
currentPage: 1,
sortKey: "updated_at",
order: -1
}
});
var scoreCollection2 = new W.ScoreCollection([], {
// All the `state` and `queryParams` key value pairs are merged with
// the defaults too.
state: {
pageSize: 5,
firstPage: 1,
currentPage: 1,
sortKey: "updated_at",
order: -1
}
});
scoreCollection2.queryParams.shared_with=true;
scoreCollection2.queryParams.shared_with=true;
var myListView2 = new W.ScoreListFooView({
collection: scoreCollection2
});
var myListView2 = new W.ScoreListFooView({
collection: scoreCollection2
});
myListView2.collection.fetch({
success: function () {
that.showChildView('user_scores', myListView2);
}
});
};
myListView2.collection.fetch({
success: function () {
that.showChildView('user_scores', myListView2);
}
});
}
});
......
......@@ -15,12 +15,10 @@
<br>
<div id="latest_updated"></div>
<% if (isLoggedIn) { %>
<br>
<br>
<h1><%- t('Mes partitions') %></h1>
<br>
<div id="user_scores"></div>
<% } %>
</section>
<article>
<article class="<% if (!is_editable) { %>read-only<% } %>">
<header id="sidebar">
<h1 id="title"><%- title %></h1>
<div class="score-actions">
<% if (canChangeScore) { %>
<button class="save" data-label-progress="<%- t('Enregistrer…') %>" data-label="<%- t('Enregistrer') %>" <% if (!hasChanges) { %>disabled<% } %>><%- t('Enregistrer') %></button>
<button class="duplicate"><%- t('Dupliquer') %></button>
<button class="delete"><%- t('Supprimer') %></button>
<!--<button data-name="export"><%- t('Export') %></button>-->
<% } %>
</div>
</header>
......@@ -24,9 +22,8 @@
<aside class="panel__alt panel--score-meta <% if (!metaExpanded) { %>is-collapsed<% } %>">
<span class="panel__toggle__alt js-toggle"><%- t('infos') %></span>
<section class="content">
<% if (canChangeScore) { %>
<button class="edit score-edit"><%- t('Éditer') %></button>
<% } %>
<dl class="score-meta">
<% if (presentation) { %>
<dt class="score-meta__term"><%- t('Présentation') %></dt>
......
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