<s> import re <EOL> from django import template <EOL> from django . conf import settings <EOL> from django . db . models import Q <EOL> from cms . models import Page <EOL> from tagging . models import TaggedItem <EOL> from tagging . utils import parse_tag_input <EOL> from pagetags . models import PageTagging <EOL> register = template . Library ( ) <EOL> class StaticSlug ( unicode ) : <EOL> def bind ( self , context ) : <EOL> pass <EOL> class DynamicSlug ( ) : <EOL> def __init__ ( self , varname ) : <EOL> self . varname = varname <EOL> self . _value = u'<STR_LIT>' <EOL> def bind ( self , context ) : <EOL> self . _value = context [ self . varname ] <EOL> def __unicode__ ( self ) : <EOL> return self . _value <EOL> class StaticTags ( list ) : <EOL> def __init__ ( self , tags ) : <EOL> super ( StaticTags , self ) . __init__ ( parse_tag_input ( tags ) ) <EOL> def bind ( self , context ) : <EOL> pass <EOL> class DynamicTags ( list ) : <EOL> def __init__ ( self , varname ) : <EOL> self . varname = varname <EOL> super ( DynamicTags , self ) . __init__ ( ) <EOL> def bind ( self , context ) : <EOL> self [ : ] = parse_tag_input ( context . get ( self . varname , '<STR_LIT>' ) ) <EOL> class TaggedPagesNode ( template . Node ) : <EOL> def __init__ ( self , parsed_tags , ordering , limit , var_name ) : <EOL> self . parsed_tags = parsed_tags <EOL> self . ordering = ordering or '<STR_LIT>' <EOL> self . limit = limit <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> self . parsed_tags . bind ( context ) <EOL> pagetaggings_from_site = PageTagging . objects . filter ( <EOL> page__site = settings . SITE_ID <EOL> ) . select_related ( ) <EOL> if self . ordering == '<STR_LIT>' : <EOL> pagetaggings_from_site = pagetaggings_from_site . order_by ( <EOL> '<STR_LIT>' <EOL> ) <EOL> if self . ordering == '<STR_LIT>' : <EOL> pagetaggings_from_site = pagetaggings_from_site . order_by ( <EOL> '<STR_LIT>' <EOL> ) <EOL> page_taggings = TaggedItem . objects . get_by_model ( <EOL> pagetaggings_from_site , self . parsed_tags <EOL> ) [ : self . limit ] <EOL> context [ self . var_name ] = [ <EOL> page_tagging . page for page_tagging in page_taggings <EOL> ] <EOL> return '<STR_LIT>' <EOL> class SimilarPagesNode ( template . Node ) : <EOL> def __init__ ( self , parsed_slug , limit , var_name ) : <EOL> self . parsed_slug = parsed_slug <EOL> self . limit = limit <EOL> self . var_name = var_name <EOL> def render ( self , context ) : <EOL> self . parsed_slug . bind ( context ) <EOL> try : <EOL> page = Page . objects . get ( <EOL> Q ( site = settings . SITE_ID ) , <EOL> Q ( title_set__slug = self . parsed_slug ) <EOL> ) <EOL> except Page . DoesNotExist : <EOL> return '<STR_LIT>' <EOL> page_taggings = TaggedItem . objects . get_related ( <EOL> page . pagetagging , <EOL> PageTagging . objects . select_related ( ) . filter ( <EOL> page__site = settings . SITE_ID <EOL> ) <EOL> ) [ : self . limit ] <EOL> context [ self . var_name ] = [ <EOL> page_tagging . page for page_tagging in page_taggings <EOL> ] <EOL> return '<STR_LIT>' <EOL> def _extract_tag_content ( token ) : <EOL> try : <EOL> tag_name , arg = token . contents . split ( None , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % token . contents . split ( ) [ <NUM_LIT:0> ] <EOL> ) <EOL> return tag_name , arg <EOL> def parsed_tags ( quoted_or_var ) : <EOL> if ( quoted_or_var [ <NUM_LIT:0> ] == quoted_or_var [ - <NUM_LIT:1> ] <EOL> and quoted_or_var [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) : <EOL> return StaticTags ( tags = quoted_or_var [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> return DynamicTags ( varname = quoted_or_var ) <EOL> def parse_slug ( quoted_or_var ) : <EOL> if ( quoted_or_var [ <NUM_LIT:0> ] == quoted_or_var [ - <NUM_LIT:1> ] <EOL> and quoted_or_var [ <NUM_LIT:0> ] in ( '<STR_LIT:">' , "<STR_LIT:'>" ) ) : <EOL> return StaticSlug ( quoted_or_var [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> return DynamicSlug ( varname = quoted_or_var ) <EOL> @ register . tag <EOL> def pages_with_tags ( parser , token ) : <EOL> parsed_tags , ordering , limit , var_name = _parse_pages_with_tags ( token ) <EOL> return TaggedPagesNode ( parsed_tags , ordering , limit , var_name ) <EOL> def _parse_pages_with_tags ( token ) : <EOL> tag_name , arg = _extract_tag_content ( token ) <EOL> m = re . search ( r"""<STR_LIT>""" , arg , flags = re . VERBOSE ) <EOL> if not m : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tag_name <EOL> ) <EOL> tags , _ , ordering , _ , limit , var_name = m . groups ( ) <EOL> return parsed_tags ( tags ) , ordering , limit and int ( limit ) , var_name <EOL> @ register . tag <EOL> def pages_similar_with ( parser , token ) : <EOL> parsed_slug , limit , var_name = _parse_pages_similar_with ( token ) <EOL> return SimilarPagesNode ( parsed_slug , limit , var_name ) <EOL> def _parse_pages_similar_with ( token ) : <EOL> tag_name , arg = _extract_tag_content ( token ) <EOL> m = re . search ( r"""<STR_LIT>""" , arg , flags = re . VERBOSE ) <EOL> if not m : <EOL> raise template . TemplateSyntaxError ( <EOL> "<STR_LIT>" % tag_name <EOL> ) <EOL> page_slug , _ , limit , var_name = m . groups ( ) <EOL> return parse_slug ( page_slug ) , limit and int ( limit ) , var_name </s>
