From 9b46db4fb710227508db88710c93def3ef874c06 Mon Sep 17 00:00:00 2001 From: Calum Mackervoy Date: Tue, 11 Jun 2024 17:26:48 +0200 Subject: [PATCH] refactor(BlockedPostReason): remplace Enum avec models.TextChoices --- lacommunaute/forum_conversation/forms.py | 2 +- .../tests/tests_views_htmx.py | 6 ++--- lacommunaute/forum_moderation/enums.py | 22 +++++++++---------- .../migrations/0003_blockedpost.py | 14 +++++++++++- lacommunaute/forum_moderation/models.py | 4 +++- .../tests/test_post_disapprove_view.py | 2 +- lacommunaute/forum_moderation/utils.py | 8 +++---- lacommunaute/forum_moderation/views.py | 2 +- 8 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lacommunaute/forum_conversation/forms.py b/lacommunaute/forum_conversation/forms.py index dd1b7f733..6fa6f66bb 100644 --- a/lacommunaute/forum_conversation/forms.py +++ b/lacommunaute/forum_conversation/forms.py @@ -24,7 +24,7 @@ def clean(self): if self.user.is_authenticated: post.poster = self.user - if post.update_reason in BlockedPostReason.reasons_tracked_for_stats(): + if post.update_reason in [x.label for x in BlockedPostReason.reasons_tracked_for_stats()]: BlockedPost.create_from_post(post) return cleaned_data diff --git a/lacommunaute/forum_conversation/tests/tests_views_htmx.py b/lacommunaute/forum_conversation/tests/tests_views_htmx.py index ed5c30336..64e40a039 100644 --- a/lacommunaute/forum_conversation/tests/tests_views_htmx.py +++ b/lacommunaute/forum_conversation/tests/tests_views_htmx.py @@ -391,7 +391,7 @@ def test_create_post_as_blocked_not_blocked_anonymous(self, *args): blocked_post = BlockedPost.objects.get() assert blocked_post.content == self.content assert blocked_post.username == username - assert blocked_post.block_reason == BlockedPostReason.BLOCKED_USER.value + assert blocked_post.block_reason == BlockedPostReason.BLOCKED_USER.label def test_create_post_with_nonfr_content(self): assign_perm("can_reply_to_topics", self.user, self.topic.forum) @@ -419,7 +419,7 @@ def test_create_post_with_nonfr_content(self): blocked_post = BlockedPost.objects.get() assert blocked_post.poster == self.user assert blocked_post.content == "популярные лучшие песни слушать онлайн" - assert blocked_post.block_reason == BlockedPostReason.ALTERNATIVE_LANGUAGE.value + assert blocked_post.block_reason == BlockedPostReason.ALTERNATIVE_LANGUAGE.label def test_create_post_with_html_content(self): assign_perm("can_reply_to_topics", self.user, self.topic.forum) @@ -477,7 +477,7 @@ def test_create_post_with_blocked_domain_name(self): blocked_post = BlockedPost.objects.get() assert blocked_post.content == "la communauté" assert blocked_post.username == "spam@blocked.com" - assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN.value + assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN.label class CertifiedPostViewTest(TestCase): diff --git a/lacommunaute/forum_moderation/enums.py b/lacommunaute/forum_moderation/enums.py index ae7cf08fc..72605b213 100644 --- a/lacommunaute/forum_moderation/enums.py +++ b/lacommunaute/forum_moderation/enums.py @@ -1,12 +1,12 @@ -from enum import Enum +from django.db import models -class BlockedPostReason(Enum): - HTML_TAGS = "HTML tags detected" - ALTERNATIVE_LANGUAGE = "Alternative Language detected" - BLOCKED_DOMAIN = "Blocked Domain detected" - BLOCKED_USER = "Blocked Email detected" - MODERATOR_DISAPPROVAL = "Moderator disapproval" +class BlockedPostReason(models.TextChoices): + HTML_TAGS = "HTML_TAGS", "HTML tags detected" + ALTERNATIVE_LANGUAGE = "ALTERNATIVE_LANGUAGE", "Alternative Language detected" + BLOCKED_DOMAIN = "BLOCKED_DOMAIN", "Blocked Domain detected" + BLOCKED_USER = "BLOCKED_USER", "Blocked Email detected" + MODERATOR_DISAPPROVAL = "MODERATOR_DISAPPROVAL", "Moderator disapproval" @classmethod def reasons_tracked_for_stats(cls): @@ -15,8 +15,8 @@ def reasons_tracked_for_stats(cls): The list of "reasons for interest" are returned by this function """ return [ - cls.ALTERNATIVE_LANGUAGE.value, - cls.BLOCKED_DOMAIN.value, - cls.BLOCKED_USER.value, - cls.MODERATOR_DISAPPROVAL.value, + cls.ALTERNATIVE_LANGUAGE, + cls.BLOCKED_DOMAIN, + cls.BLOCKED_USER, + cls.MODERATOR_DISAPPROVAL, ] diff --git a/lacommunaute/forum_moderation/migrations/0003_blockedpost.py b/lacommunaute/forum_moderation/migrations/0003_blockedpost.py index 5c55a5382..f8fc1bda7 100644 --- a/lacommunaute/forum_moderation/migrations/0003_blockedpost.py +++ b/lacommunaute/forum_moderation/migrations/0003_blockedpost.py @@ -29,7 +29,19 @@ class Migration(migrations.Migration): ), ("username", models.EmailField(max_length=254, null=True, blank=True, verbose_name="Adresse email")), ("content", models.CharField(verbose_name="Content")), - ("block_reason", models.CharField(verbose_name="Block Reason")), + ( + "block_reason", + models.CharField( + choices=[ + ("HTML_TAGS", "HTML tags detected"), + ("ALTERNATIVE_LANGUAGE", "Alternative Language detected"), + ("BLOCKED_DOMAIN", "Blocked Domain detected"), + ("BLOCKED_USER", "Blocked Email detected"), + ("MODERATOR_DISAPPROVAL", "Moderator disapproval"), + ], + verbose_name="Block Reason", + ), + ), ], options={ "verbose_name": "Blocked Post", diff --git a/lacommunaute/forum_moderation/models.py b/lacommunaute/forum_moderation/models.py index 14bf17b21..0250daf87 100644 --- a/lacommunaute/forum_moderation/models.py +++ b/lacommunaute/forum_moderation/models.py @@ -3,6 +3,8 @@ from django.utils.translation import gettext_lazy as _ from machina.models.abstract_models import DatedModel +from lacommunaute.forum_moderation.enums import BlockedPostReason + class BlockedEmail(DatedModel): email = models.EmailField(verbose_name="email", null=False, blank=False, unique=True) @@ -46,7 +48,7 @@ class BlockedPost(DatedModel): ) username = models.EmailField(null=True, blank=True, verbose_name=("Adresse email")) content = models.CharField(verbose_name=_("Content")) - block_reason = models.CharField(verbose_name=_("Block Reason")) + block_reason = models.CharField(verbose_name=_("Block Reason"), choices=BlockedPostReason) class Meta: verbose_name = _("Blocked Post") diff --git a/lacommunaute/forum_moderation/tests/test_post_disapprove_view.py b/lacommunaute/forum_moderation/tests/test_post_disapprove_view.py index 3db49d364..00fb5afe7 100644 --- a/lacommunaute/forum_moderation/tests/test_post_disapprove_view.py +++ b/lacommunaute/forum_moderation/tests/test_post_disapprove_view.py @@ -24,7 +24,7 @@ def test_post_disapprove_view(client, db): blocked_post = BlockedPost.objects.get() assert blocked_post.content == str(disapproved_post.content) assert blocked_post.username == disapproved_post.username - assert blocked_post.block_reason == BlockedPostReason.MODERATOR_DISAPPROVAL.value + assert blocked_post.block_reason == BlockedPostReason.MODERATOR_DISAPPROVAL.label def test_post_disapprove_view_with_existing_blocked_email(client, db): diff --git a/lacommunaute/forum_moderation/utils.py b/lacommunaute/forum_moderation/utils.py index 7b52c0954..aa85b684c 100644 --- a/lacommunaute/forum_moderation/utils.py +++ b/lacommunaute/forum_moderation/utils.py @@ -18,13 +18,13 @@ def check_post_approbation(post): conditions = [ ( post.username and BlockedDomainName.objects.filter(domain=post.username.split("@")[-1]).exists(), - BlockedPostReason.BLOCKED_DOMAIN.value, + BlockedPostReason.BLOCKED_DOMAIN.label, ), - (Markdown.html_removed_text_compat in rendered, BlockedPostReason.HTML_TAGS.value), - (detect(post.content.raw) not in settings.LANGUAGE_CODE, BlockedPostReason.ALTERNATIVE_LANGUAGE.value), + (Markdown.html_removed_text_compat in rendered, BlockedPostReason.HTML_TAGS.label), + (detect(post.content.raw) not in settings.LANGUAGE_CODE, BlockedPostReason.ALTERNATIVE_LANGUAGE.label), ( post.username and BlockedEmail.objects.filter(email=post.username).exists(), - BlockedPostReason.BLOCKED_USER.value, + BlockedPostReason.BLOCKED_USER.label, ), ] post.approved, post.update_reason = next( diff --git a/lacommunaute/forum_moderation/views.py b/lacommunaute/forum_moderation/views.py index c34d5ef42..17d831830 100644 --- a/lacommunaute/forum_moderation/views.py +++ b/lacommunaute/forum_moderation/views.py @@ -32,7 +32,7 @@ def post(self, request, *args, **kwargs): "l'adresse email de l'utilisateur est déjà dans la liste des emails bloqués.", ) - post.update_reason = BlockedPostReason.MODERATOR_DISAPPROVAL.value + post.update_reason = BlockedPostReason.MODERATOR_DISAPPROVAL.label BlockedPost.create_from_post(post) return self.disapprove(request, *args, **kwargs)