Skip to content

Commit

Permalink
Refactorise le formulaire
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Apr 7, 2024
1 parent 9f51ac4 commit dfcdf24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion zds/tutorialv2/tests/tests_views/tests_editcategoriesview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from zds.tutorialv2.tests import TutorialTestMixin, override_for_contents
from zds.member.tests.factories import ProfileFactory, StaffProfileFactory
from zds.tutorialv2.tests.factories import PublishableContentFactory
from zds.tutorialv2.views.categories import EditCategoriesForm
from zds.utils.tests.factories import SubCategoryFactory


Expand Down Expand Up @@ -128,5 +129,5 @@ def test_remove_published(self):

form_data = {"subcategory": []}
response = self.client.post(self.url, form_data, follow=True)
self.assertContains(response, escape("car ce contenu est déjà publié."))
self.assertContains(response, escape(EditCategoriesForm.error_messages["no_category_but_public"]))
self.assertQuerysetEqual(self.content.subcategory.all(), [self.category_0])
38 changes: 24 additions & 14 deletions zds/tutorialv2/views/categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from crispy_forms.layout import Layout, Field

from django import forms
from django.contrib import messages
from django.core.exceptions import ValidationError
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView
Expand All @@ -22,9 +22,15 @@ class EditCategoriesForm(forms.Form):
widget=forms.CheckboxSelectMultiple(),
)

def __init__(self, *args, **kwargs):
error_messages = {
"no_category_but_public": _("Vous devez choisir au moins une catégorie, car ce contenu est déjà publié.")
}

def __init__(self, content, *args, **kwargs):
super().__init__(*args, **kwargs)

self.content = content

self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
Expand All @@ -33,6 +39,13 @@ def __init__(self, *args, **kwargs):
StrictButton(_("Valider"), type="submit"),
)

def clean_subcategory(self):
subcategory = self.cleaned_data["subcategory"]
# Forbid removing all categories of a validated content
if self.content.in_public() and not subcategory:
raise ValidationError(message=self.error_messages["no_category_but_public"])
return subcategory


class EditCategoriesView(LoggedWithReadWriteHability, SingleContentFormViewMixin, FormView):
template_name = "tutorialv2/edit/categories.html"
Expand All @@ -44,21 +57,18 @@ def get_initial(self):
initial["subcategory"] = self.object.subcategory.all()
return initial

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["content"] = self.object
return kwargs

def form_valid(self, form):
publishable = self.object
content = self.object

# Forbid removing all categories of a validated content
if publishable.in_public() and not form.cleaned_data["subcategory"]:
messages.error(
self.request, _("Vous devez choisir au moins une catégorie, car ce contenu est déjà publié.")
)
return self.form_invalid(form)

# Update categories
publishable.subcategory.clear()
content.subcategory.clear()
for subcat in form.cleaned_data["subcategory"]:
publishable.subcategory.add(subcat)
content.subcategory.add(subcat)

self.success_url = reverse("content:view", args=[publishable.pk, publishable.slug])
self.success_url = reverse("content:view", args=[content.pk, content.slug])

return super().form_valid(form)

0 comments on commit dfcdf24

Please sign in to comment.