2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-06 23:26:47 +01:00
|
|
|
import sys
|
2013-01-08 23:05:42 +01:00
|
|
|
from optparse import make_option
|
2012-12-06 23:26:47 +01:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.db.utils import IntegrityError
|
|
|
|
from django.core import validators
|
|
|
|
|
2013-01-10 22:01:33 +01:00
|
|
|
from zephyr.models import Realm
|
2013-03-08 20:24:09 +01:00
|
|
|
from zephyr.lib.actions import do_create_user
|
2013-01-10 22:01:33 +01:00
|
|
|
from zephyr.views import notify_new_user
|
2012-12-06 23:26:47 +01:00
|
|
|
from zephyr.lib.initial_password import initial_password
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2013-01-08 23:05:42 +01:00
|
|
|
help = """Create the specified user with a default initial password.
|
|
|
|
|
|
|
|
A user MUST have ALREADY accepted the Terms of Service before creating their
|
|
|
|
account this way.
|
|
|
|
"""
|
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('--this-user-has-accepted-the-tos',
|
|
|
|
dest='tos',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Acknowledgement that the user has already accepted the ToS.'),
|
2013-03-21 19:07:03 +01:00
|
|
|
make_option('--domain',
|
|
|
|
dest='domain',
|
|
|
|
type='str',
|
|
|
|
help='The name of the existing realm to which to add the user.'),
|
2013-01-08 23:05:42 +01:00
|
|
|
)
|
2012-12-06 23:26:47 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2013-01-08 23:05:42 +01:00
|
|
|
if not options["tos"]:
|
|
|
|
raise CommandError("""You must confirm that this user has accepted the
|
|
|
|
Terms of Service by passing --this-user-has-accepted-the-tos.""")
|
|
|
|
|
2013-03-21 19:07:03 +01:00
|
|
|
if not options["domain"]:
|
|
|
|
raise CommandError("""Please specify a realm by passing --domain.""")
|
|
|
|
|
|
|
|
try:
|
|
|
|
realm = Realm.objects.get(domain=options["domain"])
|
|
|
|
except Realm.DoesNotExist:
|
|
|
|
raise CommandError("Realm does not exist.")
|
|
|
|
|
2012-12-06 23:26:47 +01:00
|
|
|
try:
|
|
|
|
email, full_name = args
|
|
|
|
try:
|
|
|
|
validators.validate_email(email)
|
|
|
|
except ValidationError:
|
|
|
|
raise CommandError("Invalid email address.")
|
|
|
|
except ValueError:
|
|
|
|
if len(args) != 0:
|
2013-01-08 23:08:13 +01:00
|
|
|
raise CommandError("""Either specify an email and full name as two
|
|
|
|
parameters, or specify no parameters for interactive user creation.""")
|
2012-12-06 23:26:47 +01:00
|
|
|
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:
|
2012-12-07 00:02:53 +01:00
|
|
|
notify_new_user(do_create_user(email, initial_password(email),
|
|
|
|
realm, full_name, email.split('@')[0]),
|
|
|
|
internal=True)
|
2012-12-06 23:26:47 +01:00
|
|
|
except IntegrityError:
|
|
|
|
raise CommandError("User already exists.")
|