<s> from django import template <EOL> from scaffold import app_settings <EOL> Section = app_settings . get_extending_model ( ) <EOL> register = template . Library ( ) <EOL> class SectionNode ( template . Node ) : <EOL> def __init__ ( self , section = None , as_varname = None ) : <EOL> self . as_varname = as_varname <EOL> self . current_section = section <EOL> def _resolve_section ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if self . current_section : <EOL> current_section = template . Variable ( self . current_section ) <EOL> try : <EOL> current_section = current_section . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> current_section = None <EOL> else : <EOL> current_section = None <EOL> return current_section <EOL> def render ( self , context ) : <EOL> root_sections = Section . get_root_nodes ( ) <EOL> current_section = self . _resolve_section ( context ) <EOL> if current_section : <EOL> for section in root_sections : <EOL> is_active = current_section . is_descendant_of ( section ) or current_section . pk == section . pk <EOL> setattr ( section , '<STR_LIT>' , is_active ) <EOL> context [ self . as_varname ] = root_sections <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_root_sections ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:1> ] not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] <EOL> ) <EOL> if tokens [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> varname = tokens [ <NUM_LIT:2> ] <EOL> section = None <EOL> else : <EOL> if tokens [ <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] <EOL> ) <EOL> section = tokens [ <NUM_LIT:2> ] <EOL> varname = tokens [ <NUM_LIT:4> ] <EOL> return SectionNode ( section = section , as_varname = varname ) <EOL> class SectionDescendantNode ( template . Node ) : <EOL> def __init__ ( self , section_var , ancestor_var , varname ) : <EOL> self . section_var = template . Variable ( section_var ) <EOL> self . ancestor_var = template . Variable ( ancestor_var ) <EOL> self . varname = varname <EOL> def _resolve_vars ( self , context ) : <EOL> try : <EOL> section = self . section_var . resolve ( context ) <EOL> ancestor = self . ancestor_var . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return ( None , None , ) <EOL> return ( section , ancestor , ) <EOL> def render ( self , context ) : <EOL> section , ancestor = self . _resolve_vars ( context ) <EOL> if not section or not ancestor : <EOL> context [ self . varname ] = None <EOL> return '<STR_LIT>' <EOL> result = section . is_descendant_of ( ancestor ) or section . pk == ancestor . pk <EOL> context [ self . varname ] = result <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def section_is_descendant ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:2> ] != '<STR_LIT>' or tokens [ <NUM_LIT:4> ] != '<STR_LIT>' or len ( tokens ) != <NUM_LIT:6> : <EOL> raise template . TemplateSyntaxError ( ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) % tokens [ <NUM_LIT:0> ] ) <EOL> section_var = tokens [ <NUM_LIT:1> ] <EOL> ancestor_var = tokens [ <NUM_LIT:3> ] <EOL> varname = tokens [ <NUM_LIT:5> ] <EOL> return SectionDescendantNode ( section_var , ancestor_var , varname ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' , takes_context = True ) <EOL> def submit_row ( context ) : <EOL> """<STR_LIT>""" <EOL> opts = context [ '<STR_LIT>' ] <EOL> change = context [ '<STR_LIT>' ] <EOL> is_popup = context [ '<STR_LIT>' ] <EOL> save_as = context [ '<STR_LIT>' ] <EOL> allow_associated_ordering = context [ '<STR_LIT>' ] <EOL> model_label = context [ '<STR_LIT>' ] <EOL> return { <EOL> '<STR_LIT>' : model_label , <EOL> '<STR_LIT>' : allow_associated_ordering , <EOL> '<STR_LIT>' : not is_popup and change , <EOL> '<STR_LIT>' : ( opts . get_ordered_objects ( ) and change <EOL> and '<STR_LIT>' or '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( not is_popup and context [ '<STR_LIT>' ] <EOL> and ( change or context [ '<STR_LIT>' ] ) ) , <EOL> '<STR_LIT>' : not is_popup and change and save_as , <EOL> '<STR_LIT>' : context [ '<STR_LIT>' ] and <EOL> not is_popup and ( not save_as or context [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : not is_popup and context [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : is_popup , <EOL> '<STR_LIT>' : True <EOL> } </s>
