def clean_username(self): username = self.cleaned_data['username'] if username: try: validate_username(username) except UsernameException, e: raise forms.ValidationError(e.args[0]) return username
def handle(self, *args, **options): if len(args) != 2: raise CommandError('Usage: change_username <old username> <new username>') old = args[0] new = args[1] try: user = User.objects.get(username=old) except User.DoesNotExist: raise CommandError('user %s does not exist' % old) try: validate_username(new) except UsernameInvalid, e: raise CommandError(e.message)
def clean_username(self): username = self.cleaned_data['username'] try: validate_username(username) except UsernameException, e: raise forms.ValidationError(e.message)
def handle(self, *args, **options): password = '' username = '' email = '' institute_name = '' first_name = '' last_name = '' # 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() if default_username == 'root': default_username = '' 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(username=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. 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 try: validate_username(username) break except UsernameException, e: sys.stderr.write("%s\n" % e) username = None continue # 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 while 1: if not first_name: first_name = raw_input('First Name: ') else: break while 1: if not last_name: last_name = raw_input('Last Name: ') else: break if Institute.objects.count() == 0: print "No Institutes in system will create one now" while 1: if not institute_name: institute_name = raw_input('New Institute Name: ') else: break institute = Institute.objects.create(name=institute_name, is_active=True) else: print "Choose an existing institute for new superuser:"******"Valid choices are:" for i in Institute.active.all(): print i while 1: if not institute_name: institute_name = raw_input('Institute Name: ') try: institute = Institute.objects.get(name=institute_name) except Institute.DoesNotExist: sys.stderr.write("Error: Institute does not exist\n") institute_name = None continue break