<s> from django import template <EOL> from django . db import models <EOL> from ella . core . models import Related <EOL> register = template . Library ( ) <EOL> class RelatedNode ( template . Node ) : <EOL> def __init__ ( self , obj_var , count , var_name , models , finder ) : <EOL> self . obj_var = obj_var <EOL> self . count = count <EOL> self . var_name = var_name <EOL> self . models = models <EOL> self . finder = finder <EOL> def render ( self , context ) : <EOL> try : <EOL> obj = template . Variable ( self . obj_var ) . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return '<STR_LIT>' <EOL> related = Related . objects . get_related_for_object ( obj , self . count , <EOL> mods = self . models , finder = self . finder ) <EOL> context [ self . var_name ] = related <EOL> return '<STR_LIT>' <EOL> def parse_related_tag ( bits ) : <EOL> if len ( bits ) < <NUM_LIT:6> : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" <EOL> if not bits [ <NUM_LIT:1> ] . isdigit ( ) : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" <EOL> if bits [ - <NUM_LIT:4> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" <EOL> if '<STR_LIT:.>' in bits [ <NUM_LIT:2> ] or bits [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> mod_index = <NUM_LIT:2> <EOL> finder = None <EOL> else : <EOL> mod_index = <NUM_LIT:3> <EOL> finder = bits [ <NUM_LIT:2> ] <EOL> mods = [ ] <EOL> for m in bits [ mod_index : - <NUM_LIT:4> ] : <EOL> if m == '<STR_LIT:U+002C>' : <EOL> continue <EOL> if '<STR_LIT:U+002C>' in m : <EOL> ms = m . split ( '<STR_LIT:U+002C>' ) <EOL> for msm in ms : <EOL> if not msm : <EOL> continue <EOL> try : <EOL> mods . append ( models . get_model ( * msm . split ( '<STR_LIT:.>' ) ) ) <EOL> except : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % msm <EOL> else : <EOL> try : <EOL> mods . append ( models . get_model ( * m . split ( '<STR_LIT:.>' ) ) ) <EOL> except : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % m <EOL> return bits [ - <NUM_LIT:3> ] , int ( bits [ <NUM_LIT:1> ] ) , bits [ - <NUM_LIT:1> ] , mods , finder <EOL> @ register . tag ( '<STR_LIT>' ) <EOL> def do_related ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> obj_var , count , var_name , mods , finder = parse_related_tag ( bits ) <EOL> return RelatedNode ( obj_var , count , var_name , mods , finder ) </s>
