authentik.tenants.management
authentik tenants management command utils
1"""authentik tenants management command utils""" 2 3from django.core.management.base import BaseCommand 4from django.db import connection 5from django_tenants.utils import get_public_schema_name 6 7from authentik.tenants.models import Tenant 8 9 10class TenantCommand(BaseCommand): 11 """Generic command class useful for running any existing command 12 on a particular tenant.""" 13 14 def create_parser(self, prog_name, subcommand, **kwargs): 15 parser = super().create_parser(prog_name, subcommand, **kwargs) 16 self.add_base_argument( 17 parser, 18 "-s", 19 "--schema", 20 default=get_public_schema_name(), 21 help="Tenant schema name.", 22 dest="schema_name", 23 ) 24 return parser 25 26 def handle(self, *args, **options): 27 verbosity = int(options.get("verbosity")) 28 29 schema_name = options["schema_name"] or self.schema_name 30 connection.set_schema_to_public() 31 if verbosity >= 1: 32 self.stderr.write( 33 self.style.NOTICE("Switching to schema '") 34 + self.style.SQL_TABLE(schema_name) 35 + self.style.NOTICE("'") 36 ) 37 connection.set_tenant(Tenant.objects.get(schema_name=schema_name)) 38 self.handle_per_tenant(*args, **options) 39 40 def handle_per_tenant(self, *args, **options): 41 """The actual logic of the command.""" 42 raise NotImplementedError( 43 "subclasses of TenantCommand must provide a handle_per_tenant() method" 44 )
class
TenantCommand(django.core.management.base.BaseCommand):
11class TenantCommand(BaseCommand): 12 """Generic command class useful for running any existing command 13 on a particular tenant.""" 14 15 def create_parser(self, prog_name, subcommand, **kwargs): 16 parser = super().create_parser(prog_name, subcommand, **kwargs) 17 self.add_base_argument( 18 parser, 19 "-s", 20 "--schema", 21 default=get_public_schema_name(), 22 help="Tenant schema name.", 23 dest="schema_name", 24 ) 25 return parser 26 27 def handle(self, *args, **options): 28 verbosity = int(options.get("verbosity")) 29 30 schema_name = options["schema_name"] or self.schema_name 31 connection.set_schema_to_public() 32 if verbosity >= 1: 33 self.stderr.write( 34 self.style.NOTICE("Switching to schema '") 35 + self.style.SQL_TABLE(schema_name) 36 + self.style.NOTICE("'") 37 ) 38 connection.set_tenant(Tenant.objects.get(schema_name=schema_name)) 39 self.handle_per_tenant(*args, **options) 40 41 def handle_per_tenant(self, *args, **options): 42 """The actual logic of the command.""" 43 raise NotImplementedError( 44 "subclasses of TenantCommand must provide a handle_per_tenant() method" 45 )
Generic command class useful for running any existing command on a particular tenant.
def
create_parser(self, prog_name, subcommand, **kwargs):
15 def create_parser(self, prog_name, subcommand, **kwargs): 16 parser = super().create_parser(prog_name, subcommand, **kwargs) 17 self.add_base_argument( 18 parser, 19 "-s", 20 "--schema", 21 default=get_public_schema_name(), 22 help="Tenant schema name.", 23 dest="schema_name", 24 ) 25 return parser
Create and return the ArgumentParser which will be used to
parse the arguments to this command.
def
handle(self, *args, **options):
27 def handle(self, *args, **options): 28 verbosity = int(options.get("verbosity")) 29 30 schema_name = options["schema_name"] or self.schema_name 31 connection.set_schema_to_public() 32 if verbosity >= 1: 33 self.stderr.write( 34 self.style.NOTICE("Switching to schema '") 35 + self.style.SQL_TABLE(schema_name) 36 + self.style.NOTICE("'") 37 ) 38 connection.set_tenant(Tenant.objects.get(schema_name=schema_name)) 39 self.handle_per_tenant(*args, **options)
The actual logic of the command. Subclasses must implement this method.