<s> """<STR_LIT>""" <EOL> import re <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . template . loader import select_template <EOL> from tumblelog . models import Post <EOL> register = template . Library ( ) <EOL> def render_item ( item , template_name = None ) : <EOL> model_name = item . content_object . _meta . verbose_name_raw <EOL> template_list = [ template_name , ] if template_name else [ ] <EOL> template_list . extend ( [ <EOL> '<STR_LIT>' % { '<STR_LIT>' : model_name } , <EOL> '<STR_LIT>' <EOL> ] ) <EOL> t = select_template ( template_list ) <EOL> return t . render ( template . Context ( { <EOL> '<STR_LIT:object>' : item . content_object , <EOL> '<STR_LIT>' : item . id , <EOL> '<STR_LIT>' : item . content_type , <EOL> '<STR_LIT>' : item , <EOL> '<STR_LIT>' : settings . MEDIA_URL <EOL> } ) ) <EOL> register . filter ( '<STR_LIT>' , render_item ) <EOL> class LatestPosts ( template . Node ) : <EOL> def __init__ ( self , limit , var_name ) : <EOL> self . limit = limit <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> posts = Post . objects . published ( ) [ : int ( self . limit ) ] <EOL> if posts and ( int ( self . limit ) == <NUM_LIT:1> ) : <EOL> context [ self . var_name ] = posts [ <NUM_LIT:0> ] <EOL> else : <EOL> context [ self . var_name ] = posts <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_latest_posts ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tag_name , arg = token . contents . split ( None , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> m = re . search ( r'<STR_LIT>' , arg ) <EOL> if not m : <EOL> raise template . TemplateSyntaxError , "<STR_LIT>" % tag_name <EOL> format_string , var_name = m . groups ( ) <EOL> return LatestPosts ( format_string , var_name ) </s>
