Skip to content

Commit

Permalink
fix(forum_moderation): motif de blocage illisible dans l'admin (#672)
Browse files Browse the repository at this point in the history
## Description

🎸 Validation complete de `blocked_reason` pendant la conversion d'une
poste
🎸 Migration pour les objets existants

## Type de changement

🪲 Correction de bug (changement non cassant qui corrige un problème).

### Captures d'écran (optionnel)

<img width="1042" alt="Screenshot 2024-06-12 at 13 32 31"
src="https://github.com/gip-inclusion/itou-communaute-django/assets/10801930/9ce046f0-875f-4f45-bf56-76db9693006c">
  • Loading branch information
calummackervoy authored Jun 13, 2024
1 parent 6f8bf3c commit 5480394
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
5 changes: 3 additions & 2 deletions lacommunaute/forum_conversation/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def clean(self):
if self.user.is_authenticated:
post.poster = self.user

if post.update_reason in [x.label for x in BlockedPostReason.reasons_tracked_for_stats()]:
BlockedPost.create_from_post(post)
blocked_reason = BlockedPostReason.from_label(post.update_reason)
if blocked_reason in BlockedPostReason.reasons_tracked_for_stats():
BlockedPost.create_from_post(post, blocked_reason)
return cleaned_data

def update_post(self, post):
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 @@ -322,7 +322,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.label
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_USER

def test_create_post_with_nonfr_content(self):
assign_perm("can_reply_to_topics", self.user, self.topic.forum)
Expand Down Expand Up @@ -350,7 +350,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.label
assert blocked_post.block_reason == BlockedPostReason.ALTERNATIVE_LANGUAGE

def test_create_post_with_html_content(self):
assign_perm("can_reply_to_topics", self.user, self.topic.forum)
Expand Down Expand Up @@ -408,7 +408,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.label
assert blocked_post.block_reason == BlockedPostReason.BLOCKED_DOMAIN


class CertifiedPostViewTest(TestCase):
Expand Down
4 changes: 4 additions & 0 deletions lacommunaute/forum_moderation/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ def reasons_tracked_for_stats(cls):
cls.BLOCKED_USER,
cls.MODERATOR_DISAPPROVAL,
]

@classmethod
def from_label(cls, label):
return cls(cls.values[cls.labels.index(label)]) if label in cls.labels else None
7 changes: 5 additions & 2 deletions lacommunaute/forum_moderation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ def __str__(self):
return f"Blocked Message [{ str(self.created) }]"

@classmethod
def create_from_post(cls, post):
def create_from_post(cls, post, reason: BlockedPostReason):
"""
Creates a BlockedPost object from parameterised Post (machina)
"""
if type(reason) != BlockedPostReason:
raise TypeError("Reason must be a BlockedPostReason")

return cls.objects.create(
poster=post.poster,
username=getattr(post, "username", ""),
content=str(post.content),
block_reason=post.update_reason,
block_reason=reason,
)
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.label
assert blocked_post.block_reason == BlockedPostReason.MODERATOR_DISAPPROVAL


def test_post_disapprove_view_with_existing_blocked_email(client, db):
Expand Down
3 changes: 1 addition & 2 deletions lacommunaute/forum_moderation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ 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.label
BlockedPost.create_from_post(post)
BlockedPost.create_from_post(post, BlockedPostReason.MODERATOR_DISAPPROVAL)

return self.disapprove(request, *args, **kwargs)

0 comments on commit 5480394

Please sign in to comment.