<s> from collections import namedtuple <EOL> from contextlib import contextmanager <EOL> from django import template <EOL> from django . forms . utils import flatatt <EOL> from django . forms . widgets import DateTimeBaseInput <EOL> from django . template . base import token_kwargs <EOL> from django . template . loader import get_template <EOL> from django . template . loader_tags import ( <EOL> BlockNode , ExtendsNode , BlockContext , BLOCK_CONTEXT_KEY , <EOL> ) <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import cached_property <EOL> register = template . Library ( ) <EOL> '''<STR_LIT>''' <EOL> WIDGET_CONTEXT_KEY = '<STR_LIT>' <EOL> def resolve_blocks ( template , context ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> blocks = context . render_context [ BLOCK_CONTEXT_KEY ] <EOL> except KeyError : <EOL> blocks = context . render_context [ BLOCK_CONTEXT_KEY ] = BlockContext ( ) <EOL> if isinstance ( template , six . string_types ) : <EOL> template = get_template ( template ) <EOL> template = getattr ( template , '<STR_LIT>' , template ) <EOL> local_blocks = dict ( <EOL> ( block . name , block ) <EOL> for block in template . nodelist . get_nodes_by_type ( BlockNode ) <EOL> ) <EOL> blocks . add_blocks ( local_blocks ) <EOL> extends = template . nodelist . get_nodes_by_type ( ExtendsNode ) <EOL> if extends : <EOL> extends_node = extends [ <NUM_LIT:0> ] <EOL> parent_template = extends_node . get_parent ( context ) <EOL> resolve_blocks ( parent_template , context ) <EOL> return blocks <EOL> def parse_widget_name ( widget ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> alias , block_name = widget . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % widget <EOL> ) <EOL> return alias , block_name <EOL> @ contextmanager <EOL> def using ( context , alias ) : <EOL> '''<STR_LIT>''' <EOL> if alias == '<STR_LIT>' : <EOL> yield context <EOL> else : <EOL> try : <EOL> widgets = context . render_context [ WIDGET_CONTEXT_KEY ] <EOL> except KeyError : <EOL> raise template . TemplateSyntaxError ( '<STR_LIT>' ) <EOL> try : <EOL> block_set = widgets [ alias ] <EOL> except KeyError : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % alias <EOL> ) <EOL> context . render_context . push ( ) <EOL> context . render_context [ BLOCK_CONTEXT_KEY ] = block_set <EOL> context . render_context [ WIDGET_CONTEXT_KEY ] = widgets <EOL> yield context <EOL> context . render_context . pop ( ) <EOL> def find_block ( context , * names ) : <EOL> '''<STR_LIT>''' <EOL> block_set = context . render_context [ BLOCK_CONTEXT_KEY ] <EOL> for name in names : <EOL> block = block_set . get_block ( name ) <EOL> if block is not None : <EOL> return block <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % ( names , ) <EOL> ) <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def load_widgets ( context , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> _soft = kwargs . pop ( '<STR_LIT>' , False ) <EOL> try : <EOL> widgets = context . render_context [ WIDGET_CONTEXT_KEY ] <EOL> except KeyError : <EOL> widgets = context . render_context [ WIDGET_CONTEXT_KEY ] = { } <EOL> for alias , template_name in kwargs . items ( ) : <EOL> if _soft and alias in widgets : <EOL> continue <EOL> with context . render_context . push ( { BLOCK_CONTEXT_KEY : BlockContext ( ) } ) : <EOL> blocks = resolve_blocks ( template_name , context ) <EOL> widgets [ alias ] = blocks <EOL> return '<STR_LIT>' <EOL> def pop_asvar ( bits ) : <EOL> if len ( bits ) >= <NUM_LIT:2> and bits [ - <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> asvar = bits [ - <NUM_LIT:1> ] <EOL> del bits [ - <NUM_LIT:2> : ] <EOL> return asvar <EOL> class Widget ( template . Node ) : <EOL> def __init__ ( self , widget , kwargs , asvar ) : <EOL> self . widget = widget <EOL> self . kwargs = kwargs <EOL> self . asvar = asvar <EOL> def render ( self , context ) : <EOL> widget = self . widget . resolve ( context ) <EOL> alias , block_name = parse_widget_name ( widget ) <EOL> with using ( context , alias ) : <EOL> block = find_block ( context , block_name ) <EOL> kwargs = { <EOL> key : val . resolve ( context ) <EOL> for key , val in self . kwargs . items ( ) <EOL> } <EOL> with context . push ( kwargs ) : <EOL> result = block . render ( context ) <EOL> if self . asvar : <EOL> context [ self . asvar ] = result <EOL> return '<STR_LIT>' <EOL> return result <EOL> @ register . tag <EOL> def widget ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> tag_name = bits . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> widget = parser . compile_filter ( bits . pop ( <NUM_LIT:0> ) ) <EOL> except IndexError : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % tag_name <EOL> ) <EOL> asvar = pop_asvar ( bits ) <EOL> kwargs = token_kwargs ( bits , parser ) <EOL> if bits : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % tag_name <EOL> ) <EOL> return Widget ( widget , kwargs , asvar ) <EOL> class NestedWidget ( template . Node ) : <EOL> def __init__ ( self , widget , nodelist , kwargs , asvar ) : <EOL> self . widget = widget <EOL> self . nodelist = nodelist <EOL> self . kwargs = kwargs <EOL> self . asvar = asvar <EOL> def render ( self , context ) : <EOL> widget = self . widget . resolve ( context ) <EOL> alias , block_name = parse_widget_name ( widget ) <EOL> with using ( context , alias ) : <EOL> block = find_block ( context , block_name ) <EOL> kwargs = { <EOL> key : val . resolve ( context ) <EOL> for key , val in self . kwargs . items ( ) <EOL> } <EOL> with context . push ( kwargs ) : <EOL> content = self . nodelist . render ( context ) <EOL> with context . push ( { '<STR_LIT:content>' : content } ) : <EOL> result = block . render ( context ) <EOL> if self . asvar : <EOL> context [ self . asvar ] = result <EOL> return '<STR_LIT>' <EOL> return result <EOL> @ register . tag <EOL> def nested_widget ( parser , token ) : <EOL> bits = token . split_contents ( ) <EOL> tag_name = bits . pop ( <NUM_LIT:0> ) <EOL> try : <EOL> widget = parser . compile_filter ( bits . pop ( <NUM_LIT:0> ) ) <EOL> except IndexError : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % tag_name <EOL> ) <EOL> asvar = pop_asvar ( bits ) <EOL> kwargs = token_kwargs ( bits , parser ) <EOL> if bits : <EOL> raise template . TemplateSyntaxError ( <EOL> '<STR_LIT>' % tag_name <EOL> ) <EOL> nodelist = parser . parse ( ( '<STR_LIT>' , ) ) <EOL> parser . delete_first_token ( ) <EOL> return NestedWidget ( widget , nodelist , kwargs , asvar ) <EOL> class ChoiceWrapper ( object ) : <EOL> def __init__ ( self , value , display ) : <EOL> self . value = force_text ( value ) <EOL> self . _display = display <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . value , self . display ) <EOL> def __iter__ ( self ) : <EOL> yield self . value <EOL> yield self . display <EOL> def is_group ( self ) : <EOL> return isinstance ( self . _display , ( list , tuple ) ) <EOL> @ property <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_group ( ) : <EOL> return self . _display <EOL> return ( ( force_text ( k ) , v ) for k , v in self . _display ) <EOL> class FieldExtractor ( dict ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , field ) : <EOL> self . form_field = field <EOL> self . update ( { <EOL> '<STR_LIT:id>' : field . auto_id , <EOL> '<STR_LIT>' : field . field . widget . __class__ . __name__ , <EOL> '<STR_LIT>' : field . field . __class__ . __name__ , <EOL> } ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT:name>' , ) : <EOL> self [ attr ] = getattr ( field , attr ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self [ attr ] = getattr ( field . field , attr , None ) <EOL> def __contains__ ( self , key ) : <EOL> '''<STR_LIT>''' <EOL> return key in self . keys ( ) or hasattr ( self , key ) <EOL> def __missing__ ( self , key ) : <EOL> return getattr ( self , key ) <EOL> @ cached_property <EOL> def raw_value ( self ) : <EOL> return self . form_field . value ( ) <EOL> @ cached_property <EOL> def value ( self ) : <EOL> if isinstance ( self . raw_value , ( tuple , list ) ) : <EOL> return [ force_text ( bit ) for bit in self . raw_value ] <EOL> return force_text ( self . raw_value ) <EOL> @ cached_property <EOL> def display ( self ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( self . choices ) . get ( self . value , '<STR_LIT>' ) <EOL> @ cached_property <EOL> def choices ( self ) : <EOL> c = self . form_field . field . choices <EOL> if not c : <EOL> return c <EOL> return tuple ( <EOL> ChoiceWrapper ( value = k , display = v ) <EOL> for k , v in self . form_field . field . choices <EOL> ) <EOL> class FileFieldExtractor ( FieldExtractor ) : <EOL> @ cached_property <EOL> def file_size ( self ) : <EOL> if self . raw_alue : <EOL> return self . raw_value . size <EOL> @ cached_property <EOL> def url ( self ) : <EOL> if self . raw_value : <EOL> return self . raw_value . url <EOL> class ImageFieldExtractor ( FileFieldExtractor ) : <EOL> @ cached_property <EOL> def width ( self ) : <EOL> if self . value : <EOL> return self . value . width <EOL> @ cached_property <EOL> def height ( self ) : <EOL> if self . value : <EOL> return self . value . height <EOL> class NullBooleanFieldExtractor ( FieldExtractor ) : <EOL> @ cached_property <EOL> def raw_value ( self ) : <EOL> """<STR_LIT>""" <EOL> raw_value = super ( NullBooleanFieldExtractor , self ) . raw_value <EOL> if raw_value is None : <EOL> return '<STR_LIT:1>' <EOL> return raw_value <EOL> @ cached_property <EOL> def value ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return { True : '<STR_LIT:2>' , False : '<STR_LIT:3>' , '<STR_LIT:2>' : '<STR_LIT:2>' , '<STR_LIT:3>' : '<STR_LIT:3>' } [ self . raw_value ] <EOL> except KeyError : <EOL> return '<STR_LIT:1>' <EOL> @ cached_property <EOL> def choices ( self ) : <EOL> c = self [ '<STR_LIT>' ] . choices <EOL> if not c : <EOL> return c <EOL> return tuple ( <EOL> ChoiceWrapper ( value = force_text ( k ) , display = v ) <EOL> for k , v in c <EOL> ) <EOL> class DateTimeBaseExtractor ( FieldExtractor ) : <EOL> """<STR_LIT>""" <EOL> @ cached_property <EOL> def value ( self ) : <EOL> if isinstance ( self [ '<STR_LIT>' ] , DateTimeBaseInput ) : <EOL> return self [ '<STR_LIT>' ] . _format_value ( self . raw_value ) <EOL> return super ( DateTimeBaseExtractor , self ) . value <EOL> EXTRACTOR = { <EOL> '<STR_LIT>' : FileFieldExtractor , <EOL> '<STR_LIT>' : ImageFieldExtractor , <EOL> '<STR_LIT>' : NullBooleanFieldExtractor , <EOL> '<STR_LIT>' : DateTimeBaseExtractor , <EOL> '<STR_LIT>' : DateTimeBaseExtractor , <EOL> '<STR_LIT>' : DateTimeBaseExtractor , <EOL> } <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def form_field ( context , field , widget = None , ** kwargs ) : <EOL> if widget is None : <EOL> alias = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> block_names = auto_widget ( field ) <EOL> else : <EOL> alias , block_name = parse_widget_name ( widget ) <EOL> block_names = [ block_name ] <EOL> field_type = field . field . __class__ . __name__ <EOL> field_data = EXTRACTOR . get ( field_type , FieldExtractor ) ( field ) <EOL> field_data . update ( kwargs ) <EOL> with using ( context , alias ) : <EOL> block = find_block ( context , * block_names ) <EOL> try : <EOL> context . dicts . append ( field_data ) <EOL> return block . render ( context ) <EOL> finally : <EOL> context . dicts . pop ( ) <EOL> def auto_widget ( field ) : <EOL> '''<STR_LIT>''' <EOL> info = { <EOL> '<STR_LIT>' : field . field . widget . __class__ . __name__ , <EOL> '<STR_LIT>' : field . field . __class__ . __name__ , <EOL> '<STR_LIT:name>' : field . name , <EOL> } <EOL> return [ <EOL> fmt . format ( ** info ) <EOL> for fmt in ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> ] <EOL> @ register . filter <EOL> def flatattrs ( attrs ) : <EOL> return flatatt ( attrs ) <EOL> @ register . simple_tag ( takes_context = True ) <EOL> def reuse ( context , block_list , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> block_context = context . render_context [ BLOCK_CONTEXT_KEY ] <EOL> except KeyError : <EOL> block_context = BlockContext ( ) <EOL> if not isinstance ( block_list , ( list , tuple ) ) : <EOL> block_list = [ block_list ] <EOL> for block in block_list : <EOL> block = block_context . get_block ( block ) <EOL> if block : <EOL> break <EOL> else : <EOL> return '<STR_LIT>' <EOL> with context . push ( kwargs ) : <EOL> return block . render ( context ) </s>
