This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
python-entrouvert/entrouvert/djommon/multitenant/management/commands/tenant_command.py

41 lines
1.5 KiB
Python

# 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])
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)