<s> from django . conf import settings <EOL> from django . core . cache import cache <EOL> from django . contrib . sites . models import Site <EOL> from djangotoolbox . utils import make_tls_property <EOL> _default_site_id = getattr ( settings , '<STR_LIT>' , None ) <EOL> SITE_ID = settings . __class__ . SITE_ID = make_tls_property ( ) <EOL> class DynamicSiteIDMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> if '<STR_LIT::>' in request . get_host ( ) : <EOL> domain , port = request . get_host ( ) . split ( '<STR_LIT::>' ) <EOL> if int ( port ) not in ( <NUM_LIT> , <NUM_LIT> ) : <EOL> domain = request . get_host ( ) <EOL> else : <EOL> domain = request . get_host ( ) . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> domain = domain . lower ( ) <EOL> cache_key = '<STR_LIT>' % domain <EOL> site = cache . get ( cache_key ) <EOL> if site : <EOL> SITE_ID . value = site <EOL> else : <EOL> try : <EOL> site = Site . objects . get ( domain = domain ) <EOL> except Site . DoesNotExist : <EOL> site = None <EOL> if not site : <EOL> if domain . startswith ( '<STR_LIT>' ) : <EOL> fallback_domain = domain [ <NUM_LIT:4> : ] <EOL> else : <EOL> fallback_domain = '<STR_LIT>' + domain <EOL> try : <EOL> site = Site . objects . get ( domain = fallback_domain ) <EOL> except Site . DoesNotExist : <EOL> site = None <EOL> if not site and getattr ( settings , '<STR_LIT>' , <EOL> True ) : <EOL> site = Site ( domain = domain , name = domain ) <EOL> site . save ( ) <EOL> if site : <EOL> SITE_ID . value = site . pk <EOL> else : <EOL> SITE_ID . value = _default_site_id <EOL> cache . set ( cache_key , SITE_ID . value , <NUM_LIT:5> * <NUM_LIT> ) </s>
