Skip to content

Commit

Permalink
add tags when creating or updating forum
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Aug 7, 2024
1 parent 0192545 commit 30287a8
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lacommunaute/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from django import forms
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.forms import CharField, CheckboxSelectMultiple, ModelMultipleChoiceField
from taggit.models import Tag

from lacommunaute.forum.models import Forum

Expand Down Expand Up @@ -36,12 +39,30 @@ 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)
try:
if self.instance.pk:
self.fields["tags"].initial = self.instance.tags.all()
except ObjectDoesNotExist:
pass

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:
Expand Down
51 changes: 51 additions & 0 deletions lacommunaute/forum/tests/__snapshots__/test_forum_updateview.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# serializer version: 1
# name: test_selected_tags_are_preloaded[selected_tags_preloaded]
'''
<div class="form-group" id="div_id_tags">


<label class="control-label" for="id_tags">
Sélectionner un ou plusieurs tags

<span class="text-muted">(optional)</span>

</label>



<div class="form-check form-check-inline">
<input checked="" class="form-check-input" id="id_tags_0" name="tags" type="checkbox" value="9999"/>
<label class="form-check-label badge badge-sm rounded-pill bg-info-lighter text-info" for="id_tags_0">
iae
</label>
</div>

<div class="form-check form-check-inline">
<input checked="" class="form-check-input" id="id_tags_1" name="tags" type="checkbox" value="10000"/>
<label class="form-check-label badge badge-sm rounded-pill bg-info-lighter text-info" for="id_tags_1">
siae
</label>
</div>

<div class="form-check form-check-inline">
<input checked="" class="form-check-input" id="id_tags_2" name="tags" type="checkbox" value="10001"/>
<label class="form-check-label badge badge-sm rounded-pill bg-info-lighter text-info" for="id_tags_2">
prescripteur
</label>
</div>

<div class="form-check form-check-inline">
<input class="form-check-input" id="id_tags_3" name="tags" type="checkbox" value="10002"/>
<label class="form-check-label badge badge-sm rounded-pill bg-info-lighter text-info" for="id_tags_3">
undesired_tag
</label>
</div>






</div>
'''
# ---
49 changes: 49 additions & 0 deletions lacommunaute/forum/tests/test_forum_updateview.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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])
2 changes: 2 additions & 0 deletions lacommunaute/forum/tests/tests_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions lacommunaute/templates/forum/partials/forum_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
<hr>
{% include "partials/form_field.html" with field=form.tags %}
{% include "partials/form_field.html" with field=form.new_tag %}
<hr class="mb-5">
<div class="form-actions form-row">
<div class="form-group col-auto">
Expand Down

0 comments on commit 30287a8

Please sign in to comment.