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

Le forum évite de remonter les sujets dont le dernier message est masqué #6056

Open
wants to merge 34 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8ea40c1
Fix #5980
Rowin Mar 2, 2021
3a8256e
Ajout des tests
Rowin Mar 2, 2021
55d9662
Ajout d'un filtre par tag sur les flux RSS/Atom des contenus
entwanne Feb 26, 2021
39cdb47
fixup! Ajout d'un filtre par tag sur les flux RSS/Atom des contenus
entwanne Feb 26, 2021
2f2b880
fixup! Ajout d'un filtre par tag sur les flux RSS/Atom des contenus
entwanne Feb 26, 2021
ea8302e
Strip des valeurs des filtres category/subcategory sur les flux RSS/Atom
entwanne Feb 26, 2021
469ad1a
Relaie les paramTres GET de la page courante aux flux RSS
entwanne Feb 26, 2021
bc02384
Correction de l'affichage des membres sur les pages des casquettes
Situphen Mar 2, 2021
7e7a914
Survoler le sous-titre d'une cartouche de sujet du forum ne souligne …
Situphen Mar 2, 2021
1fd1fd5
Rend moins visible un paragraphe
Situphen Mar 4, 2021
3cebb75
On cache le bouton Modifier dans les MPs si quelqu'un a répondu après
Situphen Mar 2, 2021
41e56d0
Un membre ne peut pas modifier son message s'il est masqué
Situphen Mar 2, 2021
8cb1a6a
Cache les boutons Spam potentiel et Réponse utile pour les messages m…
Situphen Mar 4, 2021
fde6149
Fix hauteur body et z-index header
viki53 Feb 26, 2021
83c99c5
Change l'URL de la page de profil en /@pseudo (#6057)
Rowin Mar 4, 2021
03e2227
Passage à zmarkdown 9.1.4 pour corriger un bug
Situphen Mar 6, 2021
4ed1610
Création d'une méthode last_visible_post
Rowin Mar 7, 2021
434e34f
Créé un filtre pour trier les topics par date
Rowin Mar 7, 2021
bde3301
Ajout d'un test de l'anti-spam
Rowin Mar 7, 2021
59001b6
Ajout d'un test du filtre de tri des topics
Rowin Mar 7, 2021
b20aa9a
Merge branch 'dev' into feature-lastposthidden
Rowin Mar 7, 2021
65ded0c
Merge branch 'dev' into feature-lastposthidden
Rowin Mar 11, 2021
82d583c
Changement dans les tests
Rowin Mar 11, 2021
8457537
Merge branch 'dev' into feature-lastposthidden
Situphen Apr 16, 2021
1b45c77
Changement de update pour last_update
Rowin Apr 27, 2021
2dda367
Merge branch 'feature-lastposthidden' of github.com:Rowin/zds-site in…
Rowin Apr 27, 2021
794f1db
Merge branch 'dev' into feature-lastposthidden
Rowin Apr 27, 2021
f7d681f
Linting
Rowin Apr 27, 2021
ff3aea2
Merge branch 'dev' into feature-lastposthidden
Rowin May 3, 2021
267ec15
Merge branch 'dev' into feature-lastposthidden
AmauryCarrade May 18, 2021
9a9d5b1
Corrige un problème d'ordre des derniers sujets lorsque les sujets so…
Rowin May 24, 2021
eaa0b01
Corrige un problème d'ordre des sujets lorsqu'un message a été modifié
Rowin May 24, 2021
814ac8d
Merge branch 'dev' into feature-lastposthidden
Rowin May 25, 2021
046f4bf
Merge branch 'dev' into feature-lastposthidden
Rowin Aug 17, 2021
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
3 changes: 2 additions & 1 deletion templates/forum/category/forum.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% extends "forum/base.html" %}
{% load i18n %}
{% load interventions %}
{% load topics_sort %}


{% block title %}
Expand Down Expand Up @@ -86,7 +87,7 @@

{% if topics %}
<div class="topic-list navigable-list" itemscope itemtype="http://schema.org/ItemList">
{% for topic in topics %}
{% for topic in topics|topics_sort %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puisque le tri est fait dans get_all_topics_of_a_forum(), on n'a plus besoin de trier ici, non ?

{% include "forum/includes/topic_row.part.html" %}
{% endfor %}
</div>
Expand Down
5 changes: 3 additions & 2 deletions zds/forum/managers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.conf import settings
from django.db import models
from django.db.models import Q, F
from django.db.models import Q, F, Max
from model_utils.managers import InheritanceManager

from zds.utils import get_current_user
Expand Down Expand Up @@ -93,7 +93,8 @@ def get_last_topics(self):
def get_all_topics_of_a_forum(self, forum_pk, is_sticky=False):
return (
self.filter(forum__pk=forum_pk, is_sticky=is_sticky)
.order_by("-last_message__pubdate")
.annotate(last_visible_update=Max("post__pubdate", filter=Q(post__is_visible=True)))
.order_by("-last_visible_update")
.select_related("author__profile")
.prefetch_related("last_message", "tags")
.all()
Expand Down
25 changes: 22 additions & 3 deletions zds/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,15 @@ def get_post_count(self):

def get_last_message(self):
"""
:return: the last message on the forum, if there are any.
:return: the last visible message on the forum, if there are any.
"""
try:
last_post = Post.objects.select_related("topic").filter(topic__forum=self).order_by("-pubdate").all()[0]
last_post = (
Post.objects.select_related("topic")
.filter(topic__forum=self, is_visible=True)
.order_by("-pubdate")
.all()[0]
)
last_post.topic.forum = self
return last_post
except IndexError:
Expand Down Expand Up @@ -232,6 +237,11 @@ def meta_description(self):
return first_post.text
return Topic.__remove_greetings(first_post)[: settings.ZDS_APP["forum"]["description_size"]]

@property
def last_update(self):
last_visible_post = self.get_last_visible_post()
return last_visible_post.pubdate
Comment on lines +242 to +243
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.get_last_visible_post() peut retourner None, dans ce cas, on ne peut pas accéder à l'attribut pubdate et ça va générer une erreur.


@staticmethod
def __remove_greetings(post):
greetings = settings.ZDS_APP["forum"]["greetings"]
Expand All @@ -257,6 +267,15 @@ def get_post_count(self):
"""
return Post.objects.filter(topic__pk=self.pk).count()

def get_last_visible_post(self):
"""
:return: the last visible post in the thread.
"""
try:
return self.post_set.filter(is_visible=True).latest("pubdate")
except Post.DoesNotExist:
return None

def get_last_post(self):
"""
:return: the last post in the thread.
Expand All @@ -270,7 +289,7 @@ def get_last_answer(self):
return `None`.
:return: the last answer in the thread, if any.
"""
last_post = self.get_last_post()
last_post = self.get_last_visible_post()

if last_post == self.first_post():
return None
Expand Down
34 changes: 34 additions & 0 deletions zds/forum/tests/tests_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,24 @@ def test_failure_new_post_stopped_by_anti_spam(self):

self.assertEqual(403, response.status_code)

def test_new_post_stopped_by_anti_spam_after_hidden_post(self):
profile = ProfileFactory()
_, forum = create_category_and_forum()
topic = create_topic_in_forum(forum, profile)

post_to_hide = PostFactory(topic=topic, author=profile.user, position=topic.last_message.position + 1)

staff = StaffProfileFactory()
self.client.force_login(staff.user)
text_hidden_expected = "Bad guy!"
data = {"delete_message": "", "text_hidden": text_hidden_expected}
response = self.client.post(reverse("post-edit") + "?message={}".format(post_to_hide.pk), data, follow=False)

self.client.force_login(profile.user)
response = self.client.get(reverse("post-new") + "?sujet={}".format(topic.pk))

self.assertEqual(403, response.status_code)

def test_success_new_post_method_get(self):
another_profile = ProfileFactory()
_, forum = create_category_and_forum()
Expand Down Expand Up @@ -1345,6 +1363,22 @@ def test_success_edit_post_hide_message_by_staff(self):
self.assertEqual(staff.user, post.editor)
self.assertEqual(text_hidden_expected, post.text_hidden)

def test_last_post_update_after_hiding(self):
profile = ProfileFactory()
_, forum = create_category_and_forum()
topic = create_topic_in_forum(forum, profile)
expected_last_post = topic.last_message
post_to_hide = PostFactory(topic=topic, author=profile.user, position=2)

staff = StaffProfileFactory()
self.client.force_login(staff.user)
text_hidden_expected = "Bad guy!"
data = {"delete_message": "", "text_hidden": text_hidden_expected}
response = self.client.post(reverse("post-edit") + "?message={}".format(post_to_hide.pk), data, follow=False)

last_post = Post.objects.get(pk=topic.get_last_visible_post().pk)
self.assertEqual(last_post.pk, expected_last_post.pk)

def test_hide_helpful_message(self):
profile = ProfileFactory()
_, forum = create_category_and_forum()
Expand Down
13 changes: 13 additions & 0 deletions zds/utils/templatetags/topics_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django import template
from django.core.cache import cache


register = template.Library()


@register.filter("topics_sort")
def topics_sort(topics):
"""
:return: the topics sorted by last update (last updated first)
"""
return sorted(topics, key=lambda topic: topic.last_update, reverse=True)
10 changes: 10 additions & 0 deletions zds/utils/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from zds.utils.misc import contains_utf8mb4
from zds.utils.models import Alert
from zds.utils.context_processor import get_header_notifications
from zds.utils.templatetags.topics_sort import topics_sort
from zds.forum.factories import TopicFactory, create_category_and_forum, create_topic_in_forum


class Misc(TestCase):
Expand All @@ -31,3 +33,11 @@ def test_intervention_filter_for_tribunes(self):
filter_result = get_header_notifications(staff.user)["alerts"]
self.assertEqual(1, filter_result["total"])
self.assertEqual(alert.text, filter_result["list"][0]["text"])

def test_topics_sort(self):
author = ProfileFactory()
_, forum = create_category_and_forum()
topic1 = create_topic_in_forum(forum, author)
topic2 = create_topic_in_forum(forum, author)

self.assertEqual(topics_sort([topic1, topic2]), [topic2, topic1])