tenant_command now accepts options from the command to be wrapped. resolves #99

This commit is contained in:
Bernardo Pires 2013-12-22 18:01:51 +01:00
parent ab9eb6ac3d
commit 0ee3e47323
1 changed files with 27 additions and 2 deletions

View File

@ -1,5 +1,6 @@
from django.core.management.base import BaseCommand
from django.core.management import call_command
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 tenant_schemas.management.commands import InteractiveTenantOption
@ -7,6 +8,30 @@ from tenant_schemas.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("-s", "--schema", dest="schema_name", 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)