2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Creates permissions for all installed apps that need permissions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db.models import get_models, signals
|
|
|
|
from django.contrib.auth import models as auth_app
|
|
|
|
|
|
|
|
def _get_permission_codename(action, opts):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'%s_%s' % (action, opts.object_name.lower())
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def _get_all_permissions(opts):
|
|
|
|
"Returns (codename, name) for all permissions in the given opts."
|
|
|
|
perms = []
|
|
|
|
for action in ('add', 'change', 'delete'):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
perms.append((_get_permission_codename(action, opts), u'Can %s %s' % (action, opts.verbose_name_raw)))
|
2006-05-02 09:31:56 +08:00
|
|
|
return perms + list(opts.permissions)
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
def create_permissions(app, created_models, verbosity, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
app_models = get_models(app)
|
|
|
|
if not app_models:
|
|
|
|
return
|
|
|
|
for klass in app_models:
|
|
|
|
ctype = ContentType.objects.get_for_model(klass)
|
|
|
|
for codename, name in _get_all_permissions(klass._meta):
|
2006-06-20 12:44:27 +08:00
|
|
|
p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
|
|
|
|
defaults={'name': name, 'content_type': ctype})
|
2006-08-27 20:46:39 +08:00
|
|
|
if created and verbosity >= 2:
|
2006-05-02 09:31:56 +08:00
|
|
|
print "Adding permission '%s'" % p
|
|
|
|
|
2006-08-27 20:46:39 +08:00
|
|
|
def create_superuser(app, created_models, verbosity, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.models import User
|
2008-06-08 13:31:16 +08:00
|
|
|
from django.core.management import call_command
|
2006-08-27 20:46:39 +08:00
|
|
|
if User in created_models and kwargs.get('interactive', True):
|
2006-05-02 09:31:56 +08:00
|
|
|
msg = "\nYou just installed Django's auth system, which means you don't have " \
|
|
|
|
"any superusers defined.\nWould you like to create one now? (yes/no): "
|
|
|
|
confirm = raw_input(msg)
|
|
|
|
while 1:
|
|
|
|
if confirm not in ('yes', 'no'):
|
|
|
|
confirm = raw_input('Please enter either "yes" or "no": ')
|
|
|
|
continue
|
|
|
|
if confirm == 'yes':
|
2008-06-09 02:18:01 +08:00
|
|
|
call_command("createsuperuser", interactive=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
break
|
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
signals.post_syncdb.connect(create_permissions,
|
|
|
|
dispatch_uid = "django.contrib.auth.management.create_permissions")
|
|
|
|
signals.post_syncdb.connect(create_superuser,
|
|
|
|
sender=auth_app, dispatch_uid = "django.contrib.auth.management.create_superuser")
|