0001-tenants-in-TENANT_BASE-hostname.patch
entrouvert/djommon/multitenant/management/commands/create_tenant.py | ||
---|---|---|
13 | 13 |
if not args: |
14 | 14 |
raise CommandError("you must give at least one tenant hostname") |
15 | 15 | |
16 |
for arg in args: |
|
17 |
hostname = arg |
|
18 |
tenant_name = TenantMiddleware.hostname2schema(hostname) |
|
16 |
for hostname in args: |
|
19 | 17 |
try: |
20 | 18 |
tenant_base = TenantMiddleware.base() |
21 | 19 |
except AttributeError: |
22 | 20 |
raise CommandError("you must configure TENANT_BASE in your settings") |
23 | 21 |
if not tenant_base: |
24 | 22 |
raise CommandError("you must set a value to TENANT_BASE in your settings") |
25 |
tenant_dir = os.path.join(tenant_base, |
|
26 |
tenant_name) |
|
23 |
tenant_dir = os.path.join(tenant_base, hostname) |
|
27 | 24 |
if not os.path.exists(tenant_dir): |
28 | 25 |
os.mkdir(tenant_dir, 0755) |
29 | 26 |
for folder in ('media', 'static', 'templates'): |
... | ... | |
38 | 35 |
+ self.style.SQL_TABLE(tenant.schema_name) |
39 | 36 |
if not tenant.create_schema(check_if_exists=True): |
40 | 37 |
print self.style.ERROR(' Nothing to do: %r already exist' % hostname) |
41 |
entrouvert/djommon/multitenant/management/commands/deploy.py | ||
---|---|---|
22 | 22 | |
23 | 23 |
call_command('create_tenant', hostname) |
24 | 24 | |
25 |
tenant_name = TenantMiddleware.hostname2schema(hostname) |
|
26 |
tenant = TenantMiddleware.get_tenant_by_hostname(tenant_name) |
|
25 |
tenant = TenantMiddleware.get_tenant_by_hostname(hostname) |
|
27 | 26 |
with tenant_context(tenant): |
28 | 27 |
self.deploy_tenant(environment, service, options) |
29 | 28 |
entrouvert/djommon/multitenant/middleware.py | ||
---|---|---|
33 | 33 |
@classmethod |
34 | 34 |
def get_tenant_by_hostname(cls, hostname): |
35 | 35 |
'''Retrieve a tenant object for this hostname''' |
36 |
schema = cls.hostname2schema(hostname) |
|
37 |
p = os.path.join(cls.base(), schema) |
|
38 |
if not os.path.exists(p): |
|
36 |
if not os.path.exists(os.path.join(cls.base(), hostname)): |
|
39 | 37 |
raise TenantNotFound |
38 |
schema = cls.hostname2schema(hostname) |
|
40 | 39 |
return get_tenant_model()(schema_name=schema, domain_url=hostname) |
41 | 40 | |
42 | 41 |
@classmethod |
... | ... | |
118 | 117 |
FILENAME = None |
119 | 118 | |
120 | 119 |
def load_tenant_settings(self, wrapped, tenant, tenant_settings, last_time): |
121 |
path = os.path.join(settings.TENANT_BASE, tenant.schema_name, self.FILENAME)
|
|
120 |
path = os.path.join(settings.TENANT_BASE, tenant.domain_url, self.FILENAME)
|
|
122 | 121 |
try: |
123 | 122 |
new_time = os.stat(path).st_mtime |
124 | 123 |
except OSError: |
entrouvert/djommon/multitenant/storage.py | ||
---|---|---|
14 | 14 |
'''Lookup files first in $TENANT_BASE/<tenant.schema>/media/ then in default location''' |
15 | 15 |
def path(self, name): |
16 | 16 |
if connection.tenant: |
17 |
location = safe_join(settings.TENANT_BASE, connection.tenant.schema_name, 'media')
|
|
17 |
location = safe_join(settings.TENANT_BASE, connection.tenant.domain_url, 'media')
|
|
18 | 18 |
else: |
19 | 19 |
location = self.location |
20 | 20 |
try: |
entrouvert/djommon/multitenant/template_loader.py | ||
---|---|---|
89 | 89 |
raise ImproperlyConfigured('To use %s.%s you must define the TENANT_TEMPLATE_DIRS' % (__name__, FilesystemLoader.__name__)) |
90 | 90 |
for template_dir in template_dirs: |
91 | 91 |
try: |
92 |
yield safe_join(template_dir, connection.tenant.schema_name, 'templates', template_name)
|
|
92 |
yield safe_join(template_dir, connection.tenant.domain_url, 'templates', template_name)
|
|
93 | 93 |
except UnicodeDecodeError: |
94 | 94 |
# The template dir name was a bytestring that wasn't valid UTF-8. |
95 | 95 |
raise |
96 |
- |