From f831b975fa12c0ecff9576184f497344317472ce Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Tue, 3 Feb 2015 16:36:55 +0100
Subject: [PATCH] tenants in TENANT_BASE/hostname

---
 .../djommon/multitenant/management/commands/create_tenant.py      | 8 ++------
 entrouvert/djommon/multitenant/management/commands/deploy.py      | 3 +--
 entrouvert/djommon/multitenant/middleware.py                      | 7 +++----
 entrouvert/djommon/multitenant/storage.py                         | 2 +-
 entrouvert/djommon/multitenant/template_loader.py                 | 2 +-
 5 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/entrouvert/djommon/multitenant/management/commands/create_tenant.py b/entrouvert/djommon/multitenant/management/commands/create_tenant.py
index 5f67f51..1d72195 100644
--- a/entrouvert/djommon/multitenant/management/commands/create_tenant.py
+++ b/entrouvert/djommon/multitenant/management/commands/create_tenant.py
@@ -13,17 +13,14 @@ class Command(BaseCommand):
         if not args:
             raise CommandError("you must give at least one tenant hostname")
 
-        for arg in args:
-            hostname = arg
-            tenant_name = TenantMiddleware.hostname2schema(hostname)
+        for hostname in args:
             try:
                 tenant_base = TenantMiddleware.base()
             except AttributeError:
                 raise CommandError("you must configure TENANT_BASE in your settings")
             if not tenant_base:
                 raise CommandError("you must set a value to TENANT_BASE in your settings")
-            tenant_dir = os.path.join(tenant_base,
-                    tenant_name)
+            tenant_dir = os.path.join(tenant_base, hostname)
             if not os.path.exists(tenant_dir):
                 os.mkdir(tenant_dir, 0755)
             for folder in ('media', 'static', 'templates'):
@@ -38,4 +35,3 @@ class Command(BaseCommand):
                     + self.style.SQL_TABLE(tenant.schema_name)
             if not tenant.create_schema(check_if_exists=True):
                 print self.style.ERROR(' Nothing to do: %r already exist' % hostname)
-
diff --git a/entrouvert/djommon/multitenant/management/commands/deploy.py b/entrouvert/djommon/multitenant/management/commands/deploy.py
index 5c31fec..f37d155 100644
--- a/entrouvert/djommon/multitenant/management/commands/deploy.py
+++ b/entrouvert/djommon/multitenant/management/commands/deploy.py
@@ -22,8 +22,7 @@ class Command(BaseCommand):
 
         call_command('create_tenant', hostname)
 
-        tenant_name = TenantMiddleware.hostname2schema(hostname)
-        tenant = TenantMiddleware.get_tenant_by_hostname(tenant_name)
+        tenant = TenantMiddleware.get_tenant_by_hostname(hostname)
         with tenant_context(tenant):
             self.deploy_tenant(environment, service, options)
 
diff --git a/entrouvert/djommon/multitenant/middleware.py b/entrouvert/djommon/multitenant/middleware.py
index c6d0c7a..f805a25 100644
--- a/entrouvert/djommon/multitenant/middleware.py
+++ b/entrouvert/djommon/multitenant/middleware.py
@@ -33,10 +33,9 @@ class TenantMiddleware(object):
     @classmethod
     def get_tenant_by_hostname(cls, hostname):
         '''Retrieve a tenant object for this hostname'''
-        schema = cls.hostname2schema(hostname)
-        p = os.path.join(cls.base(), schema)
-        if not os.path.exists(p):
+        if not os.path.exists(os.path.join(cls.base(), hostname)):
             raise TenantNotFound
+        schema = cls.hostname2schema(hostname)
         return get_tenant_model()(schema_name=schema, domain_url=hostname)
 
     @classmethod
@@ -118,7 +117,7 @@ class FileBasedTenantSettingBaseMiddleware(TenantSettingBaseMiddleware):
     FILENAME = None
 
     def load_tenant_settings(self, wrapped, tenant, tenant_settings, last_time):
-        path = os.path.join(settings.TENANT_BASE, tenant.schema_name, self.FILENAME)
+        path = os.path.join(settings.TENANT_BASE, tenant.domain_url, self.FILENAME)
         try:
             new_time = os.stat(path).st_mtime
         except OSError:
diff --git a/entrouvert/djommon/multitenant/storage.py b/entrouvert/djommon/multitenant/storage.py
index c127891..2bd3391 100644
--- a/entrouvert/djommon/multitenant/storage.py
+++ b/entrouvert/djommon/multitenant/storage.py
@@ -14,7 +14,7 @@ class TenantFileSystemStorage(FileSystemStorage):
     '''Lookup files first in $TENANT_BASE/<tenant.schema>/media/ then in default location'''
     def path(self, name):
         if connection.tenant:
-            location = safe_join(settings.TENANT_BASE, connection.tenant.schema_name, 'media')
+            location = safe_join(settings.TENANT_BASE, connection.tenant.domain_url, 'media')
         else:
             location = self.location
         try:
diff --git a/entrouvert/djommon/multitenant/template_loader.py b/entrouvert/djommon/multitenant/template_loader.py
index e54978d..6e0190c 100644
--- a/entrouvert/djommon/multitenant/template_loader.py
+++ b/entrouvert/djommon/multitenant/template_loader.py
@@ -89,7 +89,7 @@ class FilesystemLoader(BaseLoader):
                 raise ImproperlyConfigured('To use %s.%s you must define the TENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__))
         for template_dir in template_dirs:
             try:
-                yield safe_join(template_dir, connection.tenant.schema_name, 'templates', template_name)
+                yield safe_join(template_dir, connection.tenant.domain_url, 'templates', template_name)
             except UnicodeDecodeError:
                 # The template dir name was a bytestring that wasn't valid UTF-8.
                 raise
-- 
2.1.4

