django.contrib.auth.management.commands.createsuperuser: 83 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py

Stats: 0 executed, 73 missed, 10 excluded, 44 ignored

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