-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute une modale d'édition pour les objectifs
- Loading branch information
Showing
9 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase | ||
from django.urls import reverse | ||
|
||
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory | ||
from zds.tutorialv2.tests.factories import PublishableContentFactory, GoalFactory | ||
|
||
|
||
class EditGoalsPermissionTests(TestCase): | ||
def setUp(self): | ||
self.user = ProfileFactory().user | ||
self.staff = StaffProfileFactory().user | ||
self.content = PublishableContentFactory() | ||
self.good_url = reverse("content:edit-goals", kwargs={"pk": self.content.pk}) | ||
self.bad_url = reverse("content:edit-goals", kwargs={"pk": 42}) | ||
self.success_url = reverse("content:view", kwargs={"pk": self.content.pk, "slug": self.content.slug}) | ||
|
||
def test_unauthenticated_not_existing_pk(self): | ||
"""Invalid pks in URL""" | ||
self.client.logout() | ||
response = self.client.post(self.bad_url) | ||
self.assertEqual(response.status_code, 404) | ||
|
||
def test_unauthenticated_redirected(self): | ||
"""As login is required, unauthenticated users shall be redirected to the login page.""" | ||
self.client.logout() | ||
response = self.client.post(self.good_url) | ||
self.login_url = f"{reverse('member-login')}?next={self.good_url}" | ||
self.assertRedirects(response, self.login_url) | ||
|
||
def test_simple_user_forbidden(self): | ||
"""Simple users shall not be able to access to the view.""" | ||
self.client.force_login(self.user) | ||
response = self.client.post(self.good_url) | ||
self.assertEqual(response.status_code, 403) | ||
|
||
def test_staff_authorized(self): | ||
"""Staff shall have access to the view.""" | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.good_url) | ||
self.assertRedirects(response, self.success_url) | ||
|
||
|
||
class EditGoalsFunctionalTests(TestCase): | ||
def setUp(self): | ||
self.staff = StaffProfileFactory().user | ||
self.content = PublishableContentFactory() | ||
self.content = PublishableContentFactory() | ||
self.url = reverse("content:edit-goals", kwargs={"pk": self.content.pk}) | ||
self.goals = [GoalFactory() for _ in range(3)] | ||
|
||
@patch("zds.tutorialv2.signals.goals_management") | ||
def test_goals_updated(self, goals_management): | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.url, {"goals": [goal.pk for goal in self.goals]}, follow=True) | ||
self.assertEqual(list(self.content.goals.all()), self.goals) | ||
self.assertContains(response, "alert-box success") | ||
self.assertEqual(goals_management.send.call_count, 1) | ||
|
||
@patch("zds.tutorialv2.signals.goals_management") | ||
def test_invalid_parameters(self, goals_management): | ||
self.client.force_login(self.staff) | ||
response = self.client.post(self.url, {"goals": [42]}, follow=True) | ||
self.assertEqual(list(self.content.goals.all()), []) | ||
self.assertContains(response, "alert-box alert") | ||
self.assertFalse(goals_management.send.called) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from crispy_forms.bootstrap import StrictButton | ||
from crispy_forms.helper import FormHelper | ||
from crispy_forms.layout import HTML, Layout, Field, ButtonHolder | ||
from django.contrib import messages | ||
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin | ||
from django.forms import forms, ModelMultipleChoiceField, CheckboxSelectMultiple | ||
from django.shortcuts import get_object_or_404 | ||
from django.urls import reverse | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from zds.tutorialv2 import signals | ||
from zds.tutorialv2.mixins import SingleContentFormViewMixin | ||
from zds.tutorialv2.models.database import PublishableContent | ||
from zds.tutorialv2.models.goals import Goal | ||
from zds.utils import get_current_user | ||
|
||
|
||
class EditGoalsForm(forms.Form): | ||
goals = ModelMultipleChoiceField( | ||
label=_("Objectifs de la publication :"), | ||
queryset=Goal.objects.all(), | ||
required=False, | ||
widget=CheckboxSelectMultiple, | ||
) | ||
|
||
def __init__(self, content, *args, **kwargs): | ||
kwargs["initial"] = {"goals": content.goals.all()} | ||
super().__init__(*args, **kwargs) | ||
|
||
self.helper = FormHelper() | ||
self.helper.form_class = "content-wrapper" | ||
self.helper.form_method = "post" | ||
self.helper.form_id = "edit-goals" | ||
self.helper.form_class = "modal modal-flex" | ||
self.helper.form_action = reverse("content:edit-goals", kwargs={"pk": content.pk}) | ||
self.helper.layout = Layout( | ||
Field("goals"), | ||
ButtonHolder(StrictButton("Valider", type="submit")), | ||
) | ||
|
||
|
||
class EditGoals(LoginRequiredMixin, PermissionRequiredMixin, SingleContentFormViewMixin): | ||
permission_required = "tutorialv2.change_publishablecontent" | ||
model = PublishableContent | ||
form_class = EditGoalsForm | ||
success_message = _("Les objectifs ont bien été modifiés.") | ||
modal_form = True | ||
|
||
def dispatch(self, request, *args, **kwargs): | ||
content = get_object_or_404(PublishableContent, pk=self.kwargs["pk"]) | ||
success_url_kwargs = {"pk": content.pk, "slug": content.slug} | ||
self.success_url = reverse("content:view", kwargs=success_url_kwargs) | ||
return super().dispatch(request, *args, **kwargs) | ||
|
||
def get_form_kwargs(self): | ||
kwargs = super().get_form_kwargs() | ||
kwargs["content"] = self.object | ||
return kwargs | ||
|
||
def form_invalid(self, form): | ||
form.previous_page_url = self.success_url | ||
return super().form_invalid(form) | ||
|
||
def form_valid(self, form): | ||
self.object.goals.clear() | ||
new_goals = Goal.objects.filter(id__in=form.cleaned_data["goals"]) | ||
self.object.goals.add(*new_goals) | ||
messages.success(self.request, self.success_message) | ||
signals.goals_management.send(sender=self.__class__, performer=get_current_user(), content=self.object) | ||
return super().form_valid(form) |