Skip to content

Commit

Permalink
Ajoute les actions nécessaires niveau utilisateur
Browse files Browse the repository at this point in the history
J'ai fait en sorte que tout soit ajaxifié.
  • Loading branch information
fdambrine authored and artragis committed Feb 23, 2018
1 parent 709ed87 commit c4f9f2d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ gulp.task('js', () =>
'assets/js/autocompletion.js',
'assets/js/close-alert-box.js',
'assets/js/compare-commits.js',
'assets/js/content-publication-readiness.js',
'assets/js/dropdown-menu.js',
'assets/js/editor.js',
'assets/js/featured-resource-preview.js',
Expand Down
21 changes: 21 additions & 0 deletions assets/js/content-publication-readiness.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function ($, undefined) {
$("li").on("click", ".readiness", function (e) {
var url = $(e.target).data("url");
var readiness = $(e.target).data("is-ready") === "true";
var csrf = $("input[name=csrfmiddlewaretoken]").val();

$.ajax(url, {method: "PUT", data: {
"ready_to_publish": readiness,
"container_slug": $(e.target).data("container-slug"),
"parent_container_slug": $(e.target).data("parent-container-slug") || ""
}, success: function () {
$(e.target).data("is-ready", readiness?"false":"true")
.children("span")
.removeClass("glyphicon-remove-sign")
.removeClass("glyphicon-ok-sign")
.addClass(readiness?"glyphicon-remove-sign":"glyphicon-ok-sign");
}, headers: {
"X-CSRFToken": csrf
}});
});
})($);
17 changes: 17 additions & 0 deletions templates/tutorialv2/view/container.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


{% block breadcrumb %}

{% if container.parent.parent %}
<li>
<a
Expand Down Expand Up @@ -96,6 +97,11 @@ <h1>


{% block content %}
{% if not container.ready_to_publish %}
<div class="alert-box ico-alerts">
{% trans "Cette partie n'est pas prête à la publication. En cas de validation, elle ne sera pas publiée." %}
</div>
{% endif %}
{% include "tutorialv2/includes/chapter_pager.part.html" with position="top" %}

{% if container.introduction and container.get_introduction %}
Expand Down Expand Up @@ -237,6 +243,17 @@ <h1>
</form>
</li>
{% endif %}
{% if container.ready_to_publish %}
<li>
<a data-url="{% url 'api:content:readiness' content.pk %}" class="readiness" data-is-ready="true" data-container-slug="{{ container.slug }}"
{% if container.parent.parent %}data-parent-container-slug="{{ container.parent.slug }}"{% endif %}>
<span class="glyphicon-remove-sign"></span>{% trans "Marquer comme à ne pas valider." %}
</a></li>
{% else %}
<li><a data-url="{% url 'api:content:readiness' content.pk %}" class="readiness" data-is-ready="false"
{% if container.parent.parent %}data-parent-container-slug="{{ container.parent.slug }}"{% endif %}>
<span class="glyphicon-ok-sign"></span>{% trans "Marquer comme prêt à valider." %}</a></li>
{% endif %}
{% endif %}
{% endblock %}

Expand Down
13 changes: 13 additions & 0 deletions zds/member/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ def has_object_permission(self, request, view, obj):
return author == request.user


class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
# Write permissions are not allowed to the owner of the snippet
if hasattr(obj, 'user'):
owners = [obj.user]
elif hasattr(obj, 'author'):
owners = [obj.author]
elif hasattr(obj, 'authors'):
owners = list(obj.authors)

return request.user in owners


class IsNotOwnerOrReadOnly(permissions.BasePermission):
"""
Custom permission to prevent owner to vote on their objects
Expand Down
3 changes: 2 additions & 1 deletion zds/tutorialv2/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.conf.urls import url

from zds.tutorialv2.api.views import ContentReactionKarmaView
from zds.tutorialv2.api.views import ContentReactionKarmaView, ContainerPublicationReadinessView

urlpatterns = [
url(r'^reactions/(?P<pk>\d+)/karma/?$', ContentReactionKarmaView.as_view(), name='reaction-karma'),
url(r'^publication/preparation/(?P<pk>[0-9]+)/', ContainerPublicationReadinessView.as_view(), name='readiness')
]
52 changes: 50 additions & 2 deletions zds/tutorialv2/api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
from django.http import Http404
from django.utils.translation import gettext as _
from rest_framework.fields import empty
from rest_framework.generics import UpdateAPIView
from rest_framework.serializers import Serializer, CharField, BooleanField
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from zds.member.api.permissions import CanReadAndWriteNowOrReadOnly, IsNotOwnerOrReadOnly
from zds.member.api.permissions import CanReadAndWriteNowOrReadOnly, IsNotOwnerOrReadOnly, IsStaffUser, IsOwner
from zds.tutorialv2.utils import search_container_or_404
from zds.utils.api.views import KarmaView
from zds.tutorialv2.models.database import ContentReaction
from zds.tutorialv2.models.database import ContentReaction, PublishableContent


class ContainerReadinessSerializer(Serializer):
parent_container_slug = CharField()
container_slug = CharField(required=True)
ready_to_publish = BooleanField(required=True)

def run_validation(self, data=empty):
init = super().run_validation(data)
if not init:
return init
if not data.get('parent_container_slug', ''):
init.pop('parent_container_slug', '')
return init

def save(self, **kwargs):
if not self.validated_data:
self.is_valid(True)
versioned = self.instance.load_version()
container = search_container_or_404(versioned, self.validated_data)
container.ready_to_publish = self.validated_data['ready_to_publish']
sha = versioned.repo_update(versioned.title, versioned.get_introduction(), versioned.get_conclusion(),
commit_message=_('{} est {} à la publication.').format(
container.get_path(True),
_('prêt') if container.ready_to_publish else _('ignoré')))
PublishableContent.objects.filter(pk=self.instance.pk).update(sha_draft=sha)

def to_representation(self, instance):
return {}


class ContentReactionKarmaView(KarmaView):
queryset = ContentReaction.objects.all()
permission_classes = (IsAuthenticatedOrReadOnly, CanReadAndWriteNowOrReadOnly, IsNotOwnerOrReadOnly)


class ContainerPublicationReadinessView(UpdateAPIView):
permission_classes = (IsStaffUser, IsOwner)
serializer_class = ContainerReadinessSerializer

def get_object(self):
content = PublishableContent.objects.prefetch_related('authors')\
.filter(pk=int(self.kwargs['pk']))\
.first()
if not content:
raise Http404()
return content
2 changes: 1 addition & 1 deletion zds/tutorialv2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def fill_containers_from_json(json_sub, parent):
parent.add_container(new_container, generate_slug=(slug == ''))
except InvalidOperationError as e:
raise BadManifestError(e.message)
new_container.ready_to_publish = json_sub.get('ready_to_publish', True)
new_container.ready_to_publish = child.get('ready_to_publish', True)
if 'children' in child:
fill_containers_from_json(child, new_container)
elif child['object'] == 'extract':
Expand Down

0 comments on commit c4f9f2d

Please sign in to comment.