<s> from django . conf import settings <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db import connection <EOL> from django . shortcuts import get_object_or_404 <EOL> from . utils import get_tenant_model , remove_www , get_public_schema_name , get_tenant_domain_model <EOL> class TenantMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def hostname_from_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return remove_www ( request . get_host ( ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] ) <EOL> def process_request ( self , request ) : <EOL> connection . set_schema_to_public ( ) <EOL> hostname = self . hostname_from_request ( request ) <EOL> domain = get_object_or_404 ( get_tenant_domain_model ( ) . objects . select_related ( '<STR_LIT>' ) , <EOL> domain = hostname ) <EOL> request . tenant = domain . tenant <EOL> connection . set_tenant ( request . tenant ) <EOL> ContentType . objects . clear_cache ( ) <EOL> if hasattr ( settings , '<STR_LIT>' ) and request . tenant . schema_name == get_public_schema_name ( ) : <EOL> request . urlconf = settings . PUBLIC_SCHEMA_URLCONF </s>
