Skip to content

Implement list_staff_users based on list_superusers #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ Run `./manage.py check --deploy` to execute these checks (in addition to Django'
Run `./manage.py list_superusers` to print a list of all (in)active superusers.

The name of each superuser is printed with a "+" (active) or "-" (inactive) prefix.

## List staff users

Run `./manage.py list_staff_users` to print a list of all (in)active staff users.

The name of each staff user is printed with a "+" (active) or "-" (inactive) prefix.
34 changes: 34 additions & 0 deletions leukeleu_django_checks/management/commands/list_staff_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand

STATUS_FLAGS = {
True: "+",
False: "-",
}


def get_name(user, email_field):
name = user.get_full_name() or getattr(user, email_field) or user.get_username()
return name.partition("@")[0]


class Command(BaseCommand):
def handle(self, *args, **options):
try:
user_model = get_user_model()
except ImproperlyConfigured:
return

if not hasattr(user_model, "is_staff"):
return

staff_users = user_model.objects.filter(is_staff=True)
email_field = user_model.get_email_field_name()

name_status_list = sorted(
((get_name(user, email_field), user.is_active) for user in staff_users),
key=lambda item: item[0].casefold(),
)
for name, is_active in name_status_list:
self.stdout.write(f"{STATUS_FLAGS[is_active]}{name}")
6 changes: 5 additions & 1 deletion tests/custom_users/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from django.contrib.auth.base_user import AbstractBaseUser


class NoSuperUser(AbstractBaseUser):
class NoSuperuser(AbstractBaseUser):
pass


class NoStaff(AbstractBaseUser):
pass
67 changes: 67 additions & 0 deletions tests/test_list_staff_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from io import StringIO

from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.test import TestCase, override_settings


class TestListStaffUsersNoAuth(TestCase):
"""Does nothing when django.contrib.auth is not in INSTALLED_APPS"""

available_apps = [
"leukeleu_django_checks",
]

def test_no_user_model(self):
out = StringIO()
call_command("list_staff_users", stdout=out, stderr=out)
self.assertEqual(out.getvalue(), "")


@override_settings(AUTH_USER_MODEL="custom_users.NoStaff")
class TestListStaffUsersCustomUserModel(TestCase):
"""Does nothing if AUTH_USER_MODEL does not have an is_staff field"""

def test_no_staff_field(self):
user_model = get_user_model()
user_model.objects.create()

out = StringIO()
call_command("list_staff_users", stdout=out, stderr=out)
self.assertEqual(out.getvalue(), "")


class TestListStaffUsersStandardUserModel(TestCase):
def test_default_user_model(self):
user_model = get_user_model()

user_model.objects.create(
username="user",
)
user_model.objects.create(
username="staff",
email="staff.user@example.com",
is_staff=True,
)
john = user_model.objects.create(
username="john",
email="john.smith@example.com",
first_name="John",
last_name="Smith",
is_staff=True,
)
john.is_active = False
john.save()

out = StringIO()
call_command("list_staff_users", stdout=out, stderr=out)
self.assertEqual(
out.getvalue(),
"\n".join(
[
"-John Smith", # Prefers get_full_name
"+staff.user", # Uses email field, removes domain part.
"", # blank line
]
),
)
2 changes: 1 addition & 1 deletion tests/test_list_superusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_no_user_model(self):
self.assertEqual(out.getvalue(), "")


@override_settings(AUTH_USER_MODEL="custom_users.NoSuperUser")
@override_settings(AUTH_USER_MODEL="custom_users.NoSuperuser")
class TestListSuperusersCustomUserModel(TestCase):
"""Does nothing if AUTH_USER_MODEL does not have an is_superuser field"""

Expand Down