Skip to content

Commit

Permalink
refactor(BlockedPostReason): remplace Enum avec models.TextChoices
Browse files Browse the repository at this point in the history
  • Loading branch information
calummackervoy committed Jun 11, 2024
1 parent 8b5ce01 commit 9b46db4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lacommunaute/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lacommunaute/forum_conversation/tests/tests_views_htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 == "[email protected]"
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN.value
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN.label


class CertifiedPostViewTest(TestCase):
Expand Down
22 changes: 11 additions & 11 deletions lacommunaute/forum_moderation/enums.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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,
]
14 changes: 13 additions & 1 deletion lacommunaute/forum_moderation/migrations/0003_blockedpost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion lacommunaute/forum_moderation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions lacommunaute/forum_moderation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion lacommunaute/forum_moderation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9b46db4

Please sign in to comment.