Projet

Général

Profil

0001-multitenant-add-support-for-deletion-of-a-tenant.patch

Benjamin Dauvergne, 30 septembre 2015 10:23

Télécharger (2,44 ko)

Voir les différences:

Subject: [PATCH 1/3] multitenant: add support for deletion of a tenant

 hobo/multitenant/middleware.py |  2 ++
 hobo/multitenant/models.py     | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)
hobo/multitenant/middleware.py
42 42
        self = cls()
43 43
        for path in glob.glob(os.path.join(cls.base(), '*')):
44 44
            hostname = os.path.basename(path)
45
            if hostname.endswith('.invalid'):
46
                continue
45 47
            yield get_tenant_model()(
46 48
                    schema_name=self.hostname2schema(hostname),
47 49
                    domain_url=hostname)
hobo/multitenant/models.py
3 3
import json
4 4

  
5 5
from django.conf import settings
6
from django.db import connection
6 7

  
8
from tenant_schemas.utils import get_public_schema_name
7 9
from tenant_schemas.models import TenantMixin
10
from tenant_schemas.utils import django_is_in_test_mode, schema_exists
8 11

  
9 12
class Tenant(TenantMixin):
10 13
    # default true, schema will be automatically created and synced when it is saved
......
49 52

  
50 53
    def build_absolute_uri(self, location):
51 54
        return urljoin(self.get_base_url(), location)
55

  
56
    def delete(self, force_drop=False, *args, **kwargs):
57
        """
58
        Deletes this row. Drops the tenant's schema if the attribute
59
        auto_drop_schema set to True.
60
        """
61
        if connection.schema_name not in (self.schema_name, get_public_schema_name()):
62
            raise Exception("Can't delete tenant outside it's own schema or "
63
                            "the public schema. Current schema is %s."
64
                            % connection.schema_name)
65

  
66
        os.rename(self.get_directory(), self.get_directory()+'.invalid')
67

  
68
        if schema_exists(self.schema_name) and (self.auto_drop_schema or force_drop) and not django_is_in_test_mode():
69
            cursor = connection.cursor()
70
            cursor.execute('DROP SCHEMA %s CASCADE' % self.schema_name)
52
-