<s> from django import template <EOL> from django . apps import apps <EOL> from django . utils . encoding import iri_to_uri <EOL> from django . utils . six . moves . urllib . parse import urljoin <EOL> register = template . Library ( ) <EOL> class PrefixNode ( template . Node ) : <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . name <EOL> def __init__ ( self , varname = None , name = None ) : <EOL> if name is None : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" ) <EOL> self . varname = varname <EOL> self . name = name <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token , name ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . contents . split ( ) <EOL> if len ( tokens ) > <NUM_LIT:1> and tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if len ( tokens ) > <NUM_LIT:1> : <EOL> varname = tokens [ <NUM_LIT:2> ] <EOL> else : <EOL> varname = None <EOL> return cls ( varname , name ) <EOL> @ classmethod <EOL> def handle_simple ( cls , name ) : <EOL> try : <EOL> from django . conf import settings <EOL> except ImportError : <EOL> prefix = '<STR_LIT>' <EOL> else : <EOL> prefix = iri_to_uri ( getattr ( settings , name , '<STR_LIT>' ) ) <EOL> return prefix <EOL> def render ( self , context ) : <EOL> prefix = self . handle_simple ( self . name ) <EOL> if self . varname is None : <EOL> return prefix <EOL> context [ self . varname ] = prefix <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_static_prefix ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return PrefixNode . handle_token ( parser , token , "<STR_LIT>" ) <EOL> @ register . tag <EOL> def get_media_prefix ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return PrefixNode . handle_token ( parser , token , "<STR_LIT>" ) <EOL> class StaticNode ( template . Node ) : <EOL> def __init__ ( self , varname = None , path = None ) : <EOL> if path is None : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" ) <EOL> self . path = path <EOL> self . varname = varname <EOL> def url ( self , context ) : <EOL> path = self . path . resolve ( context ) <EOL> return self . handle_simple ( path ) <EOL> def render ( self , context ) : <EOL> url = self . url ( context ) <EOL> if self . varname is None : <EOL> return url <EOL> context [ self . varname ] = url <EOL> return '<STR_LIT>' <EOL> @ classmethod <EOL> def handle_simple ( cls , path ) : <EOL> if apps . is_installed ( '<STR_LIT>' ) : <EOL> from django . contrib . staticfiles . storage import staticfiles_storage <EOL> return staticfiles_storage . url ( path ) <EOL> else : <EOL> return urljoin ( PrefixNode . handle_simple ( "<STR_LIT>" ) , path ) <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) < <NUM_LIT:2> : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> path = parser . compile_filter ( bits [ <NUM_LIT:1> ] ) <EOL> if len ( bits ) >= <NUM_LIT:2> and bits [ - <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> varname = bits [ <NUM_LIT:3> ] <EOL> else : <EOL> varname = None <EOL> return cls ( varname , path ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_static ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return StaticNode . handle_token ( parser , token ) <EOL> def static ( path ) : <EOL> """<STR_LIT>""" <EOL> return StaticNode . handle_simple ( path ) </s>
