mirror of
https://github.com/jazzband/django-constance.git
synced 2026-03-16 22:40:24 +00:00
Fix create_perm in apps.py to use database alias given by the post_migrate signal
This commit is contained in:
parent
61d32d2f9e
commit
3ba4780a13
3 changed files with 53 additions and 3 deletions
|
|
@ -12,7 +12,7 @@ class ConstanceConfig(AppConfig):
|
||||||
signals.post_migrate.connect(self.create_perm,
|
signals.post_migrate.connect(self.create_perm,
|
||||||
dispatch_uid='constance.create_perm')
|
dispatch_uid='constance.create_perm')
|
||||||
|
|
||||||
def create_perm(self, *args, **kwargs):
|
def create_perm(self, using=None, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Creates a fake content type and permission
|
Creates a fake content type and permission
|
||||||
to be able to check for permissions
|
to be able to check for permissions
|
||||||
|
|
@ -21,12 +21,12 @@ class ConstanceConfig(AppConfig):
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
|
||||||
if ContentType._meta.installed and Permission._meta.installed:
|
if ContentType._meta.installed and Permission._meta.installed:
|
||||||
content_type, created = ContentType.objects.get_or_create(
|
content_type, created = ContentType.objects.using(using).get_or_create(
|
||||||
app_label='constance',
|
app_label='constance',
|
||||||
model='config',
|
model='config',
|
||||||
)
|
)
|
||||||
|
|
||||||
permission, created = Permission.objects.get_or_create(
|
permission, created = Permission.objects.using(using).get_or_create(
|
||||||
name='Can change config',
|
name='Can change config',
|
||||||
content_type=content_type,
|
content_type=content_type,
|
||||||
codename='change_config')
|
codename='change_config')
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,10 @@ DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.sqlite3',
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
'NAME': ':memory:',
|
'NAME': ':memory:',
|
||||||
|
},
|
||||||
|
'secondary': {
|
||||||
|
'ENGINE': 'django.db.backends.sqlite3',
|
||||||
|
'NAME': ':memory:',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
46
tests/test_app.py
Normal file
46
tests/test_app.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
from django.apps import apps
|
||||||
|
from django.contrib.auth.models import Permission
|
||||||
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.db.models import signals
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestApp(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.app_config = apps.get_app_config('constance')
|
||||||
|
|
||||||
|
def test_post_migrate_signal_creates_content_type_and_permission_in_default_database(self):
|
||||||
|
self.assert_uses_correct_database('default')
|
||||||
|
|
||||||
|
def test_post_migrate_signal_creates_content_type_and_permission_in_secondary_database(self):
|
||||||
|
self.assert_uses_correct_database('secondary')
|
||||||
|
|
||||||
|
def test_uses_default_db_even_without_giving_using_keyword(self):
|
||||||
|
self.call_post_migrate(None)
|
||||||
|
|
||||||
|
self.assert_content_type_and_permission_created('default')
|
||||||
|
|
||||||
|
def assert_uses_correct_database(self, database_name):
|
||||||
|
self.call_post_migrate(database_name)
|
||||||
|
|
||||||
|
self.assert_content_type_and_permission_created(database_name)
|
||||||
|
|
||||||
|
def assert_content_type_and_permission_created(self, database_name):
|
||||||
|
content_type_queryset = ContentType.objects.filter(app_label=self.app_config.name) \
|
||||||
|
.using(database_name)
|
||||||
|
|
||||||
|
self.assertTrue(content_type_queryset.exists())
|
||||||
|
|
||||||
|
permission_queryset = Permission.objects.filter(content_type=content_type_queryset.get()) \
|
||||||
|
.using(database_name).exists()
|
||||||
|
|
||||||
|
self.assertTrue(permission_queryset)
|
||||||
|
|
||||||
|
def call_post_migrate(self, database_name):
|
||||||
|
signals.post_migrate.send(
|
||||||
|
sender=self.app_config,
|
||||||
|
app_config=self.app_config,
|
||||||
|
verbosity=None,
|
||||||
|
interactive=None,
|
||||||
|
using=database_name
|
||||||
|
)
|
||||||
Loading…
Reference in a new issue