Skip to content

Commit

Permalink
Fixed tenant_command not wrapping commands properly in django 1.7+. F…
Browse files Browse the repository at this point in the history
…ixes #267.
  • Loading branch information
bernardopires committed Jul 30, 2015
1 parent e7a2e87 commit 5cdc7ef
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tenant_schemas/management/commands/tenant_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from optparse import make_option
import argparse
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
Expand Down Expand Up @@ -26,10 +26,18 @@ def run_from_argv(self, argv):
else:
klass = load_command_class(app_name, argv[2])

super(Command, self).run_from_argv(argv)
# Ugly, but works. Delete tenant_command from the argv, parse the schema manually
# and forward the rest of the arguments to the actual command being wrapped.
del argv[1]
schema_parser = argparse.ArgumentParser()
schema_parser.add_argument("-s", "--schema", dest="schema_name", help="specify tenant schema")
schema_namespace, args = schema_parser.parse_known_args(argv)

tenant = self.get_tenant_from_options_or_interactive(schema_name=schema_namespace.schema_name)
connection.set_tenant(tenant)
klass.run_from_argv(args)

def handle(self, *args, **options):
tenant = self.get_tenant_from_options_or_interactive(**options)
connection.set_tenant(tenant)

call_command(*args, **options)

0 comments on commit 5cdc7ef

Please sign in to comment.