django.contrib.auth.management: 64 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/__init__.py

Stats: 0 executed, 55 missed, 9 excluded, 63 ignored

  1. """
  2. Creates permissions for all installed apps that need permissions.
  3. """
  4. import getpass
  5. import locale
  6. import unicodedata
  7. from django.contrib.auth import models as auth_app
  8. from django.db.models import get_models, signals
  9. from django.contrib.auth.models import User
  10. def _get_permission_codename(action, opts):
  11. return u'%s_%s' % (action, opts.object_name.lower())
  12. def _get_all_permissions(opts):
  13. "Returns (codename, name) for all permissions in the given opts."
  14. perms = []
  15. for action in ('add', 'change', 'delete'):
  16. perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
  17. return perms + list(opts.permissions)
  18. def create_permissions(app, created_models, verbosity, **kwargs):
  19. from django.contrib.contenttypes.models import ContentType
  20. app_models = get_models(app)
  21. # This will hold the permissions we're looking for as
  22. # (content_type, (codename, name))
  23. searched_perms = list()
  24. # The codenames and ctypes that should exist.
  25. ctypes = set()
  26. for klass in app_models:
  27. ctype = ContentType.objects.get_for_model(klass)
  28. ctypes.add(ctype)
  29. for perm in _get_all_permissions(klass._meta):
  30. searched_perms.append((ctype, perm))
  31. # Find all the Permissions that have a context_type for a model we're
  32. # looking for. We don't need to check for codenames since we already have
  33. # a list of the ones we're going to create.
  34. all_perms = set(auth_app.Permission.objects.filter(
  35. content_type__in=ctypes,
  36. ).values_list(
  37. "content_type", "codename"
  38. ))
  39. objs = [
  40. auth_app.Permission(codename=codename, name=name, content_type=ctype)
  41. for ctype, (codename, name) in searched_perms
  42. if (ctype.pk, codename) not in all_perms
  43. ]
  44. auth_app.Permission.objects.bulk_create(objs)
  45. if verbosity >= 2:
  46. for obj in objs:
  47. print "Adding permission '%s'" % obj
  48. def create_superuser(app, created_models, verbosity, db, **kwargs):
  49. from django.core.management import call_command
  50. if auth_app.User in created_models and kwargs.get('interactive', True):
  51. msg = ("\nYou just installed Django's auth system, which means you "
  52. "don't have any superusers defined.\nWould you like to create one "
  53. "now? (yes/no): ")
  54. confirm = raw_input(msg)
  55. while 1:
  56. if confirm not in ('yes', 'no'):
  57. confirm = raw_input('Please enter either "yes" or "no": ')
  58. continue
  59. if confirm == 'yes':
  60. call_command("createsuperuser", interactive=True, database=db)
  61. break
  62. def get_system_username():
  63. """
  64. Try to determine the current system user's username.
  65. :returns: The username as a unicode string, or an empty string if the
  66. username could not be determined.
  67. """
  68. try:
  69. return getpass.getuser().decode(locale.getdefaultlocale()[1])
  70. except (ImportError, KeyError, UnicodeDecodeError):
  71. # KeyError will be raised by os.getpwuid() (called by getuser())
  72. # if there is no corresponding entry in the /etc/passwd file
  73. # (a very restricted chroot environment, for example).
  74. # UnicodeDecodeError - preventive treatment for non-latin Windows.
  75. return u''
  76. def get_default_username(check_db=True):
  77. """
  78. Try to determine the current system user's username to use as a default.
  79. :param check_db: If ``True``, requires that the username does not match an
  80. existing ``auth.User`` (otherwise returns an empty string).
  81. :returns: The username, or an empty string if no username can be
  82. determined.
  83. """
  84. from django.contrib.auth.management.commands.createsuperuser import (
  85. RE_VALID_USERNAME)
  86. default_username = get_system_username()
  87. try:
  88. default_username = unicodedata.normalize('NFKD', default_username)\
  89. .encode('ascii', 'ignore').replace(' ', '').lower()
  90. except UnicodeDecodeError:
  91. return ''
  92. if not RE_VALID_USERNAME.match(default_username):
  93. return ''
  94. # Don't return the default username if it is already taken.
  95. if check_db and default_username:
  96. try:
  97. User.objects.get(username=default_username)
  98. except User.DoesNotExist:
  99. pass
  100. else:
  101. return ''
  102. return default_username
  103. signals.post_syncdb.connect(create_permissions,
  104. dispatch_uid = "django.contrib.auth.management.create_permissions")
  105. signals.post_syncdb.connect(create_superuser,
  106. sender=auth_app, dispatch_uid = "django.contrib.auth.management.create_superuser")