From 981d04528ce7d88a8affbf68aee153aeb6b588c3 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Sun, 13 Oct 2024 23:00:29 -0400 Subject: [PATCH] Better support for django-admin options verbosity --- .../management/commands/create_debug_users.py | 14 +++++++------- tin/apps/users/tests.py | 11 +++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 tin/apps/users/tests.py diff --git a/tin/apps/users/management/commands/create_debug_users.py b/tin/apps/users/management/commands/create_debug_users.py index 9f5b6f75..b888e86c 100644 --- a/tin/apps/users/management/commands/create_debug_users.py +++ b/tin/apps/users/management/commands/create_debug_users.py @@ -1,9 +1,8 @@ from __future__ import annotations -import contextlib from getpass import getpass -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, no_translations import tin.tests.create_users as users @@ -15,14 +14,15 @@ def add_arguments(self, parser): parser.add_argument("--noinput", action="store_true", help="Do not ask for password") parser.add_argument("--force", action="store_true", help="Force creation of users") + @no_translations def handle(self, *args, **options): if not options["noinput"]: pwd = getpass("Enter password for all users: ") else: pwd = "jasongrace" - with ( - contextlib.redirect_stdout(self.stdout), # type: ignore[misc] - contextlib.redirect_stderr(self.stderr), # type: ignore[misc] - ): - users.add_users_to_database(password=pwd, verbose=True, force=options["force"]) + users.add_users_to_database( + password=pwd, + verbose=options["verbosity"] > 0, + force=options["force"], + ) diff --git a/tin/apps/users/tests.py b/tin/apps/users/tests.py new file mode 100644 index 00000000..8e6b7071 --- /dev/null +++ b/tin/apps/users/tests.py @@ -0,0 +1,11 @@ +from __future__ import annotations + +from django.contrib.auth import get_user_model +from django.core.management import call_command + + +def test_create_debug_users(): + call_command("create_debug_users", noinput=True, verbosity=0) + assert get_user_model().objects.filter(username="admin", is_superuser=True) + assert get_user_model().objects.filter(username="student", is_student=True) + assert get_user_model().objects.filter(username="teacher", is_teacher=True)