From dc8e61f316e13360edc5d3fbcf8a8ab9d84eb4bd Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 4 Jul 2022 19:57:58 -0700 Subject: [PATCH] Updates reports created in automod task --- bookwyrm/models/antispam.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py index f506b6f19..061659026 100644 --- a/bookwyrm/models/antispam.py +++ b/bookwyrm/models/antispam.py @@ -3,7 +3,7 @@ from functools import reduce import operator from django.apps import apps -from django.db import models +from django.db import models, transaction from django.db.models import Q from django.utils.translation import gettext_lazy as _ @@ -58,25 +58,20 @@ def automod_task(): return reporter = AutoMod.objects.first().created_by reports = automod_users(reporter) + automod_statuses(reporter) - if reports: - admins = User.objects.filter( - models.Q(user_permissions__name__in=["moderate_user", "moderate_post"]) - | models.Q(is_superuser=True) - ).all() - notification_model = apps.get_model( - "bookwyrm", "Notification", require_ready=True - ) + if not reports: + return + + admins = User.objects.filter( + models.Q(user_permissions__name__in=["moderate_user", "moderate_post"]) + | models.Q(is_superuser=True) + ).all() + notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True) + with transaction.atomic(): for admin in admins: - notification_model.objects.bulk_create( - [ - notification_model( - user=admin, - related_report=r, - notification_type="REPORT", - ) - for r in reports - ] + notification, _ = notification_model.objects.get_or_create( + user=admin, notification_type="REPORT", unread=True ) + notification.related_repors.add(reports) def automod_users(reporter):