6 |
6 |
# Home-page: http://github.com/bcarneiro/django-tenant-schemas
|
7 |
7 |
|
8 |
8 |
import argparse
|
|
9 |
import sys
|
9 |
10 |
|
10 |
11 |
from django.conf import settings
|
11 |
|
from django.core.management.base import BaseCommand, CommandError
|
|
12 |
from django.core.exceptions import ImproperlyConfigured
|
|
13 |
from django.core.management.base import (BaseCommand, CommandError,
|
|
14 |
SystemCheckError, handle_default_options)
|
12 |
15 |
from django.core.management import call_command, get_commands, load_command_class
|
13 |
|
from django.db import connection
|
|
16 |
from django.db import connection, connections
|
14 |
17 |
|
15 |
18 |
from hobo.multitenant.management.commands import InteractiveTenantOption
|
16 |
19 |
from hobo.multitenant.middleware import TenantMiddleware
|
17 |
20 |
|
|
21 |
def run_command_from_argv(command, argv):
|
|
22 |
# copied/adapted from Django run_from_argv
|
|
23 |
command._called_from_command_line = True
|
|
24 |
parser = command.create_parser(argv[0], argv[1])
|
|
25 |
|
|
26 |
options = parser.parse_args(argv[2:])
|
|
27 |
cmd_options = vars(options)
|
|
28 |
# Move positional args out of options to mimic legacy optparse
|
|
29 |
args = cmd_options.pop('args', ())
|
|
30 |
handle_default_options(options)
|
|
31 |
try:
|
|
32 |
command.execute(*args, **cmd_options)
|
|
33 |
except Exception as e:
|
|
34 |
if options.traceback:
|
|
35 |
raise
|
|
36 |
|
|
37 |
# SystemCheckError takes care of its own formatting.
|
|
38 |
if isinstance(e, SystemCheckError):
|
|
39 |
command.stderr.write(str(e), lambda x: x)
|
|
40 |
else:
|
|
41 |
command.stderr.write('%s: %s: %s' % (
|
|
42 |
connection.get_tenant(), e.__class__.__name__, e))
|
|
43 |
return e
|
|
44 |
finally:
|
|
45 |
try:
|
|
46 |
connections.close_all()
|
|
47 |
except ImproperlyConfigured:
|
|
48 |
# Ignore if connections aren't setup at this point (e.g. no
|
|
49 |
# configured settings).
|
|
50 |
pass
|
|
51 |
|
|
52 |
|
18 |
53 |
class Command(InteractiveTenantOption, BaseCommand):
|
19 |
54 |
help = "Wrapper around django commands for use with an individual tenant"
|
20 |
55 |
args = '<other_command>'
|
... | ... | |
64 |
99 |
return
|
65 |
100 |
|
66 |
101 |
if args_namespace.all_tenants:
|
|
102 |
errors = []
|
67 |
103 |
for tenant in TenantMiddleware.get_tenants():
|
68 |
104 |
if args_verbosity.verbosity > 1:
|
69 |
105 |
print(u'* Running command on tenant %s' % tenant.domain_url)
|
70 |
106 |
connection.set_tenant(tenant)
|
71 |
|
klass.run_from_argv(args)
|
|
107 |
error = run_command_from_argv(klass, args)
|
|
108 |
if error:
|
|
109 |
errors.append(error)
|
|
110 |
if errors:
|
|
111 |
self.stderr.write('Command failed on multiple tenants')
|
|
112 |
sys.exit(1)
|
72 |
113 |
else:
|
73 |
114 |
tenant = self.get_tenant_from_options_or_interactive(domain=args_namespace.domain_name)
|
74 |
115 |
connection.set_tenant(tenant)
|
75 |
|
-
|