<s> from contextlib import contextmanager <EOL> from django . conf import settings <EOL> from django . db import connection <EOL> try : <EOL> from django . apps import apps <EOL> get_model = apps . get_model <EOL> except ImportError : <EOL> from django . db . models . loading import get_model <EOL> from django . core import mail <EOL> @ contextmanager <EOL> def schema_context ( schema_name ) : <EOL> previous_tenant = connection . tenant <EOL> try : <EOL> connection . set_schema ( schema_name ) <EOL> yield <EOL> finally : <EOL> if previous_tenant is None : <EOL> connection . set_schema_to_public ( ) <EOL> else : <EOL> connection . set_tenant ( previous_tenant ) <EOL> @ contextmanager <EOL> def tenant_context ( tenant ) : <EOL> previous_tenant = connection . tenant <EOL> try : <EOL> connection . set_tenant ( tenant ) <EOL> yield <EOL> finally : <EOL> if previous_tenant is None : <EOL> connection . set_schema_to_public ( ) <EOL> else : <EOL> connection . set_tenant ( previous_tenant ) <EOL> def get_tenant_model ( ) : <EOL> return get_model ( settings . TENANT_MODEL ) <EOL> def get_tenant_domain_model ( ) : <EOL> return get_model ( settings . TENANT_DOMAIN_MODEL ) <EOL> def get_public_schema_name ( ) : <EOL> return getattr ( settings , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_limit_set_calls ( ) : <EOL> return getattr ( settings , '<STR_LIT>' , False ) <EOL> def clean_tenant_url ( url_string ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> if ( settings . PUBLIC_SCHEMA_URLCONF and <EOL> url_string . startswith ( settings . PUBLIC_SCHEMA_URLCONF ) ) : <EOL> url_string = url_string [ len ( settings . PUBLIC_SCHEMA_URLCONF ) : ] <EOL> return url_string <EOL> def remove_www_and_dev ( hostname ) : <EOL> """<STR_LIT>""" <EOL> return remove_www ( hostname ) <EOL> def remove_www ( hostname ) : <EOL> """<STR_LIT>""" <EOL> if hostname . startswith ( "<STR_LIT>" ) : <EOL> return hostname [ <NUM_LIT:4> : ] <EOL> return hostname <EOL> def django_is_in_test_mode ( ) : <EOL> """<STR_LIT>""" <EOL> return hasattr ( mail , '<STR_LIT>' ) <EOL> def schema_exists ( schema_name ) : <EOL> cursor = connection . cursor ( ) <EOL> sql = '<STR_LIT>' <EOL> cursor . execute ( sql , ( schema_name , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> exists = row [ <NUM_LIT:0> ] <EOL> else : <EOL> exists = False <EOL> cursor . close ( ) <EOL> return exists <EOL> def app_labels ( apps_list ) : <EOL> """<STR_LIT>""" <EOL> return [ app . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] for app in apps_list ] </s>
