From 7adf2654b14f21c7a9134534889febc8109ef264 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 29 Sep 2015 19:35:07 +0200 Subject: [PATCH 1/5] 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(+) diff --git a/hobo/multitenant/middleware.py b/hobo/multitenant/middleware.py index 68411cd..8dd1e7a 100644 --- a/hobo/multitenant/middleware.py +++ b/hobo/multitenant/middleware.py @@ -42,6 +42,8 @@ class TenantMiddleware(object): self = cls() for path in glob.glob(os.path.join(cls.base(), '*')): hostname = os.path.basename(path) + if hostname.endswith('.invalid'): + continue yield get_tenant_model()( schema_name=self.hostname2schema(hostname), domain_url=hostname) diff --git a/hobo/multitenant/models.py b/hobo/multitenant/models.py index 96f63be..b86b964 100644 --- a/hobo/multitenant/models.py +++ b/hobo/multitenant/models.py @@ -3,8 +3,11 @@ from urlparse import urljoin import json from django.conf import settings +from django.db import connection +from tenant_schemas.utils import get_public_schema_name from tenant_schemas.models import TenantMixin +from tenant_schemas.utils import django_is_in_test_mode, schema_exists class Tenant(TenantMixin): # default true, schema will be automatically created and synced when it is saved @@ -49,3 +52,19 @@ class Tenant(TenantMixin): def build_absolute_uri(self, location): return urljoin(self.get_base_url(), location) + + def delete(self, force_drop=False, *args, **kwargs): + """ + Deletes this row. Drops the tenant's schema if the attribute + auto_drop_schema set to True. + """ + if connection.schema_name not in (self.schema_name, get_public_schema_name()): + raise Exception("Can't delete tenant outside it's own schema or " + "the public schema. Current schema is %s." + % connection.schema_name) + + os.rename(self.get_directory(), self.get_directory()+'.invalid') + + if schema_exists(self.schema_name) and (self.auto_drop_schema or force_drop) and not django_is_in_test_mode(): + cursor = connection.cursor() + cursor.execute('DROP SCHEMA %s CASCADE' % self.schema_name) -- 2.1.4