Skip to content
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

Add create user command #38

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
33 changes: 33 additions & 0 deletions terra_accounts/management/commands/createuser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from django.conf import settings
from django.core.management import BaseCommand


class Command(BaseCommand):
help = "Create regular user"

def add_arguments(self, parser):
parser.add_argument("email", help="The user's e-mail address")
parser.add_argument("password", help="The user's password")
parser.add_argument("group", help="The user's group")

def handle(self, **options):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function handle has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.

user_data = {
"email": options.get("email"),
"password": options.get("password"),
}

TerraUser = get_user_model()

if not TerraUser.objects.filter(email=user_data['email']).exists():
user = TerraUser.objects.create_user(**user_data)

groups = {}
user_group = options.get("group")
if user_group:
if not groups.get(user_group):
groups[user_group], _ = Group.objects.get_or_create(name=user_group)
user.groups.add(groups[user_group])