示例#1
0
    def save(self, verbosity=1, *args, **kwargs):

        is_new = self.pk is None
        if not self.db_string:
            self.db_string = get_db_string(self.schema_name)
        db = self.db_string

        from django.db import connection
        if db:
            connection = connections[db]

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. "
                            "Current schema is %s." % connection.schema_name)
        elif not is_new and connection.schema_name not in (
                self.schema_name, get_public_schema_name()):
            raise Exception("Can't update tenant outside it's own schema or "
                            "the public schema. Current schema is %s." %
                            connection.schema_name)

        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            try:
                self.create_schema(check_if_exists=True,
                                   verbosity=verbosity,
                                   db=db)
            except Exception as e:
                # We failed creating the tenant, delete what we created and
                # re-raise the exception
                self.delete(force_drop=True, using=db)
                raise
            else:
                post_schema_sync.send(sender=TenantMixin, tenant=self)
示例#2
0
    def save(self, verbosity = 1, *args, **kwargs):
        if connection.get_schema() != get_public_schema_name():
            raise Exception("Can't update tenant outside the public schema. Current schema is %s." % connection.get_schema())

        is_new = self.pk is None
        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            self.create_schema(check_if_exists=True, verbosity=verbosity)
            post_schema_sync.send(sender=TenantMixin, tenant=self)

        transaction.commit_unless_managed()
示例#3
0
    def create_schema(self,
                      check_if_exists=False,
                      sync_schema=True,
                      verbosity=1):
        """
        Creates the schema 'schema_name' for this tenant. Optionally checks if
        the schema already exists before creating it. Returns true if the
        schema was created, false otherwise.
        """

        # safety check
        _check_schema_name(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            if django.VERSION >= (
                    1,
                    7,
                    0,
            ):
                call_command('migrate_schemas',
                             schema_name=self.schema_name,
                             interactive=False,
                             verbosity=verbosity)
            else:
                # default is faking all migrations and syncing directly to the current models state
                fake_all_migrations = getattr(
                    settings, 'TENANT_CREATION_FAKES_MIGRATIONS', True)
                call_command('sync_schemas',
                             schema_name=self.schema_name,
                             tenant=True,
                             public=False,
                             interactive=False,
                             migrate_all=fake_all_migrations,
                             verbosity=verbosity)

                # run/fake all migrations
                if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode(
                ):
                    call_command('migrate_schemas',
                                 fake=fake_all_migrations,
                                 schema_name=self.schema_name,
                                 verbosity=verbosity)

        connection.set_schema_to_public()
        post_schema_sync.send(sender=TenantMixin, tenant=self)
    def save(self, verbosity=1, *args, **kwargs):
        if connection.get_schema() != get_public_schema_name():
            raise Exception(
                "Can't update tenant outside the public schema. Current schema is %s."
                % connection.get_schema())

        is_new = self.pk is None
        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            self.create_schema(check_if_exists=True, verbosity=verbosity)
            post_schema_sync.send(sender=TenantMixin, tenant=self)

        transaction.commit_unless_managed()
示例#5
0
    def save(self, verbosity=1, *args, **kwargs):
        is_new = self.pk is None

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. Current schema is %s."
                            % connection.schema_name)
        elif not is_new and connection.schema_name not in (self.schema_name, get_public_schema_name()):
            raise Exception("Can't update tenant outside it's own schema or the public schema. Current schema is %s."
                            % connection.schema_name)

        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            self.create_schema(check_if_exists=True, verbosity=verbosity)
            post_schema_sync.send(sender=TenantMixin, tenant=self)
    def save(self, verbosity=1, *args, **kwargs):
        is_new = self.pk is None

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. Current schema is %s."
                            % connection.schema_name)
        elif not is_new and connection.schema_name not in (self.schema_name, get_public_schema_name()):
            raise Exception("Can't update tenant outside it's own schema or the public schema. Current schema is %s."
                            % connection.schema_name)

        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            self.create_schema(check_if_exists=True, verbosity=verbosity)
            post_schema_sync.send(sender=TenantMixin, tenant=self)
示例#7
0
    def create_schema(self, check_if_exists=False, sync_schema=True,
                      verbosity=1):
        """
        Creates the schema 'schema_name' for this tenant. Optionally checks if
        the schema already exists before creating it. Returns true if the
        schema was created, false otherwise.
        """

        # safety check
        _check_schema_name(self.schema_name)
        cursor = connection.cursor()

        if check_if_exists and schema_exists(self.schema_name):
            return False

        # create the schema
        cursor.execute('CREATE SCHEMA %s' % self.schema_name)

        if sync_schema:
            if django.VERSION >= (1, 7, 0,):
                call_command('migrate_schemas',
                             schema_name=self.schema_name,
                             interactive=False,
                             verbosity=verbosity)
            else:
                # default is faking all migrations and syncing directly to the current models state
                fake_all_migrations = getattr(settings, 'TENANT_CREATION_FAKES_MIGRATIONS', True)
                call_command('sync_schemas',
                             schema_name=self.schema_name,
                             tenant=True,
                             public=False,
                             interactive=False,
                             migrate_all=fake_all_migrations,
                             verbosity=verbosity)

                # run/fake all migrations
                if 'south' in settings.INSTALLED_APPS and not django_is_in_test_mode():
                    call_command('migrate_schemas',
                                 fake=fake_all_migrations,
                                 schema_name=self.schema_name,
                                 verbosity=verbosity)

        connection.set_schema_to_public()
        post_schema_sync.send(sender=TenantMixin, tenant=self)
示例#8
0
    def save(self, verbosity=1, *args, **kwargs):
        if hasattr(connection, 'get_schema') and connection.get_schema() != get_public_schema_name():
            raise Exception("Can't update tenant outside the public schema. Current schema is %s." % connection.get_schema())

        is_new = self.pk is None

        if is_new and settings.TENANT_DYNAMIC_SITE and hasattr(self, 'site_id') and not self.site_id:
            self.site_id = self.create_site().id

        print self.site_id

        super(TenantMixin, self).save(*args, **kwargs)

        print self.site_id

        if is_new and self.auto_create_schema and hasattr(connection, 'get_schema'):
            self.create_schema(check_if_exists=True, verbosity=verbosity)
            post_schema_sync.send(sender=TenantMixin, tenant=self)

        transaction.commit_unless_managed()
示例#9
0
    def save(self, verbosity=1, force_create=False, *args, **kwargs):
        is_new = self.pk is None or force_create

        if is_new and connection.schema_name not in (get_public_schema_name(), settings.DEFAULT_TENANT_SCHEMA):
            raise Exception("Can't create tenant outside the public schema. "
                            "Current schema is %s." % connection.schema_name)
        elif not is_new and connection.schema_name not in (self.schema_name, get_public_schema_name()):
            raise Exception("Can't update tenant outside it's own schema or "
                            "the public schema. Current schema is %s."
                            % connection.schema_name)

        super(TenantMixin, self).save(*args, **kwargs)

        if is_new and self.is_active and self.auto_create_schema:
            try:
                self.create_schema(check_if_exists=True, verbosity=verbosity)
                post_schema_sync.send(sender=TenantMixin, tenant=self)
            except:
                # We failed creating the tenant, delete what we created and
                # re-raise the exception
                self.delete(force_drop=True)
                raise
    def save(self, verbosity=1, *args, **kwargs):
        is_new = self.pk is None

        if is_new and connection.schema_name != get_public_schema_name():
            raise Exception("Can't create tenant outside the public schema. "
                            "Current schema is %s." % connection.schema_name)
        elif not is_new and connection.schema_name not in (
                self.schema_name, get_public_schema_name()):
            raise Exception("Can't update tenant outside it's own schema or "
                            "the public schema. Current schema is %s." %
                            connection.schema_name)

        super(Tenant, self).save(*args, **kwargs)

        if is_new and self.auto_create_schema:
            try:
                self.create_schema(check_if_exists=True, verbosity=verbosity)
                post_schema_sync.send(sender=Tenant, tenant=self)
            except:
                # We failed creating the tenant, delete what we created and
                # re-raise the exception
                self.delete(force_drop=True)
                raise