Remove group arg and add --read-only flag to createuser command

This commit is contained in:
earthcomfy 2022-10-31 08:24:20 +03:00 committed by Christopher Charbonneau Wells
parent 6992ddc911
commit 6f83611f42
2 changed files with 20 additions and 16 deletions

View File

@ -7,7 +7,6 @@ Example usage:
manage.py createuser \ manage.py createuser \
--username test \ --username test \
--email test@test.test \ --email test@test.test \
--group read_only
""" """
import sys import sys
import getpass import getpass
@ -52,11 +51,10 @@ 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(
"--group", "--read-only",
dest="groups", action="store_true",
choices=("read_only", "standard"), default=False,
default="standard", help="Specifies read-only privileges for the user. Default is False.",
help="Specifies the group a user belongs to. Default is standard.",
) )
parser.add_argument( parser.add_argument(
"--is-staff", "--is-staff",
@ -69,7 +67,8 @@ class Command(BaseCommand):
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", "standard") is_group_read_only = options.get("read_only")
is_staff = options.get("is_staff")
user_data = {} user_data = {}
user_password = "" user_password = ""
@ -84,7 +83,13 @@ 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)
if is_staff and is_group_read_only:
raise CommandError(
"You cannot set both read_only and is_staff flags simultaneously."
)
group = self.get_user_group(is_group_read_only)
# 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:
@ -178,11 +183,10 @@ class Command(BaseCommand):
except exceptions.ValidationError as e: except exceptions.ValidationError as e:
return "; ".join(e.messages) return "; ".join(e.messages)
def _validate_group(self, group_name): def get_user_group(self, is_group_read_only):
""" """
Validate user group. If invalid, raise an error. Returns the group a user belongs to depnding on the '--read-only' flag
""" """
try: if is_group_read_only:
return models.Group.objects.get(name=group_name) return models.Group.objects.get(name="read_only")
except models.Group.DoesNotExist as e: return models.Group.objects.get(name="standard")
raise CommandError(e)

View File

@ -44,10 +44,10 @@ python manage.py createuser --username <username>
You will then be prompted to enter and confirm a password. You will then be prompted to enter and confirm a password.
- If you want to create a user with read only privileges, pass in the `read_only` value to the `group` arg: - If you want to create a user with read only privileges, pass in the `--read_only` flag:
```shell ```shell
python manage.py createuser --username <username> --password <password> --group read_only python manage.py createuser --username <username> --password <password> --read-only
``` ```
- If you want to create a user with the highest level of permission, you can append the `--is-staff` argument: - If you want to create a user with the highest level of permission, you can append the `--is-staff` argument: