<s> from optparse import make_option <EOL> from django . core import exceptions <EOL> from django . core . management . base import BaseCommand <EOL> from django . utils . encoding import force_str <EOL> from django . utils . six . moves import input <EOL> from django . db . utils import IntegrityError <EOL> from django . db import connection <EOL> from django_tenants . clone import CloneSchema <EOL> from django_tenants . utils import get_tenant_model , get_tenant_domain_model <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> tenant_fields = [ field for field in get_tenant_model ( ) . _meta . fields <EOL> if field . editable and not field . primary_key ] <EOL> domain_fields = [ field for field in get_tenant_domain_model ( ) . _meta . fields <EOL> if field . editable and not field . primary_key ] <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Command , self ) . __init__ ( * args , ** kwargs ) <EOL> self . option_list = BaseCommand . option_list <EOL> self . option_list += ( make_option ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , ) <EOL> for field in self . tenant_fields : <EOL> self . option_list += ( make_option ( '<STR_LIT>' % field . name , <EOL> help = '<STR_LIT>' % field . name ) , ) <EOL> for field in self . domain_fields : <EOL> self . option_list += ( make_option ( '<STR_LIT>' % field . name , <EOL> help = "<STR_LIT>" % field . name ) , ) <EOL> def handle ( self , * args , ** options ) : <EOL> tenant_data = { } <EOL> for field in self . tenant_fields : <EOL> input_value = options . get ( field . name , None ) <EOL> tenant_data [ field . name ] = input_value <EOL> domain_data = { } <EOL> for field in self . domain_fields : <EOL> input_value = options . get ( field . name , None ) <EOL> domain_data [ field . name ] = input_value <EOL> clone_schema_from = options . get ( '<STR_LIT>' ) <EOL> while clone_schema_from == '<STR_LIT>' or clone_schema_from is None : <EOL> clone_schema_from = input ( force_str ( '<STR_LIT>' ) ) <EOL> tenant = None <EOL> while True : <EOL> for field in self . tenant_fields : <EOL> if tenant_data . get ( field . name , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> input_msg = field . verbose_name <EOL> default = field . get_default ( ) <EOL> if default : <EOL> input_msg = "<STR_LIT>" % ( input_msg , default ) <EOL> input_value = input ( force_str ( '<STR_LIT>' % input_msg ) ) or default <EOL> tenant_data [ field . name ] = input_value <EOL> tenant = self . store_tenant ( clone_schema_from , ** tenant_data ) <EOL> if tenant is not None : <EOL> break <EOL> tenant_data = { } <EOL> while True : <EOL> domain_data [ '<STR_LIT>' ] = tenant <EOL> for field in self . domain_fields : <EOL> if domain_data . get ( field . name , '<STR_LIT>' ) == '<STR_LIT>' : <EOL> input_msg = field . verbose_name <EOL> default = field . get_default ( ) <EOL> if default : <EOL> input_msg = "<STR_LIT>" % ( input_msg , default ) <EOL> input_value = input ( force_str ( '<STR_LIT>' % input_msg ) ) or default <EOL> domain_data [ field . name ] = input_value <EOL> domain = self . store_tenant_domain ( ** domain_data ) <EOL> if domain is not None : <EOL> break <EOL> domain_data = { } <EOL> def store_tenant ( self , clone_schema_from , ** fields ) : <EOL> connection . set_schema_to_public ( ) <EOL> cursor = connection . cursor ( ) <EOL> try : <EOL> tenant = get_tenant_model ( ) ( ** fields ) <EOL> tenant . auto_create_schema = False <EOL> tenant . save ( ) <EOL> clone_schema = CloneSchema ( cursor ) <EOL> clone_schema . clone ( clone_schema_from , tenant . schema_name ) <EOL> return tenant <EOL> except exceptions . ValidationError as e : <EOL> self . stderr . write ( "<STR_LIT>" % '<STR_LIT>' . join ( e . messages ) ) <EOL> return None <EOL> except IntegrityError : <EOL> return None <EOL> def store_tenant_domain ( self , ** fields ) : <EOL> try : <EOL> domain = get_tenant_domain_model ( ) . objects . create ( ** fields ) <EOL> domain . save ( ) <EOL> return domain <EOL> except exceptions . ValidationError as e : <EOL> self . stderr . write ( "<STR_LIT>" % '<STR_LIT>' . join ( e . messages ) ) <EOL> return None <EOL> except IntegrityError : <EOL> return None </s>
