mirror of https://github.com/snachodog/mybuddy.git
feat: Add group argument to createuser command
This commit is contained in:
parent
6816349103
commit
400ad66968
|
@ -7,12 +7,12 @@ Example usage:
|
||||||
manage.py createuser \
|
manage.py createuser \
|
||||||
--username test \
|
--username test \
|
||||||
--email test@test.test \
|
--email test@test.test \
|
||||||
--is-staff
|
--group staff
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
import getpass
|
import getpass
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model, models
|
||||||
from django.contrib.auth.password_validation import validate_password
|
from django.contrib.auth.password_validation import validate_password
|
||||||
from django.core import exceptions
|
from django.core import exceptions
|
||||||
from django.core.management.base import BaseCommand, CommandError
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
@ -52,19 +52,21 @@ class Command(BaseCommand):
|
||||||
help="Specifies the password for the user. Optional.",
|
help="Specifies the password for the user. Optional.",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--is-staff",
|
"--group",
|
||||||
dest="is_staff",
|
dest="groups",
|
||||||
action="store_true",
|
choices=("read_only", "standard", "staff"),
|
||||||
default=False,
|
default="read_only",
|
||||||
help="Specifies the staff status for the user. Default is False.",
|
help="Specifies the group a user belongs to. Default is read_only.",
|
||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
username = options.get(self.UserModel.USERNAME_FIELD)
|
username = options.get(self.UserModel.USERNAME_FIELD)
|
||||||
password = options.get("password")
|
password = options.get("password")
|
||||||
|
group_name = options.get("groups", "read_only")
|
||||||
|
|
||||||
user_data = {}
|
user_data = {}
|
||||||
user_password = ""
|
user_password = ""
|
||||||
|
group = ""
|
||||||
verbose_field_name = self.username_field.verbose_name
|
verbose_field_name = self.username_field.verbose_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -75,6 +77,7 @@ class Command(BaseCommand):
|
||||||
raise CommandError(error_msg)
|
raise CommandError(error_msg)
|
||||||
|
|
||||||
user_data[self.UserModel.USERNAME_FIELD] = username
|
user_data[self.UserModel.USERNAME_FIELD] = username
|
||||||
|
group = self._validate_group(group_name)
|
||||||
|
|
||||||
# Prompt for a password interactively (if password not set via arg)
|
# Prompt for a password interactively (if password not set via arg)
|
||||||
while password is None:
|
while password is None:
|
||||||
|
@ -112,10 +115,10 @@ class Command(BaseCommand):
|
||||||
DEFAULT_DB_ALIAS
|
DEFAULT_DB_ALIAS
|
||||||
).create_user(**user_data, password=user_password)
|
).create_user(**user_data, password=user_password)
|
||||||
user.email = options.get("email")
|
user.email = options.get("email")
|
||||||
user.is_staff = options.get("is_staff")
|
if group_name == "staff":
|
||||||
# All Baby Buddy users are superusers.
|
user.is_staff = True
|
||||||
user.is_superuser = True
|
|
||||||
user.save()
|
user.save()
|
||||||
|
user.groups.add(group)
|
||||||
|
|
||||||
if options.get("verbosity") > 0:
|
if options.get("verbosity") > 0:
|
||||||
self.stdout.write(f"User {username} created successfully.")
|
self.stdout.write(f"User {username} created successfully.")
|
||||||
|
@ -164,3 +167,12 @@ class Command(BaseCommand):
|
||||||
self.username_field.clean(username, None)
|
self.username_field.clean(username, None)
|
||||||
except exceptions.ValidationError as e:
|
except exceptions.ValidationError as e:
|
||||||
return "; ".join(e.messages)
|
return "; ".join(e.messages)
|
||||||
|
|
||||||
|
def _validate_group(self, group_name):
|
||||||
|
"""
|
||||||
|
Validate user group. If invalid, raise an error.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return models.Group.objects.get(name=group_name)
|
||||||
|
except models.Group.DoesNotExist as e:
|
||||||
|
raise CommandError(e)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from django.test import TransactionTestCase
|
from django.test import TransactionTestCase
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User, Group
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
|
|
||||||
from core.models import Child
|
from core.models import Child
|
||||||
|
@ -24,6 +24,9 @@ class CommandsTestCase(TransactionTestCase):
|
||||||
self.assertEqual(Child.objects.count(), 1)
|
self.assertEqual(Child.objects.count(), 1)
|
||||||
|
|
||||||
def test_createuser(self):
|
def test_createuser(self):
|
||||||
|
Group.objects.create(name="read_only")
|
||||||
|
Group.objects.create(name="staff")
|
||||||
|
|
||||||
call_command(
|
call_command(
|
||||||
"createuser",
|
"createuser",
|
||||||
username="test",
|
username="test",
|
||||||
|
@ -35,10 +38,10 @@ class CommandsTestCase(TransactionTestCase):
|
||||||
self.assertFalse(User.objects.filter(username="test", is_staff=True))
|
self.assertFalse(User.objects.filter(username="test", is_staff=True))
|
||||||
call_command(
|
call_command(
|
||||||
"createuser",
|
"createuser",
|
||||||
"--is-staff",
|
|
||||||
username="testadmin",
|
username="testadmin",
|
||||||
email="testadmin@testadmin.testadmin",
|
email="testadmin@testadmin.testadmin",
|
||||||
password="test",
|
password="test",
|
||||||
|
group="staff",
|
||||||
verbosity=0,
|
verbosity=0,
|
||||||
)
|
)
|
||||||
self.assertIsInstance(User.objects.get(username="testadmin"), User)
|
self.assertIsInstance(User.objects.get(username="testadmin"), User)
|
||||||
|
|
Loading…
Reference in New Issue