| 1 | # vim: set ts=8 sw=4 sts=4 et ai:
|
|---|
| 2 | from django.conf import settings
|
|---|
| 3 | from django.contrib.auth.management import create_permissions, _get_all_permissions
|
|---|
| 4 | from django.db.models import get_models
|
|---|
| 5 | from django.db.models.signals import post_syncdb
|
|---|
| 6 | from django.utils.encoding import smart_unicode
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | def create_permissions_respecting_proxy(app, created_models, verbosity, **kwargs):
|
|---|
| 10 | '''
|
|---|
| 11 | An alternative to create_permissions found in django.contrib.auth.
|
|---|
| 12 | This one doesn't use the ContentType.objects.get_for_model which
|
|---|
| 13 | resolves the klass to the base model. Instead it returns the
|
|---|
| 14 | content type for the proxy model.
|
|---|
| 15 | '''
|
|---|
| 16 | from django.contrib.contenttypes.models import ContentType
|
|---|
| 17 | from django.contrib.auth.models import Permission
|
|---|
| 18 | app_models = get_models(app)
|
|---|
| 19 | if not app_models:
|
|---|
| 20 | return
|
|---|
| 21 | for klass in app_models:
|
|---|
| 22 | # The difference is here:
|
|---|
| 23 | #ctype = ContentType.objects.get_for_model(klass)
|
|---|
| 24 | opts = klass._meta
|
|---|
| 25 | ctype, created = ContentType.objects.get_or_create(
|
|---|
| 26 | app_label = opts.app_label,
|
|---|
| 27 | model = opts.object_name.lower(),
|
|---|
| 28 | defaults = {'name': smart_unicode(opts.verbose_name_raw)},
|
|---|
| 29 | )
|
|---|
| 30 | # (end of difference)
|
|---|
| 31 | for codename, name in _get_all_permissions(klass._meta):
|
|---|
| 32 | p, created = Permission.objects.get_or_create(codename=codename, content_type__pk=ctype.id,
|
|---|
| 33 | defaults={'name': name, 'content_type': ctype})
|
|---|
| 34 | if created and verbosity >= 2:
|
|---|
| 35 | print "Adding permission '%s'" % p
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | # Replace the original handling with our modified one if
|
|---|
| 39 | # CONTENTTYPE_NO_TRAVERSE_PROXY is set.
|
|---|
| 40 | # This is needed if you want to use proper permissions for proxy models
|
|---|
| 41 | # that are tied to the proxy application.
|
|---|
| 42 | # See also: http://code.djangoproject.com/ticket/11154
|
|---|
| 43 | try:
|
|---|
| 44 | settings.CONTENTTYPE_NO_TRAVERSE_PROXY
|
|---|
| 45 | except AttributeError:
|
|---|
| 46 | pass
|
|---|
| 47 | else:
|
|---|
| 48 | print 'bier'
|
|---|
| 49 | if settings.CONTENTTYPE_NO_TRAVERSE_PROXY:
|
|---|
| 50 | post_syncdb.disconnect(create_permissions, \
|
|---|
| 51 | dispatch_uid='django.contrib.auth.management.create_permissions')
|
|---|
| 52 | post_syncdb.connect(create_permissions_respecting_proxy, \
|
|---|
| 53 | dispatch_uid='django.contrib.auth.management.create_permissions')
|
|---|