diff --git a/lacommunaute/forum_conversation/forms.py b/lacommunaute/forum_conversation/forms.py index 22c7c9cb..81f4ed37 100644 --- a/lacommunaute/forum_conversation/forms.py +++ b/lacommunaute/forum_conversation/forms.py @@ -50,6 +50,7 @@ class TopicForm(CreateUpdatePostMixin, AbstractTopicForm): tags = ModelMultipleChoiceField( label="", queryset=Tag.objects.all(), widget=CheckboxSelectMultiple, required=False ) + new_tags = CharField(required=False, label="Ajouter un tag ou plusieurs tags (séparés par des virgules)") def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) @@ -59,5 +60,10 @@ def __init__(self, *args, **kwargs): def save(self): post = super().save() post.topic.tags.set(self.cleaned_data["tags"]) + ( + post.topic.tags.add(*[tag.strip() for tag in self.cleaned_data["new_tags"].split(",")]) + if self.cleaned_data.get("new_tags") + else None + ) return post diff --git a/lacommunaute/forum_conversation/tests/tests_views.py b/lacommunaute/forum_conversation/tests/tests_views.py index 946488ab..b614f0c9 100644 --- a/lacommunaute/forum_conversation/tests/tests_views.py +++ b/lacommunaute/forum_conversation/tests/tests_views.py @@ -1,4 +1,4 @@ -import pytest +import pytest # noqa from django.conf import settings from django.contrib.messages.api import get_messages from django.contrib.messages.middleware import MessageMiddleware @@ -255,6 +255,38 @@ def test_redirections_on_documentation_forum(self, db, client, snapshot): content = parse_response_to_soup(response, selector="#div_id_content") assert str(content) == snapshot(name="topic_create") + def test_create_with_new_tags(self, db, client): + forum = ForumFactory(with_public_perms=True) + client.force_login(UserFactory()) + tags_list = [faker.word() for i in range(2)] + response = client.post( + reverse("forum_conversation:topic_create", kwargs={"forum_pk": forum.pk, "forum_slug": forum.slug}), + { + "subject": faker.sentence(), + "content": faker.paragraph(nb_sentences=5), + "new_tags": ", ".join(tags_list), + }, + follow=True, + ) + assert response.status_code == 200 + + queryset = forum.topics.get().tags.filter(name__in=tags_list) + assert all(tag in queryset.values_list("name", flat=True) for tag in tags_list) + + def test_create_without_tag(self, db, client): + forum = ForumFactory(with_public_perms=True) + client.force_login(UserFactory()) + response = client.post( + reverse("forum_conversation:topic_create", kwargs={"forum_pk": forum.pk, "forum_slug": forum.slug}), + { + "subject": faker.sentence(), + "content": faker.paragraph(nb_sentences=5), + }, + follow=True, + ) + assert response.status_code == 200 + assert forum.topics.get().tags.count() == 0 + class TopicUpdateViewTest(TestCase): @classmethod diff --git a/lacommunaute/templates/forum_conversation/partials/topic_form.html b/lacommunaute/templates/forum_conversation/partials/topic_form.html index 0534c331..ac40d25f 100644 --- a/lacommunaute/templates/forum_conversation/partials/topic_form.html +++ b/lacommunaute/templates/forum_conversation/partials/topic_form.html @@ -40,6 +40,7 @@
{% include "partials/form_field.html" with field=post_form.tags %}
+ {% include "partials/form_field.html" with field=post_form.new_tags %}
{% if poll_option_formset %}