Пример #1
0
    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 connection
        from tenant_schemas.utils import get_public_schema_name, app_labels

        if isinstance(app_label, ModelBase):
            # In django <1.7 the `app_label` parameter is actually `model`
            app_label = app_label._meta.app_label

        if connection.schema_name == get_public_schema_name():
            if app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if app_label not in app_labels(settings.TENANT_APPS):
                return False

        if model_name:
            model = apps.get_model(app_label=app_label, model_name=model_name)
            if hasattr(model, '__schema_name__') and model.__schema_name__:

                conn_schema = connection.tenant.schema_name.strip()
                if conn_schema == 'public' and conn_schema != model.__schema_name__ or \
                   conn_schema != 'public' and model.__schema_name__ == 'public':
                    return False

        return None
    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 connection
        from tenant_schemas.utils import get_public_schema_name, app_labels
        from tenant_schemas.postgresql_backend.base import DatabaseWrapper as TenantDbWrapper

        db_engine = settings.DATABASES[db]["ENGINE"]
        if not (
            db_engine == "tenant_schemas.postgresql_backend"
            or issubclass(getattr(load_backend(db_engine), "DatabaseWrapper"), TenantDbWrapper)
        ):
            return None

        if isinstance(app_label, ModelBase):
            # In django <1.7 the `app_label` parameter is actually `model`
            app_label = app_label._meta.app_label

        if connection.schema_name == get_public_schema_name():
            if app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
    def allow_migrate(self, db, model):
        # the imports below need to be done here else django <1.5 goes crazy
        # https://code.djangoproject.com/ticket/20704
        from django.db import connection
        from tenant_schemas.utils import get_public_schema_name, app_labels

        if connection.schema_name == get_public_schema_name():
            if model._meta.app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if model._meta.app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
Пример #4
0
    def allow_migrate(self, db, model):
        # the imports below need to be done here else django <1.5 goes crazy
        # https://code.djangoproject.com/ticket/20704
        from django.db import connection
        from tenant_schemas.utils import get_public_schema_name, app_labels

        if connection.schema_name == get_public_schema_name():
            if model._meta.app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if model._meta.app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
    def _get_tenant(self, request, allow_public=None):
        tenant_id = request.session.get('active_tenant')
        tenant = None
        TenantModel = get_tenant_model()
        if tenant_id is not None:
            try:
                tenant = TenantModel.objects.get(pk=tenant_id)
            except TenantModel.DoesNotExist:
                del request.session['active_tenant']
                tenant = None
        if tenant is None:
            tenant = connection.tenant

        if tenant.schema_name == get_public_schema_name():
            allow_public = allow_public if allow_public is not None else (
                self.model._meta.app_label in app_labels(settings.TENANT_APPS))
            if not allow_public:
                return None
            tenant = TenantModel.objects.exclude(
                schema_name=get_public_schema_name()).first()
            if tenant:
                self.message_user(request,
                                  "This model does not exist in public schema."
                                  " Falling into any first tenant.",
                                  level=messages.WARNING)

        return tenant
Пример #6
0
    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 connection
        from tenant_schemas.utils import get_public_schema_name, app_labels

        if isinstance(app_label, ModelBase):
            # In django <1.7 the `app_label` parameter is actually `model`
            app_label = app_label._meta.app_label

        if getattr(connection, 'schema_name', None) == get_public_schema_name():
            if app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
Пример #7
0
    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 connection
        from tenant_schemas.utils import get_public_schema_name, app_labels

        if not isinstance(app_label, str):
            # In django 1.7 the `app_label` parameter is actually `model`
            app_label = app_label._meta.app_label

        if connection.schema_name == get_public_schema_name():
            if app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
Пример #8
0
    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 tenant_schemas.utils import get_public_schema_name, app_labels
        from tenant_schemas.postgresql_backend.base import DatabaseWrapper as TenantDbWrapper

        db_engine = settings.DATABASES[db]['ENGINE']
        if not (db_engine == 'tenant_schemas.postgresql_backend' or
                issubclass(getattr(load_backend(db_engine), 'DatabaseWrapper'), TenantDbWrapper)):
            return None

        if isinstance(app_label, ModelBase):
            # In django <1.7 the `app_label` parameter is actually `model`
            app_label = app_label._meta.app_label
        if connections[db].schema_name == get_public_schema_name():
            if app_label not in app_labels(settings.SHARED_APPS):
                return False
        else:
            if app_label not in app_labels(settings.TENANT_APPS):
                return False

        return None
Пример #9
0
    def test_app_labels_1_7(self):
        """
        Verifies that app_labels handle Django 1.7+ AppConfigs properly.
        https://docs.djangoproject.com/en/1.7/ref/applications/
        """
        if django.VERSION >= (1, 7):
            from django.apps import AppConfig
        else:
            # set up poor man's mock for 1.7/1.8 behaviour.
            from collections import namedtuple

            class AppConfig(object):
                call_count = 0
                ret_map = {
                    'example1': 'example1',
                    'example2.apps.Example2AppConfig': 'example2_app',
                }

                @classmethod
                def create(cls, app):
                    return namedtuple('AppConfig', ['label'])(
                        cls.ret_map[app]
                    )
            dj_apps = self.set_up_module('django.apps')
            dj_apps.AppConfig = AppConfig

            reload(utils)

        self.set_up_module('example1')
        apps = self.set_up_module('example2.apps')

        # set up AppConfig on the `test_app.apps` module
        class Example2AppConfig(AppConfig):
            name = 'example2'
            label = 'example2_app'  # with different name
            path = '/tmp'  # for whatever reason path is required

        apps.Example2AppConfig = Example2AppConfig

        self.assertEqual(
            utils.app_labels([
                'example1',
                'example2.apps.Example2AppConfig'
            ]),
            ['example1', 'example2_app'],
        )
Пример #10
0
    def test_app_labels_pre_1_7(self):
        if django.VERSION >= (1, 7):
            # set AppConfig to None and reload utils module
            from django import apps
            AppConfig = apps.AppConfig
            apps.AppConfig = None

            reload(utils)

        self.assertEqual(
            utils.app_labels(
                ['example1', 'example2.apps.Example2AppConfig']
            ),
            ['example1', 'Example2AppConfig']
        )

        if django.VERSION >= (1, 7):
            # restore AppConfig
            apps.AppConfig = AppConfig
            reload(utils)
Пример #11
0
    def test_app_labels(self):
        """
        Verifies that app_labels handle Django 1.7+ AppConfigs properly.
        https://docs.djangoproject.com/en/1.7/ref/applications/
        """
        self.set_up_module('example1')
        apps = self.set_up_module('example2.apps')

        # set up AppConfig on the `test_app.apps` module
        class Example2AppConfig(AppConfig):
            name = 'example2'
            label = 'example2_app'  # with different name
            path = '/tmp'  # for whatever reason path is required

        apps.Example2AppConfig = Example2AppConfig

        self.assertEqual(
            utils.app_labels(['example1', 'example2.apps.Example2AppConfig']),
            ['example1', 'example2_app'],
        )
    def test_app_labels(self):
        """
        Verifies that app_labels handle Django 1.7+ AppConfigs properly.
        https://docs.djangoproject.com/en/1.7/ref/applications/
        """
        self.set_up_module('example1')
        apps = self.set_up_module('example2.apps')

        # set up AppConfig on the `test_app.apps` module
        class Example2AppConfig(AppConfig):
            name = 'example2'
            label = 'example2_app'  # with different name
            path = '/tmp'  # for whatever reason path is required

        apps.Example2AppConfig = Example2AppConfig

        self.assertEqual(
            utils.app_labels([
                'example1',
                'example2.apps.Example2AppConfig'
            ]),
            ['example1', 'example2_app'],
        )