diff --git a/lacommunaute/forum/forms.py b/lacommunaute/forum/forms.py
index 9edf7ac6..82eab31c 100644
--- a/lacommunaute/forum/forms.py
+++ b/lacommunaute/forum/forms.py
@@ -2,6 +2,8 @@
from django import forms
from django.conf import settings
+from django.forms import CharField, CheckboxSelectMultiple, ModelMultipleChoiceField
+from taggit.models import Tag
from lacommunaute.forum.models import Forum
@@ -36,12 +38,27 @@ class ForumForm(forms.ModelForm):
widget=forms.FileInput(attrs={"accept": settings.SUPPORTED_IMAGE_FILE_TYPES.keys()}),
)
certified = forms.BooleanField(required=False, label="Certifiée par la communauté de l'inclusion")
+ tags = ModelMultipleChoiceField(
+ label="Sélectionner un ou plusieurs tags",
+ queryset=Tag.objects.all(),
+ widget=CheckboxSelectMultiple,
+ required=False,
+ )
+ new_tag = CharField(required=False, label="Ajouter un tag")
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ if self.instance.pk:
+ self.fields["tags"].initial = self.instance.tags.all()
def save(self, commit=True):
forum = super().save(commit=False)
forum.description = wrap_iframe_in_div_tag(self.cleaned_data.get("description"))
+
if commit:
forum.save()
+ forum.tags.set(self.cleaned_data["tags"])
+ forum.tags.add(self.cleaned_data["new_tag"]) if self.cleaned_data.get("new_tag") else None
return forum
class Meta:
diff --git a/lacommunaute/forum/tests/__snapshots__/test_forum_updateview.ambr b/lacommunaute/forum/tests/__snapshots__/test_forum_updateview.ambr
new file mode 100644
index 00000000..0fc1d617
--- /dev/null
+++ b/lacommunaute/forum/tests/__snapshots__/test_forum_updateview.ambr
@@ -0,0 +1,51 @@
+# serializer version: 1
+# name: test_selected_tags_are_preloaded[selected_tags_preloaded]
+ '''
+
+ '''
+# ---
diff --git a/lacommunaute/forum/tests/test_forum_updateview.py b/lacommunaute/forum/tests/test_forum_updateview.py
index de8edd4c..e5e23eff 100644
--- a/lacommunaute/forum/tests/test_forum_updateview.py
+++ b/lacommunaute/forum/tests/test_forum_updateview.py
@@ -1,14 +1,22 @@
from io import BytesIO
import pytest # noqa
+from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.urls import reverse
from faker import Faker
from PIL import Image
from pytest_django.asserts import assertContains
+from taggit.models import Tag
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.users.factories import UserFactory
+from lacommunaute.utils.testing import parse_response_to_soup, reset_model_sequence_fixture
+
+
+faker = Faker(settings.LANGUAGE_CODE)
+
+reset_tag_sequence = pytest.fixture(reset_model_sequence_fixture(Tag))
@pytest.fixture
@@ -97,3 +105,44 @@ def test_certified_forum(client, db):
forum.refresh_from_db()
assert forum.certified is True
+
+
+def test_selected_tags_are_preloaded(client, db, reset_tag_sequence, snapshot):
+ client.force_login(UserFactory(is_superuser=True))
+ forum = ForumFactory(with_tags=["iae", "siae", "prescripteur"])
+ Tag.objects.create(name="undesired_tag")
+ url = reverse("forum_extension:edit_forum", kwargs={"pk": forum.pk, "slug": forum.slug})
+
+ response = client.get(url)
+ assert response.status_code == 200
+
+ content_tags = parse_response_to_soup(
+ response, selector="#div_id_tags", replace_in_href=[tag for tag in Tag.objects.all()]
+ )
+ assert str(content_tags) == snapshot(name="selected_tags_preloaded")
+
+
+def test_added_tags_are_saved(client, db):
+ client.force_login(UserFactory(is_superuser=True))
+ forum = ForumFactory()
+
+ Tag.objects.bulk_create([Tag(name=tag, slug=tag) for tag in [faker.word() for _ in range(3)]])
+ # new_tag is not in the database
+ new_tag = faker.word()
+
+ url = reverse("forum_extension:edit_forum", kwargs={"pk": forum.pk, "slug": forum.slug})
+ response = client.post(
+ url,
+ data={
+ "name": forum.name,
+ "short_description": forum.short_description,
+ "description": forum.description.raw,
+ "tags": [Tag.objects.first().pk],
+ "new_tag": new_tag,
+ },
+ )
+
+ assert response.status_code == 302
+
+ forum.refresh_from_db()
+ assert all(tag in [tag.name for tag in forum.tags.all()] for tag in [Tag.objects.first().name, new_tag])
diff --git a/lacommunaute/forum/tests/tests_forms.py b/lacommunaute/forum/tests/tests_forms.py
index 6f0897e3..739bd677 100644
--- a/lacommunaute/forum/tests/tests_forms.py
+++ b/lacommunaute/forum/tests/tests_forms.py
@@ -48,3 +48,5 @@ def test_form_field():
assert not form.fields["description"].required
assert not form.fields["image"].required
assert not form.fields["certified"].required
+ assert not form.fields["tags"].required
+ assert not form.fields["new_tag"].required
diff --git a/lacommunaute/templates/forum/partials/forum_form.html b/lacommunaute/templates/forum/partials/forum_form.html
index 572b0bb3..d729515d 100644
--- a/lacommunaute/templates/forum/partials/forum_form.html
+++ b/lacommunaute/templates/forum/partials/forum_form.html
@@ -11,6 +11,9 @@
{% include "partials/form_field.html" with field=form.description %}
{% include "partials/form_field.html" with field=form.image %}
{% include "partials/form_field.html" with field=form.certified %}
+
+ {% include "partials/form_field.html" with field=form.tags %}
+ {% include "partials/form_field.html" with field=form.new_tag %}