<s> import logging <EOL> from django import template <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from banner_rotator . models import Banner , Place <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> register = template . Library ( ) <EOL> class BannerNode ( template . Node ) : <EOL> def __init__ ( self , place_slug , varname = None ) : <EOL> self . varname , self . place_slug = varname , place_slug <EOL> def render ( self , context ) : <EOL> try : <EOL> self . place = Place . objects . get ( slug = self . place_slug ) <EOL> except Place . DoesNotExist : <EOL> return '<STR_LIT>' <EOL> try : <EOL> banner_obj = Banner . objects . biased_choice ( self . place ) <EOL> banner_obj . view ( ) <EOL> except Banner . DoesNotExist : <EOL> banner_obj = None <EOL> if self . varname : <EOL> context . update ( { <EOL> self . varname : banner_obj , <EOL> '<STR_LIT>' % self . varname : self . place <EOL> } ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> templates = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> return render_to_string ( templates , { <EOL> '<STR_LIT>' : banner_obj , <EOL> '<STR_LIT>' : self . place <EOL> } ) <EOL> @ register . tag <EOL> def banner ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . contents . split ( ) <EOL> if len ( bits ) not in [ <NUM_LIT:2> , <NUM_LIT:4> ] : <EOL> raise template . TemplateSyntaxError ( _ ( "<STR_LIT>" ) ) <EOL> if <NUM_LIT:4> == len ( bits ) and '<STR_LIT>' == bits [ <NUM_LIT:2> ] : <EOL> place_slug = bits [ <NUM_LIT:1> ] <EOL> varname = bits [ <NUM_LIT:3> ] <EOL> else : <EOL> place_slug = bits [ <NUM_LIT:1> ] <EOL> varname = None <EOL> return BannerNode ( place_slug , varname ) </s>
