1 |
7e82bd8e
|
Jérôme Schneider
|
import os
|
2 |
|
|
|
3 |
|
|
from django.db import connection
|
4 |
|
|
from django.core.management.base import CommandError, BaseCommand
|
5 |
|
|
|
6 |
|
|
from entrouvert.djommon.multitenant.middleware import TenantMiddleware
|
7 |
|
|
|
8 |
|
|
class Command(BaseCommand):
|
9 |
|
|
help = "Create tenant(s) by hostname(s)"
|
10 |
|
|
|
11 |
|
|
def handle(self, *args, **options):
|
12 |
|
|
verbosity = int(options.get('verbosity'))
|
13 |
|
|
if not args:
|
14 |
|
|
raise CommandError("you must give at least one tenant hostname")
|
15 |
|
|
|
16 |
|
|
for arg in args:
|
17 |
|
|
hostname = arg
|
18 |
|
|
tenant_name = TenantMiddleware.hostname2schema(hostname)
|
19 |
|
|
try:
|
20 |
|
|
tenant_base = TenantMiddleware.base()
|
21 |
|
|
except AttributeError:
|
22 |
|
|
raise CommandError("you must configure TENANT_BASE in your settings")
|
23 |
|
|
if not tenant_base:
|
24 |
|
|
raise CommandError("you must set a value to TENANT_BASE in your settings")
|
25 |
|
|
tenant_dir = os.path.join(tenant_base,
|
26 |
|
|
tenant_name)
|
27 |
|
|
if not os.path.exists(tenant_dir):
|
28 |
|
|
os.mkdir(tenant_dir, 0755)
|
29 |
|
|
for folder in ('media', 'static', 'templates'):
|
30 |
|
|
path = os.path.join(tenant_dir, folder)
|
31 |
|
|
if not os.path.exists(path):
|
32 |
|
|
os.mkdir(path, 0755)
|
33 |
|
|
connection.set_schema_to_public()
|
34 |
|
|
tenant = TenantMiddleware.get_tenant_by_hostname(hostname)
|
35 |
|
|
if verbosity >= 1:
|
36 |
|
|
print
|
37 |
|
|
print self.style.NOTICE("=== Creating schema ") \
|
38 |
|
|
+ self.style.SQL_TABLE(tenant.schema_name)
|
39 |
|
|
if not tenant.create_schema(check_if_exists=True):
|
40 |
|
|
print self.style.ERROR(' Nothing to do: %r already exist' % hostname)
|