"""
Management utility to create superusers.
"""

import getpass
import os
import re
import sys
from optparse import make_option
from django.core import exceptions
from django.core.management.base import BaseCommand, CommandError
from django.utils.translation import ugettext as _

from seahub.base.accounts import User

RE_VALID_USERNAME = re.compile('[\w.@+-]+$')

EMAIL_RE = re.compile(
    r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*"  # dot-atom
    r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string
    r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE)  # domain

def is_valid_email(value):
    if not EMAIL_RE.search(value):
        raise exceptions.ValidationError(_('Enter a valid e-mail address.'))

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--username', dest='username', default=None,
            help='Specifies the username for the superuser.'),
        make_option('--email', dest='email', default=None,
            help='Specifies the email address for the superuser.'),
        make_option('--noinput', action='store_false', dest='interactive', default=True,
            help='Tells Django to NOT prompt the user for input of any kind. '    \
                 'You must use --username and --email with --noinput, and '      \
                 'superusers created with --noinput will not be able to log in '  \
                 'until they\'re given a valid password.'),
    )
    help = 'Used to create a superuser.'

    def handle(self, *args, **options):
        username = options.get('username', None)
        email = options.get('email', None)
        interactive = options.get('interactive')
        
        # Do quick and dirty validation if --noinput
        if not interactive:
            if not username or not email:
                raise CommandError("You must use --username and --email with --noinput.")
            if not RE_VALID_USERNAME.match(username):
                raise CommandError("Invalid username. Use only letters, digits, and underscores")
            try:
                is_valid_email(email)
            except exceptions.ValidationError:
                raise CommandError("Invalid email address.")

        password = ''

        # Try to determine the current system user's username to use as a default.
        try:
            import pwd
            default_username = pwd.getpwuid(os.getuid())[0].replace(' ', '').lower()
        except (ImportError, KeyError):
            # KeyError will be raised by getpwuid() if there is no
            # corresponding entry in the /etc/passwd file (a very restricted
            # chroot environment, for example).
            default_username = ''

        # Determine whether the default username is taken, so we don't display
        # it as an option.
        if default_username:
            try:
                User.objects.get(email=default_username)
            except User.DoesNotExist:
                pass
            else:
                default_username = ''

        # Prompt for username/email/password. Enclose this whole thing in a
        # try/except to trap for a keyboard interrupt and exit gracefully.
        if interactive:
            try:
            
                # Get a username
                while 1:
                    if not username:
                        input_msg = 'Username'
                        if default_username:
                            input_msg += ' (Leave blank to use %r)' % default_username
                        username = raw_input(input_msg + ': ')
                    if default_username and username == '':
                        username = default_username
                    if not RE_VALID_USERNAME.match(username):
                        sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")
                        username = None
                        continue
                    try:
                        User.objects.get(email=username)
                    except User.DoesNotExist:
                        break
                    else:
                        sys.stderr.write("Error: That username is already taken.\n")
                        username = None
            
                # Get an email
                while 1:
                    if not email:
                        email = raw_input('E-mail address: ')
                    try:
                        is_valid_email(email)
                    except exceptions.ValidationError:
                        sys.stderr.write("Error: That e-mail address is invalid.\n")
                        email = None
                    else:
                        break
            
                # Get a password
                while 1:
                    if not password:
                        password = getpass.getpass()
                        password2 = getpass.getpass('Password (again): ')
                        if password != password2:
                            sys.stderr.write("Error: Your passwords didn't match.\n")
                            password = None
                            continue
                    if password.strip() == '':
                        sys.stderr.write("Error: Blank passwords aren't allowed.\n")
                        password = None
                        continue
                    break
            except KeyboardInterrupt:
                sys.stderr.write("\nOperation cancelled.\n")
                sys.exit(1)
        
        User.objects.create_superuser(email, password)
        print "Superuser created successfully."

@@ -3333,32 +3333,34 @@
 %0A               
+ #
  while 1:%0A      
@@ -3360,32 +3360,34 @@
 %0A               
+ #
      if not user
@@ -3404,32 +3404,33 @@
             
+#
         
 input_msg = 
@@ -3409,32 +3409,33 @@
        #        
+ 
 input_msg = 'Use
@@ -3448,32 +3448,34 @@
 %0A               
+ #
          if defa
@@ -3495,32 +3495,34 @@
 %0A               
+ #
              inp
@@ -3584,32 +3584,34 @@
 %0A               
+ #
          usernam
@@ -3654,28 +3654,30 @@
             
+#
     
+ 
 if default_u
@@ -3716,32 +3716,33 @@
             
+#
         
 username = d
@@ -3721,32 +3721,33 @@
        #        
+ 
 username = defau
@@ -3765,32 +3765,34 @@
 %0A               
+ #
      if not RE_V
@@ -3834,32 +3834,33 @@
             
+#
         
 sys.stderr.w
@@ -3839,32 +3839,33 @@
        #        
+ 
 sys.stderr.write
@@ -3952,32 +3952,34 @@
 %0A               
+ #
          usernam
@@ -3994,32 +3994,34 @@
 %0A               
+ #
          continu
@@ -4029,32 +4029,34 @@
 %0A               
+ #
      try:%0A      
@@ -4056,32 +4056,34 @@
 %0A               
+ #
          User.ob
@@ -4116,36 +4116,38 @@
                 
+#
     
+ 
 except User.Does
@@ -4163,32 +4163,34 @@
 %0A               
+ #
          break%0A 
@@ -4195,32 +4195,34 @@
 %0A               
+ #
      else:%0A     
@@ -4223,32 +4223,34 @@
 %0A               
+ #
          sys.std
@@ -4310,32 +4310,34 @@
 %0A               
+ #
          usernam

Remove unused username in createsuperuser