Skip to content

Commit

Permalink
Permet la gestion de contributeurs sur les billets (#6551)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D authored Apr 28, 2024
1 parent 19f13c8 commit d3c7fee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 51 deletions.
4 changes: 2 additions & 2 deletions templates/tutorialv2/messages/add_contribution_pm.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% load i18n %}

{% blocktrans with title=content.title|safe type=type|safe user=user|safe %}
{% blocktrans with title=content.title|safe user=user|safe %}
Bonjour {{ user }},

Vous avez été ajouté à la liste des contributeurs {{type}} « {{ title }} », en tant que {{role}}.
Vous avez été ajouté à la liste des contributeurs de la publication « {{ title }} », en tant que {{ role }}.

Merci pour votre participation !
{% endblocktrans %}
27 changes: 8 additions & 19 deletions zds/tutorialv2/tests/tests_views/tests_addcontributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


from zds.member.tests.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.models import CONTENT_TYPE_LIST
from zds.tutorialv2.tests.factories import ContentContributionRoleFactory, PublishableContentFactory
from zds.tutorialv2.views.contributors import ContributionForm
from zds.tutorialv2.models.database import ContentContribution
Expand Down Expand Up @@ -61,26 +62,14 @@ def test_authenticated_author(self):
response = self.client.post(self.form_url, self.form_data)
self.assertRedirects(response, self.content_url)

def test_authenticated_staff_tutorial(self):
def test_authenticated_staff(self):
self.client.force_login(self.staff)
self.content.type = "TUTORIAL"
self.content.save()
response = self.client.post(self.form_url, self.form_data)
self.assertRedirects(response, self.content_url)

def test_authenticated_staff_article(self):
self.client.force_login(self.staff)
self.content.type = "ARTICLE"
self.content.save()
response = self.client.post(self.form_url, self.form_data)
self.assertRedirects(response, self.content_url)

def test_authenticated_staff_opinion(self):
self.client.force_login(self.staff)
self.content.type = "OPINION"
self.content.save()
response = self.client.post(self.form_url, self.form_data)
self.assertEqual(response.status_code, 403)
for type in CONTENT_TYPE_LIST:
with self.subTest(type):
self.content.type = type
self.content.save()
response = self.client.post(self.form_url, self.form_data)
self.assertRedirects(response, self.content_url)


class AddContributorWorkflowTests(TutorialTestMixin, TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def test_authenticated_staff_opinion(self):
self.content.type = "OPINION"
self.content.save()
response = self.client.post(self.form_url, self.form_data)
self.assertEqual(response.status_code, 403)
self.assertRedirects(response, self.content_url)


class RemoveContributorWorkflowTests(TutorialTestMixin, TestCase):
Expand Down
41 changes: 13 additions & 28 deletions zds/tutorialv2/views/contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
from crispy_forms.bootstrap import StrictButton
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field
from django import forms

from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.http import Http404
from django.shortcuts import redirect, get_object_or_404
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _

from zds.member.decorator import LoggedWithReadWriteHability
Expand Down Expand Up @@ -80,13 +78,6 @@ def clean_username(self):
return cleaned_data


class RemoveContributionForm(forms.Form):
pk_contribution = forms.CharField(
label=_("Contributeur"),
required=True,
)


class AddContributorToContent(LoggedWithReadWriteHability, SingleContentFormViewMixin):
must_be_author = True
form_class = ContributionForm
Expand All @@ -103,18 +94,11 @@ def get(self, request, *args, **kwargs):
return redirect(url, self.request.user)

def form_valid(self, form):
_type = _("à l'article")

if self.object.is_tutorial:
_type = _("au tutoriel")
elif self.object.is_opinion:
raise PermissionDenied

bot = get_bot_account()
all_authors_pk = [author.pk for author in self.object.authors.all()]
user = form.cleaned_data["user"]
if user.pk in all_authors_pk:
messages.error(self.request, _("Un auteur ne peut pas être désigné comme contributeur"))
messages.error(self.request, _("Un auteur ne peut pas être désigné comme contributeur."))
return redirect(self.object.get_absolute_url())
else:
contribution_role = form.cleaned_data.get("contribution_role")
Expand All @@ -126,7 +110,7 @@ def form_valid(self, form):
self.request,
_(
"Ce membre fait déjà partie des "
'contributeurs {} avec pour rôle "{}"'.format(_type, contribution_role.title)
'contributeurs à la publication avec pour rôle "{}"'.format(contribution_role.title)
),
)
return redirect(self.object.get_absolute_url())
Expand All @@ -139,13 +123,12 @@ def form_valid(self, form):
send_mp(
bot,
[user],
format_lazy("{} {}", _("Contribution"), _type),
_("Contribution à la publication"),
self.versioned_object.title,
render_to_string(
"tutorialv2/messages/add_contribution_pm.md",
{
"content": self.object,
"type": _type,
"url": self.object.get_absolute_url(),
"index": url_index,
"user": user.username,
Expand All @@ -168,26 +151,28 @@ def form_invalid(self, form):
return super().form_valid(form)


class RemoveContributionForm(forms.Form):
pk_contribution = forms.CharField(
label=_("Contributeur"),
required=True,
)


class RemoveContributorFromContent(LoggedWithReadWriteHability, SingleContentFormViewMixin):
form_class = RemoveContributionForm
must_be_author = True
authorized_for_staff = True

def form_valid(self, form):
_type = _("cet article")
if self.object.is_tutorial:
_type = _("ce tutoriel")
elif self.object.is_opinion:
raise PermissionDenied

contribution = get_object_or_404(ContentContribution, pk=form.cleaned_data["pk_contribution"])
user = contribution.user
contribution.delete()
signals.contributors_management.send(
sender=self.__class__, content=self.object, performer=self.request.user, contributor=user, action="remove"
)
messages.success(
self.request, _("Vous avez enlevé {} de la liste des contributeurs de {}.").format(user.username, _type)
self.request,
_("Vous avez enlevé {} de la liste des contributeurs de cette publication.").format(user.username),
)
self.success_url = self.object.get_absolute_url()

Expand Down
2 changes: 1 addition & 1 deletion zds/tutorialv2/views/display/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def show_categories_management(self) -> bool:
return self.enabled and self.is_allowed

def show_contributors_management(self) -> bool:
return self.enabled and self.is_allowed and self.requires_validation
return self.enabled and self.is_allowed

def show_ready_to_publish(self) -> bool:
return self.enabled and self.is_allowed and self.requires_validation
Expand Down

0 comments on commit d3c7fee

Please sign in to comment.