Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forum): collecter les notations des fiches pratiques #677

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lacommunaute/forum/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib import admin
from machina.apps.forum.admin import ForumAdmin as BaseForumAdmin

from lacommunaute.forum.models import ForumRating


class ForumAdmin(BaseForumAdmin):
fieldsets = BaseForumAdmin.fieldsets
Expand All @@ -9,3 +12,11 @@ class ForumAdmin(BaseForumAdmin):
"invitation_token",
"kind",
)


@admin.register(ForumRating)
class ForumRatingAdmin(admin.ModelAdmin):
list_display = ("forum", "rating", "created")
list_filter = ("forum",)
list_display_links = ("rating",)
raw_id_fields = ("forum", "user")
10 changes: 9 additions & 1 deletion lacommunaute/forum/factories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import factory
from machina.test.factories.forum import ForumFactory as BaseForumFactory

from lacommunaute.forum.models import Forum
from lacommunaute.forum.models import Forum, ForumRating
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.users.factories import GroupFactory
from lacommunaute.utils.perms import add_public_perms_on_forum
Expand Down Expand Up @@ -46,3 +46,11 @@ def with_child(self, create, extracted, **kwargs):
return

ForumFactory(parent=self, with_public_perms=True, with_image=True, name=f"{self.name} - Forum")


class ForumRatingFactory(factory.django.DjangoModelFactory):
forum = factory.SubFactory(ForumFactory)
rating = factory.Faker("random_int", min=1, max=5)

class Meta:
model = ForumRating
35 changes: 35 additions & 0 deletions lacommunaute/forum/migrations/0014_forumrating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Generated by Django 5.0.6 on 2024-06-13 13:05

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("forum", "0013_alter_forum_image"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="ForumRating",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("created", models.DateTimeField(auto_now_add=True, verbose_name="Creation date")),
("updated", models.DateTimeField(auto_now=True, verbose_name="Update date")),
("session_id", models.CharField(max_length=40)),
("rating", models.PositiveSmallIntegerField()),
("forum", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to="forum.forum")),
(
"user",
models.ForeignKey(
blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
],
options={
"abstract": False,
},
),
]
20 changes: 20 additions & 0 deletions lacommunaute/forum/migrations/0015_alter_forumrating_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 5.0.6 on 2024-06-13 15:34

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("forum", "0014_forumrating"),
]

operations = [
migrations.AlterModelOptions(
name="forumrating",
options={
"ordering": ("-created",),
"verbose_name": "Notation Forum",
"verbose_name_plural": "Notations Forum",
},
),
]
16 changes: 16 additions & 0 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.urls import reverse
from django.utils.functional import cached_property
from machina.apps.forum.abstract_models import AbstractForum
from machina.models import DatedModel
from storages.backends.s3boto3 import S3Boto3Storage

from lacommunaute.forum.enums import Kind as Forum_Kind
Expand Down Expand Up @@ -72,3 +73,18 @@ def is_toplevel_discussion_area(self):
@cached_property
def is_newsfeed(self):
return self.kind == Forum_Kind.NEWS

def get_session_rating(self, session_key):
return getattr(ForumRating.objects.filter(forum=self, session_id=session_key).first(), "rating", None)


class ForumRating(DatedModel):
vincentporte marked this conversation as resolved.
Show resolved Hide resolved
session_id = models.CharField(max_length=40)
forum = models.ForeignKey(Forum, on_delete=models.CASCADE)
rating = models.PositiveSmallIntegerField()
vincentporte marked this conversation as resolved.
Show resolved Hide resolved
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True, blank=True)

class Meta:
verbose_name = "Notation Forum"
verbose_name_plural = "Notations Forum"
ordering = ("-created",)
89 changes: 89 additions & 0 deletions lacommunaute/forum/tests/__snapshots__/test_forum_rating_view.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# serializer version: 1
# name: test_anonymous_post_forum_rating_view[anonymous_post_forum_rating_view]
'''
<html><head></head><body><div class="c-box mb-5" id="rating-area">
<h2 class="mt-3">Cette fiche vous a-t-elle été utile ?</h2>

<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

</div>


</div>
</body></html>
'''
# ---
# name: test_authenticated_post_forum_rating_view[authenticated_post_forum_rating_view]
'''
<html><head></head><body><div class="c-box mb-5" id="rating-area">
<h2 class="mt-3">Cette fiche vous a-t-elle été utile ?</h2>

<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-line"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-line"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-line"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-line"></i>

</div>

</div>


</div>
</body></html>
'''
# ---
77 changes: 77 additions & 0 deletions lacommunaute/forum/tests/__snapshots__/tests_views.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,80 @@
</nav>
'''
# ---
# name: TestForumView.test_not_rated_forum[not_rated_forum]
'''
<div class="c-box mb-5" id="rating-area">
<h2 class="mt-3">Cette fiche vous a-t-elle été utile ?</h2>

<form hx-post="/forum/b-category-forum-[PK of Forum]/rate/" hx-swap="outerHTML" hx-target="#rating-area" id="rating-button">
<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<button class="btn btn-sm btn-ico btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="1">
<i class="ri-star-line"></i>
</button>

<button class="btn btn-sm btn-ico btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="2">
<i class="ri-star-line"></i>
</button>

<button class="btn btn-sm btn-ico btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="3">
<i class="ri-star-line"></i>
</button>

<button class="btn btn-sm btn-ico btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="4">
<i class="ri-star-line"></i>
</button>

<button class="btn btn-sm btn-ico btn-secondary matomo-event" data-matomo-action="rate" data-matomo-category="engagement" data-matomo-option="post" name="rating" type="submit" value="5">
<i class="ri-star-line"></i>
</button>

</div>
</form>

</div>
'''
# ---
# name: TestForumView.test_rated_forum[rated_forum]
'''
<div class="c-box mb-5" id="rating-area">
<h2 class="mt-3">Cette fiche vous a-t-elle été utile ?</h2>

<div aria-label="Notation de la fiche pratique" class="btn-group btn-group-lg" role="group">

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

<div class="btn btn-sm btn-ico">

<i class="ri-star-fill"></i>

</div>

</div>


</div>
'''
# ---
56 changes: 56 additions & 0 deletions lacommunaute/forum/tests/test_forum_rating_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest # noqa
from django.urls import reverse

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum.models import ForumRating
from lacommunaute.users.factories import UserFactory
from lacommunaute.utils.testing import parse_response_to_soup


@pytest.fixture(name="public_forum")
def fixture_public_forum(db):
return ForumFactory(with_public_perms=True)


def test_get_forum_rating_view(client, db, public_forum):
response = client.get(reverse("forum_extension:rate", kwargs={"pk": public_forum.pk, "slug": public_forum.slug}))
assert response.status_code == 405


def test_anonymous_post_forum_rating_view(client, db, public_forum, snapshot):
client.session.save()

response = client.post(
reverse("forum_extension:rate", kwargs={"pk": public_forum.pk, "slug": public_forum.slug}),
data={"rating": "5"},
)
assert response.status_code == 200
assert response.context["forum"] == public_forum
assert response.context["rating"] == 5
content = parse_response_to_soup(response, replace_in_href=[public_forum])
assert str(content) == snapshot(name="anonymous_post_forum_rating_view")

forum_rating = ForumRating.objects.get()
assert forum_rating.forum == public_forum
assert forum_rating.user is None
assert forum_rating.rating == 5
assert forum_rating.session_id == client.session.session_key


def test_authenticated_post_forum_rating_view(client, db, public_forum, snapshot):
user = UserFactory()
client.force_login(user)

response = client.post(
reverse("forum_extension:rate", kwargs={"pk": public_forum.pk, "slug": public_forum.slug}),
data={"rating": "1"},
)
assert response.status_code == 200
content = parse_response_to_soup(response, replace_in_href=[public_forum])
assert str(content) == snapshot(name="authenticated_post_forum_rating_view")

forum_rating = ForumRating.objects.get()
assert forum_rating.forum == public_forum
assert forum_rating.user == user
assert forum_rating.rating == 1
assert forum_rating.session_id == client.session.session_key
vincentporte marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 11 additions & 1 deletion lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test import TestCase

from lacommunaute.forum.enums import Kind as ForumKind
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory
from lacommunaute.forum.factories import CategoryForumFactory, ForumFactory, ForumRatingFactory
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.factories import TopicFactory
from lacommunaute.users.factories import UserFactory
Expand Down Expand Up @@ -107,3 +107,13 @@ def test_is_newsfeed(self):
self.assertTrue(news_forum.is_newsfeed)
self.assertFalse(discussion_area_forum.is_newsfeed)
self.assertFalse(private_forum.is_newsfeed)

def test_get_session_rating(self):
forum = ForumFactory()
forum_rating = ForumRatingFactory(forum=forum)

self.assertIsNone(forum.get_session_rating("test"))
self.assertEqual(forum.get_session_rating(forum_rating.session_id), forum_rating.rating)

ForumRatingFactory(forum=forum, session_id=forum_rating.session_id, rating=forum_rating.rating + 1)
self.assertEqual(forum.get_session_rating(forum_rating.session_id), forum_rating.rating + 1)
Loading
Loading