Skip to content

Commit

Permalink
feat(topic): add unexistant tags to topic
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Aug 12, 2024
1 parent b04ee28 commit 73f4f55
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lacommunaute/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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)
Expand All @@ -67,6 +68,11 @@ 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
)

if self.user.is_anonymous:
post.username = self.cleaned_data["username"]
Expand Down
34 changes: 33 additions & 1 deletion lacommunaute/forum_conversation/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<div class="tab-pane" id="tags">
<div class="row">
<div id="tags_formset" class="col-md-12">{% include "partials/form_field.html" with field=post_form.tags %}</div>
{% include "partials/form_field.html" with field=post_form.new_tags %}
</div>
</div>
{% if poll_option_formset %}
Expand Down

0 comments on commit 73f4f55

Please sign in to comment.