-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcreate_user.py
More file actions
43 lines (34 loc) · 1.35 KB
/
create_user.py
File metadata and controls
43 lines (34 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import os
import sys
import click
import django
from django.core.exceptions import ValidationError
@click.command(name="create-user")
@click.option("--email", prompt=True, help="Email address for the user")
@click.option("--password", prompt=True, hide_input=True, confirmation_prompt=True, help="Password for the user")
def create_user(email, password):
project_name = os.getenv("PROJECT_NAME")
if not project_name:
raise click.ClickException("PROJECT_NAME environment variable not set.")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", f"{project_name}.settings")
sys.path.insert(0, os.getcwd())
django.setup()
from zango.apps.shared.platformauth.models import PlatformUserModel
click.echo(f"Creating user with email: {email}")
try:
result = PlatformUserModel.create_user(
name="CLI Created User",
email=email,
password=password,
is_superadmin=True,
mobile="",
require_verification=False,
)
if result["success"]:
click.echo("User created successfully.")
else:
click.echo(f"Failed to create user: {result['message']}")
except ValidationError as e:
raise click.ClickException(f"Validation Error: {str(e)}")
except Exception as e:
raise click.ClickException(f"Error: {str(e)}")