Fixed #22956 -- Made PermissionManager.get_by_natural_key() use the correct database for content type lookup.

This commit is contained in:
Jürno Ader 2014-07-10 17:00:36 +03:00 committed by Tim Graham
parent da59902250
commit 76f2f58a18
2 changed files with 61 additions and 3 deletions

View File

@ -32,8 +32,7 @@ class PermissionManager(models.Manager):
def get_by_natural_key(self, codename, app_label, model):
return self.get(
codename=codename,
content_type=ContentType.objects.get_by_natural_key(app_label,
model),
content_type=ContentType.objects.db_manager(self.db).get_by_natural_key(app_label, model),
)

View File

@ -1,6 +1,7 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, Group, User, UserManager
from django.contrib.auth.models import AbstractUser, Group, Permission, User, UserManager
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.contenttypes.models import ContentType
from django.core import mail
from django.db.models.signals import post_save
from django.test import TestCase, override_settings
@ -43,6 +44,64 @@ class LoadDataWithNaturalKeysTestCase(TestCase):
self.assertEqual(group, user.groups.get())
class LoadDataWithNaturalKeysAndMultipleDatabasesTestCase(TestCase):
multi_db = True
def test_load_data_with_user_permissions(self):
# Create test contenttypes for both databases
default_objects = [
ContentType.objects.db_manager('default').create(
model='examplemodela',
name='example model a',
app_label='app_a',
),
ContentType.objects.db_manager('default').create(
model='examplemodelb',
name='example model b',
app_label='app_b',
),
]
other_objects = [
ContentType.objects.db_manager('other').create(
model='examplemodelb',
name='example model b',
app_label='app_b',
),
ContentType.objects.db_manager('other').create(
model='examplemodela',
name='example model a',
app_label='app_a',
),
]
# Now we create the test UserPermission
Permission.objects.db_manager("default").create(
name="Can delete example model b",
codename="delete_examplemodelb",
content_type=default_objects[1],
)
Permission.objects.db_manager("other").create(
name="Can delete example model b",
codename="delete_examplemodelb",
content_type=other_objects[0],
)
perm_default = Permission.objects.get_by_natural_key(
'delete_examplemodelb',
'app_b',
'examplemodelb',
)
perm_other = Permission.objects.db_manager('other').get_by_natural_key(
'delete_examplemodelb',
'app_b',
'examplemodelb',
)
self.assertEqual(perm_default.content_type_id, default_objects[1].id)
self.assertEqual(perm_other.content_type_id, other_objects[0].id)
@skipIfCustomUser
class UserManagerTestCase(TestCase):