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

Supprimer la vue ModeratorEngagementView #314

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,6 @@
# ---------------------------------------
MACHINA_MARKUP_WIDGET = "lacommunaute.forum_conversation.widgets.MarkdownTextareaWidget"


# STATISTIQUES
DAYS_IN_A_PERIOD = 15

# Django sites framework
SITE_ID = 1

Expand Down
40 changes: 1 addition & 39 deletions lacommunaute/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

from django.contrib.auth.models import Group
from django.db import models
from django.db.models.functions import TruncWeek
from django.utils import timezone
from django.utils.functional import cached_property
from machina.apps.forum.abstract_models import AbstractForum

from config.settings.base import DAYS_IN_A_PERIOD
from lacommunaute.forum_conversation.forum_polls.models import TopicPollVote
from lacommunaute.forum_conversation.models import Post, Topic
from lacommunaute.forum_upvote.models import UpVote
from lacommunaute.utils.enums import PeriodAggregation
from lacommunaute.utils.stats import count_objects_per_period, format_counts_of_objects_for_timeline_chart
from lacommunaute.forum_conversation.models import Topic


class ForumQuerySet(models.QuerySet):
Expand All @@ -30,37 +23,6 @@ class Forum(AbstractForum):

objects = ForumQuerySet().as_manager()

def get_stats(self, period_back):

start_date = timezone.now() - timezone.timedelta(days=period_back * DAYS_IN_A_PERIOD - 1)
forums = self.get_family()

datas = (
count_objects_per_period(
Topic.objects.filter(forum__in=forums, created__gte=start_date).annotate(period=TruncWeek("created")),
"topics",
)
+ count_objects_per_period(
Post.objects.filter(topic__forum__in=forums, created__gte=start_date).annotate(
period=TruncWeek("created")
),
"posts",
)
+ count_objects_per_period(
UpVote.objects.filter(post__topic__forum__in=forums, created_at__gte=start_date).annotate(
period=TruncWeek("created_at")
),
"upvotes",
)
+ count_objects_per_period(
TopicPollVote.objects.filter(
poll_option__poll__topic__forum__in=forums, timestamp__gte=start_date
).annotate(period=TruncWeek("timestamp")),
"pollvotes",
)
)
return format_counts_of_objects_for_timeline_chart(datas, period=PeriodAggregation.WEEK)

def get_unanswered_topics(self):
return Topic.objects.unanswered().filter(forum__in=self.get_descendants(include_self=True))

Expand Down
42 changes: 1 addition & 41 deletions lacommunaute/forum/tests/tests_model.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from django.db import IntegrityError
from django.test import TestCase
from django.utils import timezone
from faker import Faker
from machina.core.db.models import get_model
from machina.core.loading import get_class

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory
from lacommunaute.forum_conversation.forum_polls.factories import TopicPollVoteFactory
from lacommunaute.forum_upvote.factories import UpVoteFactory
from lacommunaute.forum_conversation.factories import TopicFactory


faker = Faker()
Expand Down Expand Up @@ -40,43 +37,6 @@ def test_invitation_token_is_unique(self):
forum.id = None
forum.save()

def test_get_stats(self):

forum = ForumFactory()
topic = TopicFactory(forum=forum, with_post=True, with_poll_vote=True)
UpVoteFactory(post=topic.posts.first(), voter=topic.poster)

# create old instance to ensure they are filtered
fifteen_days_ago = timezone.now() - timezone.timedelta(days=15)
old_topic = TopicFactory(forum=forum)
old_topic.created = fifteen_days_ago
old_topic.save()
old_post = PostFactory(topic=old_topic, poster=old_topic.poster)
old_post.created = fifteen_days_ago
old_post.save()
old_pollvote = TopicPollVoteFactory(voter=old_topic.poster, poll_option__poll__topic=old_topic)
old_pollvote.timestamp = fifteen_days_ago
old_pollvote.save()
old_upvote = UpVoteFactory(post=old_topic.posts.first(), voter=old_topic.poster)
old_upvote.created_at = fifteen_days_ago
old_upvote.save()

# create fake forum to ensure post, topic, etc… number is filter by forum
fake_forum = ForumFactory()
fake_topic = TopicFactory(forum=fake_forum, with_post=True, with_poll_vote=True)
UpVoteFactory(post=fake_topic.posts.first(), voter=fake_topic.poster)

self.assertEqual(
forum.get_stats(1),
{
"period": [timezone.now().strftime("%Y-%W")],
"pollvotes": [1],
"upvotes": [1],
"posts": [1],
"topics": [1],
},
)

def test_get_unanswered_topics(self):
topic1 = TopicFactory(forum=ForumFactory(), posts_count=1)
topic2 = TopicFactory(forum=ForumFactory(parent=topic1.forum), posts_count=1)
Expand Down
118 changes: 1 addition & 117 deletions lacommunaute/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
from django.contrib.auth.models import AnonymousUser, Group
from django.template.defaultfilters import truncatechars_html
from django.test import RequestFactory, TestCase, override_settings
from django.test import RequestFactory, TestCase
from django.urls import reverse
from django.utils.http import urlencode
from faker import Faker
from machina.core.db.models import get_model
from machina.core.loading import get_class
from machina.test.factories.tracking import TopicReadTrackFactory

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum.models import Forum
from lacommunaute.forum.views import ForumView
from lacommunaute.forum_conversation.factories import PostFactory, TopicFactory
from lacommunaute.forum_conversation.forms import PostForm
from lacommunaute.forum_conversation.forum_attachments.factories import AttachmentFactory
from lacommunaute.forum_conversation.forum_polls.factories import TopicPollFactory, TopicPollVoteFactory
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.forum_upvote.factories import CertifiedPostFactory
from lacommunaute.users.factories import UserFactory
Expand Down Expand Up @@ -187,13 +184,6 @@ def test_moderator_links(self):

# no permission
response = self.client.get(self.url)
self.assertNotContains(
response,
reverse(
"forum_extension:engagement",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)
self.assertNotContains(
response,
reverse(
Expand All @@ -205,13 +195,6 @@ def test_moderator_links(self):
# permission
assign_perm("can_approve_posts", self.user, self.forum)
response = self.client.get(self.url)
self.assertContains(
response,
reverse(
"forum_extension:engagement",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)
self.assertContains(
response,
reverse(
Expand All @@ -224,13 +207,6 @@ def test_moderator_links(self):
self.forum.members_group = None
self.forum.save()
response = self.client.get(self.url)
self.assertContains(
response,
reverse(
"forum_extension:engagement",
kwargs={"pk": self.forum.pk, "slug": self.forum.slug},
),
)
self.assertNotContains(
response,
reverse(
Expand Down Expand Up @@ -399,98 +375,6 @@ def test_descendants_are_in_cards_if_forum_is_category_type(self):
)


class ModeratorEngagementViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.topic = TopicFactory(with_post=True)
cls.user = cls.topic.poster
cls.url = reverse(
"forum_extension:engagement",
kwargs={"pk": cls.topic.forum.pk, "slug": cls.topic.forum.slug},
)

@override_settings(DEFAULT_FILE_STORAGE="django.core.files.storage.FileSystemStorage")
def test_queryset(self):
assign_perm("can_approve_posts", self.user, self.topic.forum)
self.client.force_login(self.user)

users = UserFactory.create_batch(3)

# count views
# count likes
for user in users:
TopicReadTrackFactory(topic=self.topic, user=user)
self.topic.likers.add(user)
self.topic.save()

# count replies
PostFactory(topic=self.topic, poster=self.user)

# count attachments
AttachmentFactory(post=self.topic.posts.first())

# count votes
poll = TopicPollFactory(topic=self.topic)
TopicPollVoteFactory.create_batch(4, poll_option__poll=poll, voter=self.user)

response = self.client.get(self.url)
self.assertEqual(
response.context["topics"].values("likes", "messages", "attached", "votes").first(),
{"likes": 3, "messages": 2, "attached": 1, "votes": 4},
)

# exclued topic not approved
not_approved_topic = TopicFactory(forum=self.topic.forum, poster=self.user, approved=False)
response = self.client.get(self.url)
self.assertNotIn(not_approved_topic, response.context["topics"])

# order
new_topic = TopicFactory(forum=self.topic.forum, poster=self.user)
response = self.client.get(self.url)
self.assertEqual(response.context["topics"].first(), new_topic)

def test_context(self):
assign_perm("can_approve_posts", self.user, self.topic.forum)
self.client.force_login(self.user)

response = self.client.get(self.url)
self.assertEqual(response.context["forum"], self.topic.forum)
topic = response.context["topics"][0]
self.assertEqual(topic.id, self.topic.id)
self.assertEqual(response.context["stats"], self.topic.forum.get_stats(7))

def test_permission(self):
self.client.force_login(self.user)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 403)

assign_perm("can_approve_posts", self.user, self.topic.forum)
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertContains(
response,
reverse(
"forum_conversation:topic",
kwargs={
"forum_pk": self.topic.forum.pk,
"forum_slug": self.topic.forum.slug,
"pk": self.topic.pk,
"slug": self.topic.slug,
},
),
)

def test_forum_doesnt_exist(self):
self.client.force_login(self.user)
response = self.client.get(
reverse(
"forum_extension:engagement",
kwargs={"pk": 9999, "slug": self.topic.forum.slug},
)
)
self.assertEqual(response.status_code, 404)


class CreateForumView(TestCase):
def setUp(self):
self.user = UserFactory()
Expand Down
3 changes: 1 addition & 2 deletions lacommunaute/forum/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from lacommunaute.forum.views import CategoryForumListView, ForumCreateView, ForumView, ModeratorEngagementView
from lacommunaute.forum.views import CategoryForumListView, ForumCreateView, ForumView


app_name = "forum_extension"
Expand All @@ -9,6 +9,5 @@
urlpatterns = [
path("forum/create/", ForumCreateView.as_view(), name="create"),
path("forum/<str:slug>-<int:pk>/", ForumView.as_view(), name="forum"),
path("forum/<str:slug>-<int:pk>/engagement", ModeratorEngagementView.as_view(), name="engagement"),
path("forum/categories/", CategoryForumListView.as_view(), name="categories"),
]
38 changes: 0 additions & 38 deletions lacommunaute/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from django.contrib.auth.models import Group
from django.db.models import Count, Exists, OuterRef
from django.db.models.query import QuerySet
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import CreateView, ListView
from machina.apps.forum.views import ForumView as BaseForumView
Expand Down Expand Up @@ -145,43 +144,6 @@ def get_success_url(self):
return reverse("forum_conversation_extension:home")


class ModeratorEngagementView(PermissionRequiredMixin, ListView):
context_object_name = "topics"
template_name = "forum/moderator_engagement.html"
permission_required = ["can_approve_posts"]

def get_forum(self):
"""Returns the forum to consider."""
if not hasattr(self, "forum"):
self.forum = get_object_or_404(Forum, pk=self.kwargs["pk"])
return self.forum

def get_queryset(self):
"""Returns the list of items for this view."""
return (
self.get_forum()
.topics.exclude(approved=False)
.annotate(
likes=Count("likers", distinct=True),
views=Count("tracks", distinct=True),
messages=Count("posts", distinct=True),
attached=Count("posts__attachments", distinct=True),
votes=Count("poll__options__votes", distinct=True),
)
.order_by("-last_post_on")
)

def get_controlled_object(self):
"""Returns the controlled object."""
return self.get_forum()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["forum"] = self.forum
context["stats"] = self.forum.get_stats(7)
return context


class CategoryForumListView(ListView):
template_name = "forum/category_forum_list.html"
context_object_name = "forums"
Expand Down
Loading