Respect 'using' in signals (#200)
parent
7c9340ba3a
commit
946b9c98e0
|
|
@ -5,6 +5,10 @@ __pycache__/
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
|
||||||
|
## Local setup
|
||||||
|
.vscode/
|
||||||
|
.venv/
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
env/
|
env/
|
||||||
|
|
|
||||||
|
|
@ -17,19 +17,22 @@ class Theme(models.Model):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_migrate_handler(**kwargs):
|
def post_migrate_handler(**kwargs):
|
||||||
del_cached_active_theme()
|
del_cached_active_theme()
|
||||||
Theme.get_active_theme()
|
db = kwargs["using"]
|
||||||
|
Theme.get_active_theme(database=db)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_delete_handler(**kwargs):
|
def post_delete_handler(**kwargs):
|
||||||
del_cached_active_theme()
|
del_cached_active_theme()
|
||||||
Theme.get_active_theme()
|
db = kwargs["using"]
|
||||||
|
Theme.get_active_theme(database=db)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_save_handler(instance, **kwargs):
|
def post_save_handler(instance, **kwargs):
|
||||||
del_cached_active_theme()
|
del_cached_active_theme()
|
||||||
|
db = kwargs["using"]
|
||||||
if instance.active:
|
if instance.active:
|
||||||
Theme.objects.exclude(pk=instance.pk).update(active=False)
|
Theme.objects.using(db).exclude(pk=instance.pk).update(active=False)
|
||||||
Theme.get_active_theme()
|
Theme.get_active_theme(database=db)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def pre_save_handler(instance, **kwargs):
|
def pre_save_handler(instance, **kwargs):
|
||||||
|
|
@ -42,8 +45,8 @@ class Theme(models.Model):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_active_theme():
|
def get_active_theme(database="default"):
|
||||||
objs_manager = Theme.objects
|
objs_manager = Theme.objects.using(database)
|
||||||
objs_active_qs = objs_manager.filter(active=True)
|
objs_active_qs = objs_manager.filter(active=True)
|
||||||
objs_active_ls = list(objs_active_qs)
|
objs_active_ls = list(objs_active_qs)
|
||||||
objs_active_count = len(objs_active_ls)
|
objs_active_count = len(objs_active_ls)
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,14 @@ database_config = {
|
||||||
"HOST": "",
|
"HOST": "",
|
||||||
"PORT": "",
|
"PORT": "",
|
||||||
},
|
},
|
||||||
|
"postgres_replica": {
|
||||||
|
"ENGINE": "django.db.backends.postgresql_psycopg2",
|
||||||
|
"NAME": "admin_interface_2",
|
||||||
|
"USER": "postgres",
|
||||||
|
"PASSWORD": "postgres",
|
||||||
|
"HOST": "",
|
||||||
|
"PORT": "",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
github_workflow = os.environ.get("GITHUB_WORKFLOW")
|
github_workflow = os.environ.get("GITHUB_WORKFLOW")
|
||||||
|
|
@ -97,9 +105,16 @@ if github_workflow:
|
||||||
database_config["postgres"]["NAME"] = "postgres"
|
database_config["postgres"]["NAME"] = "postgres"
|
||||||
database_config["postgres"]["HOST"] = "127.0.0.1"
|
database_config["postgres"]["HOST"] = "127.0.0.1"
|
||||||
database_config["postgres"]["PORT"] = "5432"
|
database_config["postgres"]["PORT"] = "5432"
|
||||||
|
database_config["postgres_replica"]["HOST"] = "127.0.0.1"
|
||||||
|
database_config["postgres_replica"]["PORT"] = "5432"
|
||||||
|
|
||||||
|
replica_engine = (
|
||||||
|
"postgres_replica" if database_engine == "postgres" else database_engine
|
||||||
|
)
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
"default": database_config.get(database_engine),
|
"default": database_config.get(database_engine),
|
||||||
|
"replica": database_config.get(replica_engine),
|
||||||
}
|
}
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@ from __future__ import unicode_literals
|
||||||
|
|
||||||
import random
|
import random
|
||||||
import shutil
|
import shutil
|
||||||
|
from unittest import expectedFailure
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.test import TestCase
|
from django.test import TestCase, TransactionTestCase
|
||||||
|
|
||||||
from admin_interface.models import Theme
|
from admin_interface.models import Theme
|
||||||
|
|
||||||
|
|
@ -16,6 +17,7 @@ class AdminInterfaceModelsTestCase(TestCase):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
Theme.objects.all().delete()
|
||||||
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
|
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
|
||||||
|
|
||||||
def __test_active_theme(self):
|
def __test_active_theme(self):
|
||||||
|
|
@ -88,3 +90,28 @@ class AdminInterfaceModelsTestCase(TestCase):
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
theme = Theme.get_active_theme()
|
theme = Theme.get_active_theme()
|
||||||
self.assertEqual("{0}".format(theme), "Django")
|
self.assertEqual("{0}".format(theme), "Django")
|
||||||
|
|
||||||
|
|
||||||
|
class AdminInterfaceModelsMultiDBTestCase(TestCase):
|
||||||
|
databases = ["default", "replica"]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
Theme.objects.using("default").create(name="Change Active", active=True)
|
||||||
|
|
||||||
|
def test_get_theme_from_default_db(self):
|
||||||
|
de_theme = Theme.get_active_theme()
|
||||||
|
assert de_theme.name == "Change Active"
|
||||||
|
|
||||||
|
def test_get_theme_from_replica_db(self):
|
||||||
|
replica_theme = Theme.get_active_theme(database="replica")
|
||||||
|
assert replica_theme.name == "Django"
|
||||||
|
|
||||||
|
def test_db_are_isolated(self):
|
||||||
|
default_theme = Theme.get_active_theme()
|
||||||
|
replica_theme = Theme.get_active_theme(database="replica")
|
||||||
|
assert default_theme.name != replica_theme.name
|
||||||
|
|
||||||
|
@expectedFailure
|
||||||
|
def test_fail_for_wrong_db_defined_in_kwargs(self):
|
||||||
|
Theme.get_active_theme(database="other")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue