Skip to content

Commit

Permalink
Move create_debug_users into a management command
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonGrace2282 committed Oct 8, 2024
1 parent ed80196 commit 212b674
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/source/contributing/setup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ After that, install dependencies and follow standard django procedures
pipenv install --dev
pipenv run python3 manage.py migrate
pipenv run python3 scripts/create_debug_users.py
pipenv run python3 manage.py create_debug_users
Now you're all set! Try running the development server
Expand Down
22 changes: 0 additions & 22 deletions scripts/create_debug_users.py

This file was deleted.

28 changes: 28 additions & 0 deletions tin/apps/users/management/commands/create_debug_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

import contextlib
from getpass import getpass

from django.core.management.base import BaseCommand

import tin.tests.create_users as users


class Command(BaseCommand):
help = "Create users for debugging"

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")

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"])
4 changes: 2 additions & 2 deletions tin/tests/create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# fmt: on


def add_users_to_database(password: str, *, verbose: bool = True) -> None:
def add_users_to_database(password: str, *, force: bool = False, verbose: bool = True) -> None:
User = get_user_model()

for (
Expand All @@ -26,7 +26,7 @@ def add_users_to_database(password: str, *, verbose: bool = True) -> None:
) in user_data:
user, created = User.objects.get_or_create(username=username)

if not created:
if not created and not force:
if verbose:
print(f"User {username} already exists, skipping...")
continue
Expand Down

0 comments on commit 212b674

Please sign in to comment.