<s> from __future__ import unicode_literals <EOL> from future . builtins import str <EOL> from django import template <EOL> from django . utils . http import urlquote <EOL> from filebrowser_safe . settings import SELECT_FORMATS , EXTENSIONS <EOL> register = template . Library ( ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def query_string ( context , add = None , remove = None ) : <EOL> """<STR_LIT>""" <EOL> add = string_to_dict ( add ) <EOL> remove = string_to_list ( remove ) <EOL> params = context [ '<STR_LIT>' ] . copy ( ) <EOL> response = get_query_string ( params , add , remove ) <EOL> return { '<STR_LIT>' : response } <EOL> def query_helper ( query , add = None , remove = None ) : <EOL> """<STR_LIT>""" <EOL> add = string_to_dict ( add ) <EOL> remove = string_to_list ( remove ) <EOL> params = query . copy ( ) <EOL> return get_query_string ( params , add , remove ) <EOL> def get_query_string ( p , new_params = None , remove = None ) : <EOL> """<STR_LIT>""" <EOL> if new_params is None : <EOL> new_params = { } <EOL> if remove is None : <EOL> remove = [ ] <EOL> for r in remove : <EOL> for k in list ( p . keys ( ) ) : <EOL> if k == r : <EOL> del p [ k ] <EOL> for k , v in list ( new_params . items ( ) ) : <EOL> if k in p and v is None : <EOL> del p [ k ] <EOL> elif v is not None : <EOL> p [ k ] = v <EOL> return '<STR_LIT:?>' + '<STR_LIT:&>' . join ( [ u'<STR_LIT>' % ( urlquote ( k ) , urlquote ( v ) ) for k , v in p . items ( ) ] ) <EOL> def string_to_dict ( string ) : <EOL> """<STR_LIT>""" <EOL> kwargs = { } <EOL> if string : <EOL> string = str ( string ) <EOL> if '<STR_LIT:U+002C>' not in string : <EOL> string += '<STR_LIT:U+002C>' <EOL> for arg in string . split ( '<STR_LIT:U+002C>' ) : <EOL> arg = arg . strip ( ) <EOL> if arg == '<STR_LIT>' : <EOL> continue <EOL> kw , val = arg . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> kwargs [ kw ] = val <EOL> return kwargs <EOL> def string_to_list ( string ) : <EOL> """<STR_LIT>""" <EOL> args = [ ] <EOL> if string : <EOL> string = str ( string ) <EOL> if '<STR_LIT:U+002C>' not in string : <EOL> string += '<STR_LIT:U+002C>' <EOL> for arg in string . split ( '<STR_LIT:U+002C>' ) : <EOL> arg = arg . strip ( ) <EOL> if arg == '<STR_LIT>' : <EOL> continue <EOL> args . append ( arg ) <EOL> return args <EOL> class SelectableNode ( template . Node ) : <EOL> def __init__ ( self , filetype , format ) : <EOL> self . filetype = template . Variable ( filetype ) <EOL> self . format = template . Variable ( format ) <EOL> def render ( self , context ) : <EOL> try : <EOL> filetype = self . filetype . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> filetype = '<STR_LIT>' <EOL> try : <EOL> format = self . format . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> format = '<STR_LIT>' <EOL> if filetype and format and filetype in SELECT_FORMATS [ format ] : <EOL> selectable = True <EOL> elif filetype and format and filetype not in SELECT_FORMATS [ format ] : <EOL> selectable = False <EOL> else : <EOL> selectable = True <EOL> context [ '<STR_LIT>' ] = selectable <EOL> return '<STR_LIT>' <EOL> def selectable ( parser , token ) : <EOL> try : <EOL> tag , filetype , format = token . split_contents ( ) <EOL> except : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % token . contents . split ( ) [ <NUM_LIT:0> ] ) <EOL> return SelectableNode ( filetype , format ) <EOL> register . tag ( selectable ) <EOL> def allowed_extensions_list ( separator = '<STR_LIT:U+002C>' ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> for key in EXTENSIONS : <EOL> if key != '<STR_LIT>' : <EOL> output += EXTENSIONS [ key ] <EOL> return separator . join ( output ) <EOL> register . simple_tag ( allowed_extensions_list ) </s>
