Projet

Général

Profil

Télécharger (1,46 ko) Statistiques
| Branche: | Tag: | Révision:

root / entrouvert / djommon / multitenant / management / commands / create_tenant.py @ 01a055c6

1
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 hostname in args:
17
            try:
18
                tenant_base = TenantMiddleware.base()
19
            except AttributeError:
20
                raise CommandError("you must configure TENANT_BASE in your settings")
21
            if not tenant_base:
22
                raise CommandError("you must set a value to TENANT_BASE in your settings")
23
            tenant_dir = os.path.join(tenant_base, hostname)
24
            if not os.path.exists(tenant_dir):
25
                os.mkdir(tenant_dir, 0755)
26
            for folder in ('media', 'static', 'templates'):
27
                path = os.path.join(tenant_dir, folder)
28
                if not os.path.exists(path):
29
                    os.mkdir(path, 0755)
30
            connection.set_schema_to_public()
31
            tenant = TenantMiddleware.get_tenant_by_hostname(hostname)
32
            if verbosity >= 1:
33
                print
34
                print self.style.NOTICE("=== Creating schema ") \
35
                    + self.style.SQL_TABLE(tenant.schema_name)
36
            tenant.create_schema(check_if_exists=True)
(3-3/12)