tenants in TENANT_BASE/hostname (#6398)

This commit is contained in:
Thomas NOËL 2015-02-03 16:36:55 +01:00
parent 1f642209e6
commit bb762ce4d9
5 changed files with 8 additions and 14 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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:

View File

@ -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