root/entrouvert/djommon/multitenant/management/commands/tenant_command.py @ ccec1ff4
| ccec1ff4 | Benjamin Dauvergne | # this file derive from django-tenant-schemas
|
|
# Author: Bernardo Pires Carneiro
|
|||
# Email: carneiro.be@gmail.com
|
|||
# License: MIT license
|
|||
# Home-page: http://github.com/bcarneiro/django-tenant-schemas
|
|||
from optparse import make_option
|
|||
from django.core.management.base import BaseCommand, CommandError
|
|||
from django.core.management import call_command, get_commands, load_command_class
|
|||
from django.db import connection
|
|||
from entrouvert.djommon.multitenant.management.commands import InteractiveTenantOption
|
|||
class Command(InteractiveTenantOption, BaseCommand):
|
|||
help = "Wrapper around django commands for use with an individual tenant"
|
|||
def run_from_argv(self, argv):
|
|||
"""
|
|||
Changes the option_list to use the options from the wrapped command.
|
|||
Adds schema parameter to specifiy which schema will be used when
|
|||
executing the wrapped command.
|
|||
"""
|
|||
# load the command object.
|
|||
try:
|
|||
app_name = get_commands()[argv[2]]
|
|||
except KeyError:
|
|||
raise CommandError("Unknown command: %r" % argv[2])
|
|||
if isinstance(app_name, BaseCommand):
|
|||
# if the command is already loaded, use it directly.
|
|||
klass = app_name
|
|||
else:
|
|||
klass = load_command_class(app_name, argv[2])
|
|||
self.option_list = klass.option_list + (
|
|||
make_option("-d", "--domain", dest="domain", help="specify tenant schema"),
|
|||
)
|
|||
super(Command, self).run_from_argv(argv)
|
|||
def handle(self, *args, **options):
|
|||
tenant = self.get_tenant_from_options_or_interactive(**options)
|
|||
connection.set_tenant(tenant)
|
|||
call_command(*args, **options)
|