mirror of
https://github.com/zulip/zulip.git
synced 2025-11-16 03:41:58 +00:00
Introduce new manage.py command which creates users with default passwords.
(imported from commit ba5ed9cb6ee91435b184845019391e5dc38fc3aa)
This commit is contained in:
48
zephyr/management/commands/create_user.py
Normal file
48
zephyr/management/commands/create_user.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import sys
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.utils import IntegrityError
|
||||
from django.utils.timezone import now
|
||||
from django.core import validators
|
||||
|
||||
from zephyr.models import Realm, do_create_user
|
||||
from zephyr.views import do_send_message
|
||||
from zephyr.lib.initial_password import initial_password
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Create the specified user with a default initial password."
|
||||
|
||||
def handle(self, *args, **options):
|
||||
try:
|
||||
email, full_name = args
|
||||
try:
|
||||
validators.validate_email(email)
|
||||
except ValidationError:
|
||||
raise CommandError("Invalid email address.")
|
||||
except ValueError:
|
||||
if len(args) != 0:
|
||||
raise CommandError("Either specify an email and full name" + \
|
||||
"as two parameters, or specify no parameters for" + \
|
||||
"interactive user creation.")
|
||||
return 1
|
||||
else:
|
||||
while True:
|
||||
email = raw_input("Email: ")
|
||||
try:
|
||||
validators.validate_email(email)
|
||||
break
|
||||
except ValidationError:
|
||||
print >> sys.stderr, "Invalid email address."
|
||||
full_name = raw_input("Full name: ")
|
||||
|
||||
try:
|
||||
realm = Realm.objects.get(domain=email.split('@')[-1])
|
||||
except Realm.DoesNotExist:
|
||||
raise CommandError("Realm does not exist.")
|
||||
|
||||
try:
|
||||
do_create_user(email, initial_password(email), realm, full_name,
|
||||
email.split('@')[0])
|
||||
except IntegrityError:
|
||||
raise CommandError("User already exists.")
|
||||
Reference in New Issue
Block a user