47 lines
2.0 KiB
Python
47 lines
2.0 KiB
Python
import django
|
|
from django.apps import AppConfig, apps
|
|
|
|
from . import settings, threads
|
|
|
|
|
|
class MultitenantAppConfig(AppConfig):
|
|
name = 'hobo.multitenant'
|
|
verbose_name = 'Multitenant'
|
|
|
|
def ready(self):
|
|
from django.db.migrations import operations
|
|
from django.db import migrations
|
|
from django import conf
|
|
|
|
# Install tenant aware settings
|
|
if not isinstance(conf.settings._wrapped,
|
|
settings.TenantSettingsWrapper):
|
|
conf.settings._wrapped = settings.TenantSettingsWrapper(
|
|
conf.settings._wrapped)
|
|
# reset settings getattr method to a cache-less version, to cancel
|
|
# https://code.djangoproject.com/ticket/27625.
|
|
conf.LazySettings.__getattr__ = lambda self, name: getattr(self._wrapped, name)
|
|
threads.install_tenant_aware_threads()
|
|
|
|
if (1, 7) <= django.VERSION < (1, 8):
|
|
class RunPythonOverride(operations.RunPython):
|
|
def can_migrate(self, app_label, schema_editor):
|
|
app = apps.get_app_config(app_label)
|
|
for model in app.get_models():
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, model):
|
|
return True
|
|
return False
|
|
|
|
def database_forwards(self, app_label, schema_editor, *args, **kwargs):
|
|
if not self.can_migrate(app_label, schema_editor):
|
|
return
|
|
super(RunPythonOverride, self).database_forwards(app_label, schema_editor, *args, **kwargs)
|
|
|
|
def database_backwards(self, app_label, schema_editor, *args, **kwargs):
|
|
if not self.can_migrate(app_label, schema_editor):
|
|
return
|
|
super(RunPythonOverride, self).database_backwards(app_label, schema_editor, *args, **kwargs)
|
|
|
|
operations.RunPython = RunPythonOverride
|
|
migrations.RunPython = RunPythonOverride
|