Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / auth / management / commands / createsuperuser.py
1 """
2 Management utility to create superusers.
3 """
4
5 import getpass
6 import re
7 import sys
8 from optparse import make_option
9
10 from django.contrib.auth.models import User
11 from django.contrib.auth.management import get_default_username
12 from django.core import exceptions
13 from django.core.management.base import BaseCommand, CommandError
14 from django.db import DEFAULT_DB_ALIAS
15 from django.utils.translation import ugettext as _
16
17 RE_VALID_USERNAME = re.compile('[\w.@+-]+$')
18
19 EMAIL_RE = re.compile(
20 r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
21 r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-\011\013\014\016-\177])*"' # quoted-string
22 r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
23
24
25 def is_valid_email(value):
26 if not EMAIL_RE.search(value):
27 raise exceptions.ValidationError(_('Enter a valid e-mail address.'))
28
29
30 class Command(BaseCommand):
31 option_list = BaseCommand.option_list + (
32 make_option('--username', dest='username', default=None,
33 help='Specifies the username for the superuser.'),
34 make_option('--email', dest='email', default=None,
35 help='Specifies the email address for the superuser.'),
36 make_option('--noinput', action='store_false', dest='interactive', default=True,
37 help=('Tells Django to NOT prompt the user for input of any kind. '
38 'You must use --username and --email with --noinput, and '
39 'superusers created with --noinput will not be able to log '
40 'in until they\'re given a valid password.')),
41 make_option('--database', action='store', dest='database',
42 default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".'),
43 )
44 help = 'Used to create a superuser.'
45
46 def handle(self, *args, **options):
47 username = options.get('username', None)
48 email = options.get('email', None)
49 interactive = options.get('interactive')
50 verbosity = int(options.get('verbosity', 1))
51 database = options.get('database')
52
53 # Do quick and dirty validation if --noinput
54 if not interactive:
55 if not username or not email:
56 raise CommandError("You must use --username and --email with --noinput.")
57 if not RE_VALID_USERNAME.match(username):
58 raise CommandError("Invalid username. Use only letters, digits, and underscores")
59 try:
60 is_valid_email(email)
61 except exceptions.ValidationError:
62 raise CommandError("Invalid email address.")
63
64 # If not provided, create the user with an unusable password
65 password = None
66
67 # Prompt for username/email/password. Enclose this whole thing in a
68 # try/except to trap for a keyboard interrupt and exit gracefully.
69 if interactive:
70 default_username = get_default_username()
71 try:
72
73 # Get a username
74 while 1:
75 if not username:
76 input_msg = 'Username'
77 if default_username:
78 input_msg += ' (leave blank to use %r)' % default_username
79 username = raw_input(input_msg + ': ')
80 if default_username and username == '':
81 username = default_username
82 if not RE_VALID_USERNAME.match(username):
83 sys.stderr.write("Error: That username is invalid. Use only letters, digits and underscores.\n")
84 username = None
85 continue
86 try:
87 User.objects.using(database).get(username=username)
88 except User.DoesNotExist:
89 break
90 else:
91 sys.stderr.write("Error: That username is already taken.\n")
92 username = None
93
94 # Get an email
95 while 1:
96 if not email:
97 email = raw_input('E-mail address: ')
98 try:
99 is_valid_email(email)
100 except exceptions.ValidationError:
101 sys.stderr.write("Error: That e-mail address is invalid.\n")
102 email = None
103 else:
104 break
105
106 # Get a password
107 while 1:
108 if not password:
109 password = getpass.getpass()
110 password2 = getpass.getpass('Password (again): ')
111 if password != password2:
112 sys.stderr.write("Error: Your passwords didn't match.\n")
113 password = None
114 continue
115 if password.strip() == '':
116 sys.stderr.write("Error: Blank passwords aren't allowed.\n")
117 password = None
118 continue
119 break
120 except KeyboardInterrupt:
121 sys.stderr.write("\nOperation cancelled.\n")
122 sys.exit(1)
123
124 User.objects.db_manager(database).create_superuser(username, email, password)
125 if verbosity >= 1:
126 self.stdout.write("Superuser created successfully.\n")
127