Merge pull request #2405 from bookwyrm-social/admins-list

Fixes collecting list of admins
This commit is contained in:
Mouse Reeve 2022-11-17 15:22:10 -08:00 committed by GitHub
commit d36521c460
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 5 deletions

View file

@ -244,9 +244,10 @@ class User(OrderedCollectionPageMixin, AbstractUser):
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)
)
models.Q(groups__name__in=["moderator", "admin"])
| models.Q(is_superuser=True),
is_active=True,
).distinct()
def update_active_date(self):
"""this user is here! they are doing things!"""

View file

@ -11,7 +11,7 @@
{% block about_content %}
{# seven day cache #}
{% cache 604800 about_page %}
{% cache 604800 about_page_superlatives %}
{% get_book_superlatives as superlatives %}
<section class=" pb-4">
@ -97,6 +97,7 @@
</p>
</section>
{% endcache %}
<section class="block">
<header class="content">
@ -145,5 +146,4 @@
</div>
</section>
{% endcache %}
{% endblock %}

View file

@ -1,10 +1,12 @@
""" testing models """
import json
from unittest.mock import patch
from django.contrib.auth.models import Group
from django.test import TestCase
import responses
from bookwyrm import models
from bookwyrm.management.commands import initdb
from bookwyrm.settings import USE_HTTPS, DOMAIN
# pylint: disable=missing-class-docstring
@ -12,6 +14,7 @@ from bookwyrm.settings import USE_HTTPS, DOMAIN
class User(TestCase):
protocol = "https://" if USE_HTTPS else "http://"
# pylint: disable=invalid-name
def setUp(self):
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
@ -25,6 +28,17 @@ class User(TestCase):
name="hi",
bookwyrm_user=False,
)
self.another_user = models.User.objects.create_user(
f"nutria@{DOMAIN}",
"nutria@nutria.nutria",
"nutriaword",
local=True,
localname="nutria",
name="hi",
bookwyrm_user=False,
)
initdb.init_groups()
initdb.init_permissions()
def test_computed_fields(self):
"""username instead of id here"""
@ -176,3 +190,41 @@ class User(TestCase):
self.assertEqual(activity["type"], "Delete")
self.assertEqual(activity["object"], self.user.remote_id)
self.assertFalse(self.user.is_active)
def test_admins_no_admins(self):
"""list of admins"""
result = models.User.admins()
self.assertFalse(result.exists())
def test_admins_superuser(self):
"""list of admins"""
self.user.is_superuser = True
self.user.save(broadcast=False, update_fields=["is_superuser"])
result = models.User.admins()
self.assertEqual(result.count(), 1)
self.assertEqual(result.first(), self.user)
def test_admins_superuser_and_mod(self):
"""list of admins"""
self.user.is_superuser = True
self.user.save(broadcast=False, update_fields=["is_superuser"])
group = Group.objects.get(name="moderator")
self.another_user.groups.set([group])
results = models.User.admins()
self.assertEqual(results.count(), 2)
self.assertTrue(results.filter(id=self.user.id).exists())
self.assertTrue(results.filter(id=self.another_user.id).exists())
def test_admins_deleted_mod(self):
"""list of admins"""
self.user.is_superuser = True
self.user.save(broadcast=False, update_fields=["is_superuser"])
group = Group.objects.get(name="moderator")
self.another_user.groups.set([group])
self.another_user.is_active = False
self.another_user.save(broadcast=False, update_fields=None)
results = models.User.admins()
self.assertEqual(results.count(), 1)
self.assertEqual(results.first(), self.user)