<s> """<STR_LIT>""" <EOL> import re <EOL> from django import template <EOL> from django . template import TemplateSyntaxError , Node , loader , Variable <EOL> from django . template . defaultfilters import stringfilter <EOL> from django . utils . html import strip_tags <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . text import smart_split <EOL> from django . utils . translation import ugettext as _ <EOL> from planet . models import Author , Feed , Blog , Post <EOL> from planet . settings import PLANET_CONFIG <EOL> from tagging . models import Tag , TaggedItem <EOL> register = template . Library ( ) <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def authors_about ( tag ) : <EOL> """<STR_LIT>""" <EOL> post_ids = TaggedItem . objects . get_by_model ( <EOL> Post . site_objects , tag ) . values_list ( "<STR_LIT:id>" , flat = True ) <EOL> authors = Author . site_objects . filter ( post__in = post_ids ) . distinct ( ) <EOL> return { "<STR_LIT>" : authors , "<STR_LIT>" : tag } <EOL> @ register . inclusion_tag ( '<STR_LIT>' ) <EOL> def feeds_about ( tag ) : <EOL> """<STR_LIT>""" <EOL> post_ids = TaggedItem . objects . get_by_model ( <EOL> Post . site_objects , tag ) . values_list ( "<STR_LIT:id>" , flat = True ) <EOL> feeds_list = Feed . site_objects . filter ( post__in = post_ids ) . distinct ( ) <EOL> return { "<STR_LIT>" : feeds_list , "<STR_LIT>" : tag } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def related_tags_for ( tag , count = PLANET_CONFIG [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> related_tags = Tag . objects . related_for_model ( [ tag ] , Post , counts = True ) <EOL> return { "<STR_LIT>" : related_tags [ : count ] } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def post_details ( post , template = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> return { "<STR_LIT>" : template , "<STR_LIT>" : post } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def post_full_details ( post ) : <EOL> """<STR_LIT>""" <EOL> return { "<STR_LIT>" : post } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def cloud_for_feed ( feed , min_count = PLANET_CONFIG [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> tags_cloud = Tag . objects . cloud_for_model ( <EOL> Post , filters = { "<STR_LIT>" : feed } , min_count = min_count ) <EOL> return { "<STR_LIT>" : tags_cloud , "<STR_LIT>" : feed } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def cloud_for_author ( author , min_count = PLANET_CONFIG [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> tags_cloud = Tag . objects . cloud_for_model ( <EOL> Post , filters = { "<STR_LIT>" : author } , min_count = min_count ) <EOL> return { "<STR_LIT>" : tags_cloud , "<STR_LIT>" : author } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def cloud_for_blog ( blog , min_count = PLANET_CONFIG [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> tags_cloud = Tag . objects . cloud_for_model ( <EOL> Post , filters = { "<STR_LIT>" : blog } , min_count = min_count ) <EOL> return { "<STR_LIT>" : tags_cloud , "<STR_LIT>" : blog } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def authors_for_feed ( feed ) : <EOL> authors = Author . site_objects . filter ( post__feed = feed ) <EOL> return { "<STR_LIT>" : authors , "<STR_LIT>" : feed } <EOL> @ register . inclusion_tag ( "<STR_LIT>" ) <EOL> def feeds_for_author ( author ) : <EOL> feeds = Feed . site_objects . filter ( <EOL> post__authors = author ) . order_by ( "<STR_LIT:title>" ) . distinct ( ) <EOL> return { "<STR_LIT>" : feeds , "<STR_LIT>" : author } <EOL> class PlanetPostList ( Node ) : <EOL> def __init__ ( self , limit = None , tag = None , category = None , template = None ) : <EOL> self . limit = limit <EOL> self . tag = tag <EOL> self . category = category <EOL> self . template = template <EOL> def resolve ( self , context , vars ) : <EOL> """<STR_LIT>""" <EOL> for var in vars : <EOL> val_var = self . __getattribute__ ( var ) <EOL> if val_var is not None : <EOL> self . __setattr__ ( var , Variable ( val_var ) . resolve ( context ) ) <EOL> def process ( self , context ) : <EOL> self . resolve ( context , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if self . tag is not None : <EOL> posts = TaggedItem . objects . get_by_model ( <EOL> Post . site_objects , self . tag ) <EOL> else : <EOL> posts = Post . site_objects <EOL> posts = posts . select_related ( ) <EOL> if self . category is not None : <EOL> posts = posts . filter ( feed__category__title = self . category ) <EOL> posts = posts . extra ( <EOL> select = { '<STR_LIT:date>' : "<STR_LIT>" } <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> if self . limit is not None : <EOL> posts = posts [ : self . limit ] <EOL> context [ '<STR_LIT>' ] = posts <EOL> if self . template is None : <EOL> self . template = "<STR_LIT>" <EOL> return ( self . template , context ) <EOL> def render ( self , context ) : <EOL> template , context = self . process ( context ) <EOL> return loader . get_template ( template ) . render ( context ) <EOL> @ register . tag ( ) <EOL> def planet_post_list ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = list ( smart_split ( token . contents ) ) <EOL> len_bits = len ( bits ) <EOL> kwargs = { } <EOL> if len_bits > <NUM_LIT:1> : <EOL> if bits [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( _ ( "<STR_LIT>" ) % bits [ <NUM_LIT:0> ] ) <EOL> for i in range ( <NUM_LIT:2> , len_bits ) : <EOL> try : <EOL> name , value = bits [ i ] . split ( '<STR_LIT:=>' ) <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> kwargs [ str ( name ) ] = value <EOL> else : <EOL> raise TemplateSyntaxError ( _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : bits [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : name , <EOL> } ) <EOL> except ValueError : <EOL> raise TemplateSyntaxError ( _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : bits [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : bits [ i ] , <EOL> } ) <EOL> return PlanetPostList ( ** kwargs ) <EOL> @ register . filter <EOL> @ stringfilter <EOL> def clean_html ( html ) : <EOL> pattern_list = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> for pattern in pattern_list : <EOL> html = re . sub ( pattern , '<STR_LIT>' , html ) <EOL> pattern_list = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> for ( pattern , replacement ) in pattern_list : <EOL> html = re . sub ( pattern , replacement , html ) <EOL> return mark_safe ( html ) <EOL> @ register . assignment_tag <EOL> def get_first_paragraph ( body ) : <EOL> if body is None : <EOL> return "<STR_LIT>" <EOL> cleaned_text = strip_tags ( body ) <EOL> cleaned_text = re . sub ( "<STR_LIT>" , "<STR_LIT:U+0020>" , cleaned_text ) <EOL> splitted = [ t for t in cleaned_text . split ( "<STR_LIT:.>" ) if len ( t ) > <NUM_LIT> ] <EOL> return splitted and splitted [ <NUM_LIT:0> ] or cleaned_text [ : <NUM_LIT> ] <EOL> @ register . filter <EOL> def post_count ( obj ) : <EOL> if isinstance ( obj , Author ) : <EOL> return Post . objects . filter ( authors = obj ) . count ( ) <EOL> elif isinstance ( obj , Blog ) : <EOL> return Post . objects . filter ( feed__blog = obj ) . distinct ( ) . count ( ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> @ register . filter <EOL> def get_authors ( blog ) : <EOL> return Author . objects . filter ( post__feed__blog = blog ) . distinct ( ) <EOL> @ register . filter <EOL> def get_blogs ( author ) : <EOL> return Blog . objects . filter ( feed__post__authors = author ) . distinct ( ) <EOL> @ register . assignment_tag <EOL> def latest_posts ( count = <NUM_LIT:10> ) : <EOL> """<STR_LIT>""" <EOL> return Post . objects . all ( ) [ : count ] </s>
