<s> from django import template <EOL> from django . conf import settings <EOL> from django . contrib . flatpages . models import FlatPage <EOL> from django . contrib . sites . models import get_current_site <EOL> register = template . Library ( ) <EOL> class FlatpageNode ( template . Node ) : <EOL> def __init__ ( self , context_name , starts_with = None , user = None ) : <EOL> self . context_name = context_name <EOL> if starts_with : <EOL> self . starts_with = template . Variable ( starts_with ) <EOL> else : <EOL> self . starts_with = None <EOL> if user : <EOL> self . user = template . Variable ( user ) <EOL> else : <EOL> self . user = None <EOL> def render ( self , context ) : <EOL> if '<STR_LIT>' in context : <EOL> site_pk = get_current_site ( context [ '<STR_LIT>' ] ) . pk <EOL> else : <EOL> site_pk = settings . SITE_ID <EOL> flatpages = FlatPage . objects . filter ( sites__id = site_pk ) <EOL> if self . starts_with : <EOL> flatpages = flatpages . filter ( <EOL> url__startswith = self . starts_with . resolve ( context ) ) <EOL> if self . user : <EOL> user = self . user . resolve ( context ) <EOL> if not user . is_authenticated ( ) : <EOL> flatpages = flatpages . filter ( registration_required = False ) <EOL> else : <EOL> flatpages = flatpages . filter ( registration_required = False ) <EOL> context [ self . context_name ] = flatpages <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_flatpages ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> syntax_message = ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> dict ( tag_name = bits [ <NUM_LIT:0> ] ) ) <EOL> if len ( bits ) >= <NUM_LIT:3> and len ( bits ) <= <NUM_LIT:6> : <EOL> if len ( bits ) % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> prefix = bits [ <NUM_LIT:1> ] <EOL> else : <EOL> prefix = None <EOL> if bits [ - <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( syntax_message ) <EOL> context_name = bits [ - <NUM_LIT:1> ] <EOL> if len ( bits ) >= <NUM_LIT:5> : <EOL> if bits [ - <NUM_LIT:4> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( syntax_message ) <EOL> user = bits [ - <NUM_LIT:3> ] <EOL> else : <EOL> user = None <EOL> return FlatpageNode ( context_name , starts_with = prefix , user = user ) <EOL> else : <EOL> raise template . TemplateSyntaxError ( syntax_message ) </s>
