<s> from django . core . cache import cache <EOL> from django . core . exceptions import MiddlewareNotUsed <EOL> from django . db . models . signals import post_save , post_delete <EOL> import app_settings <EOL> try : <EOL> from threading import local <EOL> except ImportError : <EOL> from django . utils . _threading_local import local <EOL> _thread_locals = local ( ) <EOL> def _build_section_path_map ( ) : <EOL> """<STR_LIT>""" <EOL> paths = { } <EOL> Section = app_settings . get_extending_model ( ) <EOL> for section in Section . objects . all ( ) : <EOL> paths [ section . full_path ] = section . slug <EOL> cache . set ( app_settings . PATH_CACHE_KEY , paths , app_settings . PATH_CACHE_TTL ) <EOL> return paths <EOL> def _get_section_path_map ( ) : <EOL> """<STR_LIT>""" <EOL> paths = cache . get ( app_settings . PATH_CACHE_KEY ) <EOL> if not paths : <EOL> paths = _build_section_path_map ( ) <EOL> cache . set ( <EOL> app_settings . PATH_CACHE_KEY , <EOL> paths , app_settings . PATH_CACHE_TTL <EOL> ) <EOL> return paths <EOL> def get_current_section ( ) : <EOL> """<STR_LIT>""" <EOL> if not getattr ( _thread_locals , '<STR_LIT>' , None ) : <EOL> raise MiddlewareNotUsed , ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> return getattr ( _thread_locals , '<STR_LIT>' , None ) <EOL> def lookup_section ( lookup_from ) : <EOL> """<STR_LIT>""" <EOL> Section = app_settings . get_extending_model ( ) <EOL> if lookup_from . __class__ . __name__ == "<STR_LIT>" : <EOL> path_map = _get_section_path_map ( ) <EOL> section_paths = path_map . keys ( ) <EOL> section_paths . sort ( lambda x , y : len ( x ) <= len ( y ) and <NUM_LIT:1> or - <NUM_LIT:1> ) <EOL> path = lookup_from . path . strip ( "<STR_LIT:/>" ) <EOL> path_matches = [ p for p in section_paths if path . startswith ( p ) ] <EOL> if len ( path_matches ) >= <NUM_LIT:1> and path_map . has_key ( path ) : <EOL> if app_settings . VALIDATE_GLOBALLY_UNIQUE_SLUGS : <EOL> return Section . objects . get ( slug = path_map [ path ] ) <EOL> else : <EOL> sections = Section . objects . filter ( slug = path_map [ path ] ) <EOL> if len ( sections ) == <NUM_LIT:1> : <EOL> return sections [ <NUM_LIT:0> ] <EOL> else : <EOL> for section in sections : <EOL> if section . full_path == path : <EOL> return section <EOL> return None <EOL> else : <EOL> try : <EOL> return Section . objects . get ( pk = int ( lookup_from ) ) <EOL> except Section . DoesNotExist : <EOL> return None <EOL> class SectionsMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> section = lookup_section ( request ) <EOL> _thread_locals . scaffold_middleware_enabled = True <EOL> _thread_locals . section = section <EOL> def reset_section_path_map ( sender , ** kwargs ) : <EOL> _build_section_path_map ( ) <EOL> post_save . connect ( reset_section_path_map , <EOL> sender = app_settings . get_extending_model ( ) , <EOL> dispatch_uid = "<STR_LIT>" <EOL> ) <EOL> post_delete . connect ( reset_section_path_map , <EOL> sender = app_settings . get_extending_model ( ) , <EOL> dispatch_uid = "<STR_LIT>" <EOL> ) </s>
