Skip to content

Commit

Permalink
add let staff user update parent forum, when updating a forum of type…
Browse files Browse the repository at this point in the history
… FORUM_POST, in documentation_area
  • Loading branch information
vincentporte committed Feb 25, 2025
1 parent d92cd13 commit b798dff
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
35 changes: 33 additions & 2 deletions lacommunaute/forum/tests/test_forum_updateview.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from taggit.models import Tag

from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.forum.forms import SubCategoryForumUpdateForm
from lacommunaute.partner.factories import PartnerFactory
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup, reset_model_sequence_fixture
Expand Down Expand Up @@ -51,13 +52,22 @@ def test_user_access(client, db):
assert response.status_code == 200


def test_context_data(client, db):
@pytest.mark.parametrize(
"forum,should_be_subcategoryupdateform",
[
(lambda: ForumFactory(), False),
(lambda: CategoryForumFactory(), False),
(lambda: CategoryForumFactory(with_child=True).get_children().first(), True),
],
)
def test_context_data(client, db, forum, should_be_subcategoryupdateform):
client.force_login(UserFactory(is_staff=True))
forum = ForumFactory()
forum = forum()
url = reverse("forum_extension:edit_forum", kwargs={"pk": forum.pk, "slug": forum.slug})
response = client.get(url)
assertContains(response, f"Mettre à jour le forum {forum.name}", html=True)
assertContains(response, reverse("forum_extension:forum", kwargs={"pk": forum.pk, "slug": forum.slug}))
assert isinstance(response.context["form"], SubCategoryForumUpdateForm) == should_be_subcategoryupdateform


def test_update_forum_image(client, db, fake_image):
Expand All @@ -83,6 +93,27 @@ def test_update_forum_image(client, db, fake_image):
assert forum.image.name == fake_image.name


def test_update_subcategory_forum_parent(client, db):
forum = CategoryForumFactory(with_child=True).get_children().first()
new_parent = CategoryForumFactory()
client.force_login(UserFactory(is_staff=True))

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,
"parent": new_parent.pk,
},
)
assert response.status_code == 302

forum.refresh_from_db()
assert forum.parent == new_parent


def test_certified_forum(client, db):
client.force_login(UserFactory(is_staff=True))
forum = CategoryForumFactory(with_child=True).get_children().first()
Expand Down
8 changes: 6 additions & 2 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from machina.core.loading import get_class
from taggit.models import Tag

from lacommunaute.forum.forms import ForumForm
from lacommunaute.forum.forms import ForumForm, SubCategoryForumUpdateForm
from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.view_mixins import FilteredTopicsListViewMixin
Expand Down Expand Up @@ -125,12 +125,16 @@ def get_context_data(self, **kwargs):

class ForumUpdateView(UserPassesTestMixin, UpdateView):
template_name = "forum/forum_create_or_update.html"
form_class = ForumForm
model = Forum

def test_func(self):
return self.request.user.is_staff

def get_form_class(self):
if self.object.is_in_documentation_area and self.object.type == Forum.FORUM_POST:
return SubCategoryForumUpdateForm
return ForumForm

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["title"] = f"Mettre à jour le forum {self.object.name}"
Expand Down

0 comments on commit b798dff

Please sign in to comment.