<s> import re <EOL> import urllib <EOL> from django import template <EOL> from django . template . defaultfilters import stringfilter <EOL> from django . template import Template , Variable , TemplateSyntaxError <EOL> from django . http import HttpResponse <EOL> from django . db . models . query import QuerySet <EOL> from django . template . loader_tags import BlockNode , ExtendsNode <EOL> from django . template import loader , Context , RequestContext , TextNode <EOL> from djcommon . helpers import random_slice_list <EOL> register = template . Library ( ) <EOL> def get_template ( template ) : <EOL> if isinstance ( template , ( tuple , list ) ) : <EOL> return loader . select_template ( template ) <EOL> return loader . get_template ( template ) <EOL> class BlockNotFound ( Exception ) : <EOL> pass <EOL> def render_template_block ( template , block , context ) : <EOL> """<STR_LIT>""" <EOL> return render_template_block_nodelist ( template . nodelist , block , context ) <EOL> def render_template_block_nodelist ( nodelist , block , context ) : <EOL> for node in nodelist : <EOL> if isinstance ( node , BlockNode ) and node . name == block : <EOL> return node . render ( context ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if hasattr ( node , key ) : <EOL> try : <EOL> return render_template_block_nodelist ( getattr ( node , key ) , block , context ) <EOL> except : <EOL> pass <EOL> for node in nodelist : <EOL> if isinstance ( node , ExtendsNode ) : <EOL> try : <EOL> return render_template_block ( node . get_parent ( context ) , block , context ) <EOL> except BlockNotFound : <EOL> pass <EOL> raise BlockNotFound ( block ) <EOL> def render_block_to_string ( template_name , block , dictionary = None , context_instance = None ) : <EOL> """<STR_LIT>""" <EOL> import re <EOL> dictionary = dictionary or { } <EOL> t = get_template ( template_name ) <EOL> if context_instance : <EOL> context_instance . update ( dictionary ) <EOL> else : <EOL> context_instance = Context ( dictionary ) <EOL> template_block = render_template_block ( t , block , context_instance ) <EOL> return re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , template_block ) <EOL> def direct_block_to_template ( request , template , block , extra_context = None , mimetype = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if extra_context is None : <EOL> extra_context = { } <EOL> dictionary = { '<STR_LIT>' : kwargs } <EOL> for key , value in extra_context . items ( ) : <EOL> if callable ( value ) : <EOL> dictionary [ key ] = value ( ) <EOL> else : <EOL> dictionary [ key ] = value <EOL> c = RequestContext ( request , dictionary ) <EOL> t = get_template ( template ) <EOL> t . render ( c ) <EOL> return HttpResponse ( render_template_block ( t , block , c ) , mimetype = mimetype ) <EOL> class RenderAsTemplateNode ( template . Node ) : <EOL> def __init__ ( self , item_to_be_rendered ) : <EOL> self . item_to_be_rendered = Variable ( item_to_be_rendered ) <EOL> def render ( self , context ) : <EOL> try : <EOL> actual_item = self . item_to_be_rendered . resolve ( context ) <EOL> return Template ( actual_item ) . render ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def render_as_template ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) != <NUM_LIT:2> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> return RenderAsTemplateNode ( bits [ <NUM_LIT:1> ] ) <EOL> class RenderTemplateBlockNode ( template . Node ) : <EOL> def __init__ ( self , template_name , block_name ) : <EOL> self . template_name = template_name <EOL> self . block_name = block_name <EOL> def render ( self , context ) : <EOL> return render_block_to_string ( '<STR_LIT>' , self . block_name [ <NUM_LIT:1> : - <NUM_LIT:1> ] , context ) <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def render_template_block_tag ( parser , token ) : <EOL> try : <EOL> tag_name , template_name , block_name = token . split_contents ( ) <EOL> except ValueError : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % tag_name ) <EOL> if not ( block_name [ <NUM_LIT:0> ] == block_name [ - <NUM_LIT:1> ] and block_name [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tag_name ) <EOL> return RenderTemplateBlockNode ( template_name , block_name ) <EOL> @ register . filter_function <EOL> def random_slice ( value , arg = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> arg = int ( arg ) <EOL> except ValueError : <EOL> raise Exception ( '<STR_LIT>' % arg ) <EOL> if type ( value ) == QuerySet : <EOL> pks = list ( value . values_list ( '<STR_LIT>' , flat = True ) ) <EOL> random_pks = random_slice_list ( pks , arg ) <EOL> return value . filter ( pk__in = random_pks ) <EOL> elif type ( value ) == list : <EOL> return random_slice_list ( value , arg ) <EOL> else : <EOL> return value [ : arg ] <EOL> @ register . filter ( name = '<STR_LIT>' ) <EOL> def zip_lists ( a , b ) : <EOL> return zip ( a , b ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def cleartags ( value , tags ) : <EOL> tags = [ re . escape ( tag ) for tag in tags . split ( ) ] <EOL> tags_re = u'<STR_LIT>' % u'<STR_LIT:|>' . join ( tags ) <EOL> clear_re = re . compile ( "<STR_LIT>" % tags_re , re . U ) <EOL> value = clear_re . sub ( '<STR_LIT>' , value ) <EOL> return value <EOL> cleartags . is_safe = True <EOL> @ register . filter <EOL> @ stringfilter <EOL> def split ( str , splitter ) : <EOL> "<STR_LIT>" <EOL> return str . split ( splitter ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def cut ( value , arg ) : <EOL> "<STR_LIT>" <EOL> return value . replace ( arg , '<STR_LIT>' ) <EOL> cut . is_safe = True <EOL> @ register . filter <EOL> @ stringfilter <EOL> def replace ( value , arg ) : <EOL> "<STR_LIT>" <EOL> arg = arg . split ( ) <EOL> return value . replace ( arg [ <NUM_LIT:0> ] , arg [ <NUM_LIT:1> ] ) <EOL> replace . is_safe = True <EOL> @ register . filter <EOL> def nowhitespace ( value ) : <EOL> "<STR_LIT>" <EOL> return u"<STR_LIT>" . join ( value . split ( ) ) <EOL> nowhitespace . is_safe = True <EOL> @ register . filter <EOL> def cleanwhitespace ( value ) : <EOL> "<STR_LIT>" <EOL> return u"<STR_LIT:U+0020>" . join ( value . split ( ) ) <EOL> cleanwhitespace . is_safe = True <EOL> @ register . filter <EOL> @ stringfilter <EOL> def startswith ( value , arg ) : <EOL> "<STR_LIT>" <EOL> return value . startswith ( arg ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def endswith ( value , arg ) : <EOL> "<STR_LIT>" <EOL> return value . endswith ( arg ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def urlunquote ( value ) : <EOL> "<STR_LIT>" <EOL> return urllib . unquote ( value ) </s>
