Project

General

Profile

0001-tenants-in-TENANT_BASE-hostname.patch

Thomas Noël, 03 February 2015 04:41 PM

Download (5.56 KB)

View differences:

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(-)
entrouvert/djommon/multitenant/management/commands/create_tenant.py
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'):
......
+ 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)
entrouvert/djommon/multitenant/management/commands/deploy.py
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)
entrouvert/djommon/multitenant/middleware.py
@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
......
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:
entrouvert/djommon/multitenant/storage.py
'''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:
entrouvert/djommon/multitenant/template_loader.py
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