#NEWLINE# def handle( self , * args , **options ) : #NEWLINE# #INDENT# username = options . get ( self . UserModel . USERNAME_FIELD , None ) #NEWLINE# interactive = options . get ( 'interactive' ) #NEWLINE# verbosity = int ( options . get ( 'verbosity' , 1 ) ) #NEWLINE# database = options . get ( 'database' ) #NEWLINE# password = None #NEWLINE# user_data = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# if ( not interactive ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if ( not username ) : #NEWLINE# #INDENT# raise CommandError ( ( 'You must use --%s with --noinput.' % self . UserModel . USERNAME_FIELD ) ) #NEWLINE# #UNINDENT# username = self . username_field . clean ( username , None ) #NEWLINE# for field_name in self . UserModel . REQUIRED_FIELDS : #NEWLINE# #INDENT# if options . get ( field_name ) : #NEWLINE# #INDENT# field = self . UserModel . _meta . get_field ( field_name ) #NEWLINE# user_data [ field_name ] = field . clean ( options [ field_name ] , None ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise CommandError ( ( 'You must use --%s with --noinput.' % field_name ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except exceptions . ValidationError as e : #NEWLINE# #INDENT# raise CommandError ( '; ' . join ( e . messages ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# default_username = get_default_username ( ) #NEWLINE# try : #NEWLINE# #INDENT# verbose_field_name = self . username_field . verbose_name #NEWLINE# while ( username is None ) : #NEWLINE# #INDENT# if ( not username ) : #NEWLINE# #INDENT# input_msg = capfirst ( verbose_field_name ) #NEWLINE# if default_username : #NEWLINE# #INDENT# input_msg = ( "%s (leave blank to use '%s')" % ( input_msg , default_username ) ) #NEWLINE# #UNINDENT# raw_value = input ( force_str ( ( '%s: ' % input_msg ) ) ) #NEWLINE# #UNINDENT# if ( default_username and ( raw_value == '' ) ) : #NEWLINE# #INDENT# raw_value = default_username #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# username = self . username_field . clean ( raw_value , None ) #NEWLINE# #UNINDENT# except exceptions . ValidationError as e : #NEWLINE# #INDENT# self . stderr . write ( ( 'Error: %s' % '; ' . join ( e . messages ) ) ) #NEWLINE# username = None #NEWLINE# continue #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# self . UserModel . _default_manager . db_manager ( database ) . get_by_natural_key ( username ) #NEWLINE# #UNINDENT# except self . UserModel . DoesNotExist : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . stderr . write ( ( 'Error: That %s is already taken.' % verbose_field_name ) ) #NEWLINE# username = None #NEWLINE# #UNINDENT# #UNINDENT# for field_name in self . UserModel . REQUIRED_FIELDS : #NEWLINE# #INDENT# field = self . UserModel . _meta . get_field ( field_name ) #NEWLINE# user_data [ field_name ] = options . get ( field_name ) #NEWLINE# while ( user_data [ field_name ] is None ) : #NEWLINE# #INDENT# raw_value = input ( force_str ( ( '%s: ' % capfirst ( field . verbose_name ) ) ) ) #NEWLINE# try : #NEWLINE# #INDENT# user_data [ field_name ] = field . clean ( raw_value , None ) #NEWLINE# #UNINDENT# except exceptions . ValidationError as e : #NEWLINE# #INDENT# self . stderr . write ( ( 'Error: %s' % '; ' . join ( e . messages ) ) ) #NEWLINE# user_data [ field_name ] = None #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# while ( verbose_field_name is None ) : #NEWLINE# #INDENT# if ( not password ) : #NEWLINE# #INDENT# password = getpass . getpass ( ) #NEWLINE# password2 = getpass . getpass ( force_str ( 'Password (again): ' ) ) #NEWLINE# if ( password != password2 ) : #NEWLINE# #INDENT# self . stderr . write ( "Error: Your passwords didn't match." ) #NEWLINE# password = None #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# if ( password . strip ( ) == '' ) : #NEWLINE# #INDENT# self . stderr . write ( "Error: Blank passwords aren't allowed." ) #NEWLINE# password = None #NEWLINE# continue #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# except KeyboardInterrupt : #NEWLINE# #INDENT# self . stderr . write ( '\nOperation cancelled.' ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# #UNINDENT# user_data [ self . UserModel . USERNAME_FIELD ] = username #NEWLINE# user_data [ 'password' ] = password #NEWLINE# self . UserModel . _default_manager . db_manager ( database ) . create_superuser ( ** user_data ) #NEWLINE# if ( verbosity >= 1 ) : #NEWLINE# #INDENT# self . stdout . write ( 'Superuser created successfully.' )