Respect 'using' in signals (#200)
parent
7c9340ba3a
commit
946b9c98e0
|
|
@ -5,6 +5,10 @@ __pycache__/
|
|||
# C extensions
|
||||
*.so
|
||||
|
||||
## Local setup
|
||||
.vscode/
|
||||
.venv/
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
env/
|
||||
|
|
|
|||
|
|
@ -17,19 +17,22 @@ class Theme(models.Model):
|
|||
@staticmethod
|
||||
def post_migrate_handler(**kwargs):
|
||||
del_cached_active_theme()
|
||||
Theme.get_active_theme()
|
||||
db = kwargs["using"]
|
||||
Theme.get_active_theme(database=db)
|
||||
|
||||
@staticmethod
|
||||
def post_delete_handler(**kwargs):
|
||||
del_cached_active_theme()
|
||||
Theme.get_active_theme()
|
||||
db = kwargs["using"]
|
||||
Theme.get_active_theme(database=db)
|
||||
|
||||
@staticmethod
|
||||
def post_save_handler(instance, **kwargs):
|
||||
del_cached_active_theme()
|
||||
db = kwargs["using"]
|
||||
if instance.active:
|
||||
Theme.objects.exclude(pk=instance.pk).update(active=False)
|
||||
Theme.get_active_theme()
|
||||
Theme.objects.using(db).exclude(pk=instance.pk).update(active=False)
|
||||
Theme.get_active_theme(database=db)
|
||||
|
||||
@staticmethod
|
||||
def pre_save_handler(instance, **kwargs):
|
||||
|
|
@ -42,8 +45,8 @@ class Theme(models.Model):
|
|||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_active_theme():
|
||||
objs_manager = Theme.objects
|
||||
def get_active_theme(database="default"):
|
||||
objs_manager = Theme.objects.using(database)
|
||||
objs_active_qs = objs_manager.filter(active=True)
|
||||
objs_active_ls = list(objs_active_qs)
|
||||
objs_active_count = len(objs_active_ls)
|
||||
|
|
|
|||
|
|
@ -90,6 +90,14 @@ database_config = {
|
|||
"HOST": "",
|
||||
"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")
|
||||
|
|
@ -97,9 +105,16 @@ if github_workflow:
|
|||
database_config["postgres"]["NAME"] = "postgres"
|
||||
database_config["postgres"]["HOST"] = "127.0.0.1"
|
||||
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 = {
|
||||
"default": database_config.get(database_engine),
|
||||
"replica": database_config.get(replica_engine),
|
||||
}
|
||||
|
||||
USE_I18N = True
|
||||
|
|
|
|||
|
|
@ -4,9 +4,10 @@ from __future__ import unicode_literals
|
|||
|
||||
import random
|
||||
import shutil
|
||||
from unittest import expectedFailure
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, TransactionTestCase
|
||||
|
||||
from admin_interface.models import Theme
|
||||
|
||||
|
|
@ -16,6 +17,7 @@ class AdminInterfaceModelsTestCase(TestCase):
|
|||
pass
|
||||
|
||||
def tearDown(self):
|
||||
Theme.objects.all().delete()
|
||||
shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
|
||||
|
||||
def __test_active_theme(self):
|
||||
|
|
@ -88,3 +90,28 @@ class AdminInterfaceModelsTestCase(TestCase):
|
|||
def test_repr(self):
|
||||
theme = Theme.get_active_theme()
|
||||
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