def handle(self, *args, **options): super().handle(*args, **options) self.PUBLIC_SCHEMA_NAME = get_public_schema_name() if self.sync_public and not self.schema_name: self.schema_name = self.PUBLIC_SCHEMA_NAME executor = get_executor(codename=self.executor)(self.args, self.options) if self.sync_public: executor.run_migrations(tenants=[self.PUBLIC_SCHEMA_NAME]) if self.sync_tenant: if self.schema_name and self.schema_name != self.PUBLIC_SCHEMA_NAME: if not schema_exists(self.schema_name, self.options.get('database', None)): raise RuntimeError('Schema "{}" does not exist'.format( self.schema_name)) elif has_multi_type_tenants(): type_field_name = get_multi_type_database_field_name() tenants = get_tenant_model().objects.only('schema_name', type_field_name)\ .filter(schema_name=self.schema_name)\ .values_list('schema_name', type_field_name) executor.run_multi_type_migrations(tenants=tenants) else: tenants = [self.schema_name] executor.run_migrations(tenants=tenants) else: migration_order = get_tenant_migration_order() if has_multi_type_tenants(): type_field_name = get_multi_type_database_field_name() tenants = get_tenant_model().objects.only('schema_name', type_field_name)\ .exclude(schema_name=self.PUBLIC_SCHEMA_NAME)\ .values_list('schema_name', type_field_name) if migration_order is not None: tenants = tenants.order_by(*migration_order) executor.run_multi_type_migrations(tenants=tenants) else: tenants = get_tenant_model().objects.only( 'schema_name').exclude( schema_name=self.PUBLIC_SCHEMA_NAME).values_list( 'schema_name', flat=True) if migration_order is not None: tenants = tenants.order_by(*migration_order) executor.run_migrations(tenants=tenants)
def is_shared_app(app): if has_multi_type_tenants(): _apps = get_tenant_types()[get_public_schema_name()]['APPS'] else: _apps = settings.SHARED_APPS return app_in_list(app['app_label'], _apps)
def allow_migrate(self, db, app_label, model_name=None, **hints): # the imports below need to be done here else django <1.5 goes crazy # https://code.djangoproject.com/ticket/20704 from django.db import connections from django_tenants.utils import get_public_schema_name, get_tenant_database_alias if db != get_tenant_database_alias(): return False connection = connections[db] public_schema_name = get_public_schema_name() if has_multi_type_tenants(): tenant_types = get_tenant_types() if connection.schema_name == public_schema_name: installed_apps = tenant_types[public_schema_name]['APPS'] else: installed_apps = tenant_types[ connection.tenant.tenant_type]['APPS'] else: if connection.schema_name == public_schema_name: installed_apps = settings.SHARED_APPS else: installed_apps = settings.TENANT_APPS if not self.app_in_list(app_label, installed_apps): return False return None
def is_shared_app(app): if has_multi_type_tenants(): _apps = get_tenant_types()[get_public_schema_name()]['APPS'] else: _apps = settings.SHARED_APPS return app["app_label"] in [get_app_label(_app) for _app in _apps]
def is_shared_app(app): if has_multi_type_tenants(): _apps = get_tenant_types()[get_public_schema_name()]['APPS'] else: _apps = settings.SHARED_APPS return app['app_label'] in [ tenant_app.split('.')[-1] for tenant_app in _apps ]
def is_shared_app(app): if has_multi_type_tenants(): _apps = get_tenant_types()[get_public_schema_name()]['APPS'] else: _apps = settings.SHARED_APPS cfg = apps.get_app_config(app['app_label']) return cfg.module.__name__ in _apps
def is_tenant_app(context, app): if has_multi_type_tenants(): if hasattr(context.request, 'tenant') and context.request.tenant is not None: _apps = get_tenant_types()[context.request.tenant.get_tenant_type()]['APPS'] else: return True else: _apps = settings.TENANT_APPS return app_in_list(app['app_label'], _apps)
def is_tenant_app(context, app): if has_multi_type_tenants(): if hasattr(context.request, 'tenant') and context.request.tenant is not None: _apps = get_tenant_types()[context.request.tenant.get_tenant_type()]['APPS'] else: return True else: _apps = settings.TENANT_APPS cfg = apps.get_app_config(app['app_label']) return cfg.module.__name__ in _apps
def get_subfolder_urlconf(tenant): """ Creates and returns a subfolder URLConf for tenant. """ if has_multi_type_tenants(): urlconf = get_tenant_types()[tenant.get_tenant_type()]['URLCONF'] else: urlconf = settings.ROOT_URLCONF dynamic_path = urlconf + "_dynamically_tenant_prefixed" if not sys.modules.get(dynamic_path): sys.modules[dynamic_path] = get_dynamic_tenant_prefixed_urlconf( urlconf, dynamic_path) return dynamic_path
def is_tenant_app(context, app): if has_multi_type_tenants(): if hasattr(context.request, 'tenant') and context.request.tenant is not None: _apps = get_tenant_types()[ context.request.tenant.get_tenant_type()]['APPS'] else: return True else: _apps = settings.TENANT_APPS return app['app_label'] in [ tenant_app.split('.')[-1] for tenant_app in _apps ]
def setup_url_routing(request): """ Sets the correct url conf based on the tenant :param request: """ public_schema_name = get_public_schema_name() if has_multi_type_tenants(): tenant_types = get_tenant_types() if (not hasattr(request, 'tenant') or (request.tenant.schema_name == get_public_schema_name() and 'URLCONF' in tenant_types[public_schema_name])): request.urlconf = get_public_schema_urlconf() else: tenant_type = request.tenant.get_tenant_type() request.urlconf = tenant_types[tenant_type]['URLCONF'] set_urlconf(request.urlconf) else: # Do we have a public-specific urlconf? if hasattr( settings, 'PUBLIC_SCHEMA_URLCONF' ) and request.tenant.schema_name == get_public_schema_name(): request.urlconf = settings.PUBLIC_SCHEMA_URLCONF