Uses class method to get list of instance admins

Re-writing this query over and over is a bad approach
This commit is contained in:
Mouse Reeve 2022-09-19 10:43:52 -07:00
parent ac7be43e82
commit 6947f3b787
4 changed files with 11 additions and 11 deletions

View file

@ -61,10 +61,7 @@ def automod_task():
if not reports: if not reports:
return return
admins = User.objects.filter( admins = User.admins()
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) notification_model = apps.get_model("bookwyrm", "Notification", require_ready=True)
with transaction.atomic(): with transaction.atomic():
for admin in admins: for admin in admins:

View file

@ -231,10 +231,7 @@ def notify_admins_on_report(sender, instance, created, *args, **kwargs):
return return
# moderators and superusers should be notified # moderators and superusers should be notified
admins = User.objects.filter( admins = User.admins()
models.Q(user_permissions__name__in=["moderate_user", "moderate_post"])
| models.Q(is_superuser=True)
).all()
for admin in admins: for admin in admins:
notification, _ = Notification.objects.get_or_create( notification, _ = Notification.objects.get_or_create(
user=admin, user=admin,

View file

@ -231,6 +231,14 @@ class User(OrderedCollectionPageMixin, AbstractUser):
queryset = queryset.exclude(blocks=viewer) queryset = queryset.exclude(blocks=viewer)
return queryset 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): def update_active_date(self):
"""this user is here! they are doing things!""" """this user is here! they are doing things!"""
self.last_active_date = timezone.now() self.last_active_date = timezone.now()

View file

@ -19,9 +19,7 @@ def about(request):
"status_count": models.Status.objects.filter( "status_count": models.Status.objects.filter(
user__local=True, deleted=False user__local=True, deleted=False
).count(), ).count(),
"admins": models.User.objects.filter( "admins": models.User.admins(),
groups__name__in=["admin", "moderator"]
).distinct(),
"version": settings.VERSION, "version": settings.VERSION,
} }