Skip to content

Commit

Permalink
Merge 29f01dc into c232671
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud-D committed Apr 6, 2024
2 parents c232671 + 29f01dc commit 2309dcd
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 34 deletions.
28 changes: 28 additions & 0 deletions templates/tutorialv2/edit/categories.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "tutorialv2/base.html" %}
{% load crispy_forms_tags %}
{% load thumbnail %}
{% load i18n %}
{% load feminize %}

{% block title %}
{% trans "Modifier les catégories de " %}{{ content.title }}
{% endblock %}

{% block breadcrumb %}
<li><a href="{{ content.get_absolute_url }}">{{ content.title }}</a></li>
<li>{% trans "Modifier les catégories" %}</li>
{% endblock %}

{% block headline %}
<h1 {% if content.image %}class="illu"{% endif %}>
{% if content.image %}
<img src="{{content.image.physical.tutorial_illu.url }}" alt="">
{% endif %}
{% blocktrans with title=content.title %}Modifier les catégories de « {{ title }} »{% endblocktrans %}
</h1>
{% endblock %}


{% block content %}
{% crispy form %}
{% endblock %}
24 changes: 22 additions & 2 deletions templates/tutorialv2/includes/headline/categories.part.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% load i18n %}
{% load pluralize_fr %}
{% load captureas %}

{% captureas categories_list %}
<p>
{% trans "Catégorie" %}{{ content.subcategory.all|pluralize }} :

{% trans "Dans" %}
{% for category in content.subcategory.all %}
{% if forloop.first %}{% elif forloop.last %} {% trans "et" %}{% else %},{% endif %}
{% if content.is_opinion %}
Expand All @@ -13,3 +14,22 @@
{% endif %}
{% endfor %}
</p>
{% endcaptureas %}

{% url "content:edit-categories" content.pk as edit_url %}

{% if show_form %}
{% if content.subcategory.all %}
<div class="editable-element">
{{ categories_list }}

{% if show_form %}
<a href="{{ edit_url }}" class="edit-button"><span class="visuallyhidden">{% trans "Modifier" %}</span></a>
{% endif %}
</div>
{% else %}
<a href="{{ edit_url }}">{% trans "Choisissez les catégories !" %}</span></a>
{% endif %}
{% elif content.subcategory.all %}
{{ categories_list }}
{% endif %}
2 changes: 1 addition & 1 deletion templates/tutorialv2/includes/headline/header.part.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% include "tutorialv2/includes/headline/authors.part.html" with db_content=db_content edit_authors=display_config.draft_actions.show_authors_management online_mode=display_config.online_config.enable_authors_online_mode %}
{% include "tutorialv2/includes/headline/licence.part.html" with licence=content.licence show_form=display_config.draft_actions.show_license_edit form=form_edit_license %}
{% include "tutorialv2/includes/headline/contributions.part.html" %}
{% include "tutorialv2/includes/headline/categories.part.html" %}
{% include "tutorialv2/includes/headline/categories.part.html" with content=content show_form=display_config.draft_actions.show_categories_management %}
{% include "tutorialv2/includes/headline/goals.part.html" with goals=publishablecontent.goals.all %}
{% include "tutorialv2/includes/headline/labels.part.html" with labels=publishablecontent.labels.all %}

Expand Down
16 changes: 4 additions & 12 deletions zds/tutorialv2/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,6 @@ class ContentForm(ContainerForm):

type = forms.ChoiceField(choices=TYPE_CHOICES, required=False)

subcategory = forms.ModelMultipleChoiceField(
label=_("Sélectionnez les catégories qui correspondent à votre contenu."),
queryset=SubCategory.objects.order_by("title").all(),
required=False,
widget=forms.CheckboxSelectMultiple(),
)

source = forms.URLField(
label=_(
"""Si votre contenu est publié en dehors de Zeste de Savoir (blog, site personnel, etc.),
Expand Down Expand Up @@ -166,7 +159,6 @@ def _create_layout(self):
),
Field("last_hash"),
Field("source"),
Field("subcategory", template="crispy/checkboxselectmultiple.html"),
)

self.helper.layout.append(Field("msg_commit"))
Expand Down Expand Up @@ -517,9 +509,9 @@ def __init__(self, content, *args, **kwargs):
no_category_msg = HTML(
_(
"""<p><strong>Votre publication n'est dans aucune catégorie.
Vous devez <a href="{}#{}">choisir une catégorie</a>
Vous devez <a href="{}">choisir une catégorie</a>
avant de demander la validation.</strong></p>""".format(
reverse("content:edit", kwargs={"pk": content.pk, "slug": content.slug}), "div_id_subcategory"
reverse("content:edit-categories", kwargs={"pk": content.pk}),
)
)
)
Expand Down Expand Up @@ -892,9 +884,9 @@ def __init__(self, content, *args, **kwargs):
no_category_msg = HTML(
_(
"""<p><strong>Votre publication n'est dans aucune catégorie.
Vous devez <a href="{}#{}">choisir une catégorie</a>
Vous devez <a href="{}">choisir une catégorie</a>
avant de publier.</strong></p>""".format(
reverse("content:edit", kwargs={"pk": content.pk, "slug": content.slug}), "div_id_subcategory"
reverse("content:edit-categories", kwargs={"pk": content.pk})
)
)
)
Expand Down
4 changes: 1 addition & 3 deletions zds/tutorialv2/tests/tests_front.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,7 @@ def test_the_editor_forgets_its_content_on_form_submission(self):
"content:create-content", kwargs={"created_content_type": "ARTICLE"}
)
selenium.get(new_article_url)
WebDriverWait(self.selenium, 10).until(
ec.element_to_be_clickable((By.CSS_SELECTOR, "input[type=checkbox][name=subcategory]"))
).click()
WebDriverWait(self.selenium, 10).until(ec.element_to_be_clickable((By.CSS_SELECTOR, "#id_title"))).click()

self.find_element("#id_title").send_keys("Oulipo")

Expand Down
3 changes: 3 additions & 0 deletions zds/tutorialv2/urls/urls_contents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.urls import path
from django.views.generic.base import RedirectView

from zds.tutorialv2.views.categories import EditCategoriesView
from zds.tutorialv2.views.contents import (
CreateContent,
EditContent,
Expand Down Expand Up @@ -220,6 +221,8 @@ def get_version_pages():
path("modifier-licence/<int:pk>/", EditContentLicense.as_view(), name="edit-license"),
# Modify the tags
path("modifier-tags/<int:pk>/", EditTags.as_view(), name="edit-tags"),
# Modify the categories
path("modifier-categories/<int:pk>/", EditCategoriesView.as_view(), name="edit-categories"),
# beta:
path("activer-beta/<int:pk>/<slug:slug>/", ManageBetaContent.as_view(action="set"), name="set-beta"),
path(
Expand Down
64 changes: 64 additions & 0 deletions zds/tutorialv2/views/categories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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.contrib import messages
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import FormView

from zds.member.decorator import LoggedWithReadWriteHability
from zds.tutorialv2.mixins import SingleContentFormViewMixin
from zds.tutorialv2.models.database import PublishableContent
from zds.utils.models import SubCategory


class EditCategoriesForm(forms.Form):
subcategory = forms.ModelMultipleChoiceField(
label=_("Sélectionnez les catégories qui correspondent à la publication."),
queryset=SubCategory.objects.order_by("title").all(),
required=False,
widget=forms.CheckboxSelectMultiple(),
)

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

self.helper = FormHelper()
self.helper.form_class = "content-wrapper"
self.helper.form_method = "post"
self.helper.layout = Layout(
Field("subcategory", template="crispy/checkboxselectmultiple.html"),
StrictButton(_("Valider"), type="submit"),
)


class EditCategoriesView(LoggedWithReadWriteHability, SingleContentFormViewMixin, FormView):
template_name = "tutorialv2/edit/categories.html"
model = PublishableContent
form_class = EditCategoriesForm

def get_initial(self):
initial = super().get_initial()
initial["subcategory"] = self.object.subcategory.all()
return initial

def form_valid(self, form):
publishable = 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()
for subcat in form.cleaned_data["subcategory"]:
publishable.subcategory.add(subcat)

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

return super().form_valid(form)
16 changes: 0 additions & 16 deletions zds/tutorialv2/views/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ def form_valid(self, form):

self.content.ensure_author_gallery()
self.content.save()
# Add subcategories on tutorial
for subcat in form.cleaned_data["subcategory"]:
self.content.subcategory.add(subcat)

self.content.save()

# create a new repo :
init_new_repo(
Expand Down Expand Up @@ -165,13 +160,6 @@ def form_valid(self, form):
messages.error(self.request, _("Une nouvelle version a été postée avant que vous ne validiez."))
return self.form_invalid(form)

# 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)

# first, update DB (in order to get a new slug if needed)
publishable.source = form.cleaned_data["source"]

Expand Down Expand Up @@ -209,10 +197,6 @@ def form_valid(self, form):
# update relationships :
publishable.sha_draft = sha

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

publishable.save()

self.success_url = reverse("content:view", args=[publishable.pk, publishable.slug])
Expand Down
3 changes: 3 additions & 0 deletions zds/tutorialv2/views/display/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def show_title_edit(self) -> bool:
def show_authors_management(self) -> bool:
return self.enabled and self.is_allowed

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

Expand Down

0 comments on commit 2309dcd

Please sign in to comment.