From 6947f3b78773613be291566e034bdf9de965b8ac Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Sep 2022 10:43:52 -0700 Subject: [PATCH 1/3] Uses class method to get list of instance admins Re-writing this query over and over is a bad approach --- bookwyrm/models/antispam.py | 5 +---- bookwyrm/models/notification.py | 5 +---- bookwyrm/models/user.py | 8 ++++++++ bookwyrm/views/landing/about.py | 4 +--- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py index dd2a6df26..8c13109e6 100644 --- a/bookwyrm/models/antispam.py +++ b/bookwyrm/models/antispam.py @@ -61,10 +61,7 @@ def automod_task(): if not reports: return - admins = User.objects.filter( - models.Q(user_permissions__name__in=["moderate_user", "moderate_post"]) - | models.Q(is_superuser=True) - ).all() + admins = User.admins() notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True) with transaction.atomic(): for admin in admins: diff --git a/bookwyrm/models/notification.py b/bookwyrm/models/notification.py index b0b75a169..31bf3de60 100644 --- a/bookwyrm/models/notification.py +++ b/bookwyrm/models/notification.py @@ -231,10 +231,7 @@ def notify_admins_on_report(sender, instance, created, *args, **kwargs): return # moderators and superusers should be notified - admins = User.objects.filter( - models.Q(user_permissions__name__in=["moderate_user", "moderate_post"]) - | models.Q(is_superuser=True) - ).all() + admins = User.admins() for admin in admins: notification, _ = Notification.objects.get_or_create( user=admin, diff --git a/bookwyrm/models/user.py b/bookwyrm/models/user.py index f0c3cf04d..8a2b8082f 100644 --- a/bookwyrm/models/user.py +++ b/bookwyrm/models/user.py @@ -231,6 +231,14 @@ class User(OrderedCollectionPageMixin, AbstractUser): queryset = queryset.exclude(blocks=viewer) return queryset + @classmethod + def admins(cls): + """Get a queryset of the admins for this instance""" + return cls.objects.filter( + models.Q(user_permissions__name__in=["moderate_user", "moderate_post"]) + | models.Q(is_superuser=True) + ) + def update_active_date(self): """this user is here! they are doing things!""" self.last_active_date = timezone.now() diff --git a/bookwyrm/views/landing/about.py b/bookwyrm/views/landing/about.py index cd4c4a99b..7016dfcdb 100644 --- a/bookwyrm/views/landing/about.py +++ b/bookwyrm/views/landing/about.py @@ -19,9 +19,7 @@ def about(request): "status_count": models.Status.objects.filter( user__local=True, deleted=False ).count(), - "admins": models.User.objects.filter( - groups__name__in=["admin", "moderator"] - ).distinct(), + "admins": models.User.admins(), "version": settings.VERSION, } From 37dacaff6f0010cdc2d1834af3232785d143523d Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Sep 2022 11:11:08 -0700 Subject: [PATCH 2/3] Fixes creating notifications for auto-moderation reports --- bookwyrm/models/antispam.py | 2 +- bookwyrm/tests/models/test_automod.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/bookwyrm/models/antispam.py b/bookwyrm/models/antispam.py index 8c13109e6..dbc4aa54e 100644 --- a/bookwyrm/models/antispam.py +++ b/bookwyrm/models/antispam.py @@ -68,7 +68,7 @@ def automod_task(): notification, _ = notification_model.objects.get_or_create( user=admin, notification_type=notification_model.REPORT, read=False ) - notification.related_repors.add(reports) + notification.related_reports.set(reports) def automod_users(reporter): diff --git a/bookwyrm/tests/models/test_automod.py b/bookwyrm/tests/models/test_automod.py index abb9aa559..b4e9788e7 100644 --- a/bookwyrm/tests/models/test_automod.py +++ b/bookwyrm/tests/models/test_automod.py @@ -14,6 +14,7 @@ from bookwyrm.models.antispam import automod_task class AutomodModel(TestCase): """every response to a get request, html or json""" + # pylint: disable=invalid-name def setUp(self): """we need basic test data and mocks""" self.factory = RequestFactory() @@ -26,6 +27,7 @@ class AutomodModel(TestCase): "password", local=True, localname="mouse", + is_superuser=True ) def test_automod_task_no_rules(self, *_): @@ -33,6 +35,7 @@ class AutomodModel(TestCase): self.assertFalse(models.Report.objects.exists()) automod_task() self.assertFalse(models.Report.objects.exists()) + self.assertFalse(models.Notification.objects.exists()) def test_automod_task_user(self, *_): """scan activity""" @@ -52,6 +55,7 @@ class AutomodModel(TestCase): reports = models.Report.objects.all() self.assertEqual(reports.count(), 1) self.assertEqual(reports.first().user, self.local_user) + self.assertEqual(models.Notification.objects.count(), 1) def test_automod_status(self, *_): """scan activity""" @@ -73,3 +77,4 @@ class AutomodModel(TestCase): self.assertEqual(reports.count(), 1) self.assertEqual(reports.first().status, status) self.assertEqual(reports.first().user, self.local_user) + self.assertEqual(models.Notification.objects.count(), 1) From 4c099afc63a315bc4c07520dee707ae729dab394 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 19 Sep 2022 11:23:40 -0700 Subject: [PATCH 3/3] Python formatting --- bookwyrm/tests/models/test_automod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookwyrm/tests/models/test_automod.py b/bookwyrm/tests/models/test_automod.py index b4e9788e7..9de7e6488 100644 --- a/bookwyrm/tests/models/test_automod.py +++ b/bookwyrm/tests/models/test_automod.py @@ -27,7 +27,7 @@ class AutomodModel(TestCase): "password", local=True, localname="mouse", - is_superuser=True + is_superuser=True, ) def test_automod_task_no_rules(self, *_):