<s> import urllib <EOL> from urlparse import urlparse <EOL> from django . conf import settings <EOL> from django . core . handlers . wsgi import WSGIHandler <EOL> from django . contrib . staticfiles import utils <EOL> from django . contrib . staticfiles . views import serve <EOL> class StaticFilesHandler ( WSGIHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , application , base_dir = None ) : <EOL> self . application = application <EOL> if base_dir : <EOL> self . base_dir = base_dir <EOL> else : <EOL> self . base_dir = self . get_base_dir ( ) <EOL> self . base_url = urlparse ( self . get_base_url ( ) ) <EOL> super ( StaticFilesHandler , self ) . __init__ ( ) <EOL> def get_base_dir ( self ) : <EOL> return settings . STATIC_ROOT <EOL> def get_base_url ( self ) : <EOL> utils . check_settings ( ) <EOL> return settings . STATIC_URL <EOL> def _should_handle ( self , path ) : <EOL> """<STR_LIT>""" <EOL> return ( self . base_url [ <NUM_LIT:2> ] != path and <EOL> path . startswith ( self . base_url [ <NUM_LIT:2> ] ) and not self . base_url [ <NUM_LIT:1> ] ) <EOL> def file_path ( self , url ) : <EOL> """<STR_LIT>""" <EOL> relative_url = url [ len ( self . base_url [ <NUM_LIT:2> ] ) : ] <EOL> return urllib . url2pathname ( relative_url ) <EOL> def serve ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return serve ( request , self . file_path ( request . path ) , insecure = True ) <EOL> def get_response ( self , request ) : <EOL> from django . http import Http404 <EOL> if self . _should_handle ( request . path ) : <EOL> try : <EOL> return self . serve ( request ) <EOL> except Http404 , e : <EOL> if settings . DEBUG : <EOL> from django . views import debug <EOL> return debug . technical_404_response ( request , e ) <EOL> return super ( StaticFilesHandler , self ) . get_response ( request ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> if not self . _should_handle ( environ [ '<STR_LIT>' ] ) : <EOL> return self . application ( environ , start_response ) <EOL> return super ( StaticFilesHandler , self ) . __call__ ( environ , start_response ) </s>
