Projet

Général

Profil

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

Benjamin Dauvergne, 05 octobre 2015 21:19

Télécharger (2,67 ko)

Voir les différences:

Subject: [PATCH 1/8] multitenant: add support for deletion of a tenant (#8425)

As we do not really store the tenant models, supermethod is not called.
Tenant directory is not deleted but renamed with an '.invalid' suffix.
Those suffixed directories are ignored by the method to list available
tenants.
 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
-