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