<s> import re <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . template import ( Library , Node , TemplateSyntaxError , <EOL> Variable , loader , RequestContext ) <EOL> from django . utils . safestring import mark_safe <EOL> from django_comments_xtd import get_model as get_comment_model <EOL> from django_comments_xtd . conf import settings <EOL> from . . utils import import_formatter <EOL> XtdComment = get_comment_model ( ) <EOL> formatter = import_formatter ( ) <EOL> register = Library ( ) <EOL> class XtdCommentCountNode ( Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , as_varname , content_types ) : <EOL> """<STR_LIT>""" <EOL> self . as_varname = as_varname <EOL> self . qs = XtdComment . objects . for_content_types ( content_types ) <EOL> def render ( self , context ) : <EOL> context [ self . as_varname ] = self . qs . count ( ) <EOL> return '<STR_LIT>' <EOL> def get_xtdcomment_count ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . contents . split ( ) <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> as_varname = tokens [ <NUM_LIT:2> ] <EOL> if tokens [ <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> content_types = _get_content_types ( tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:4> : ] ) <EOL> return XtdCommentCountNode ( as_varname , content_types ) <EOL> class BaseLastXtdCommentsNode ( Node ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , count , content_types , template_path = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . count = int ( count ) <EOL> except : <EOL> self . count = Variable ( count ) <EOL> self . content_types = content_types <EOL> self . template_path = template_path <EOL> class RenderLastXtdCommentsNode ( BaseLastXtdCommentsNode ) : <EOL> def render ( self , context ) : <EOL> if not isinstance ( self . count , int ) : <EOL> self . count = int ( self . count . resolve ( context ) ) <EOL> self . qs = XtdComment . objects . for_content_types ( self . content_types ) [ : self . count ] <EOL> strlist = [ ] <EOL> for xtd_comment in self . qs : <EOL> if self . template_path : <EOL> template_arg = self . template_path <EOL> else : <EOL> template_arg = [ <EOL> "<STR_LIT>" % ( <EOL> xtd_comment . content_type . app_label , <EOL> xtd_comment . content_type . model ) , <EOL> "<STR_LIT>" % ( <EOL> xtd_comment . content_type . app_label , ) , <EOL> "<STR_LIT>" <EOL> ] <EOL> strlist . append ( <EOL> loader . render_to_string ( <EOL> template_arg , { "<STR_LIT>" : xtd_comment } , context ) ) <EOL> return '<STR_LIT>' . join ( strlist ) <EOL> class GetLastXtdCommentsNode ( BaseLastXtdCommentsNode ) : <EOL> def __init__ ( self , count , as_varname , content_types ) : <EOL> super ( GetLastXtdCommentsNode , self ) . __init__ ( count , content_types ) <EOL> self . as_varname = as_varname <EOL> def render ( self , context ) : <EOL> if not isinstance ( self . count , int ) : <EOL> self . count = int ( self . count . resolve ( context ) ) <EOL> self . qs = XtdComment . objects . for_content_types ( self . content_types ) [ : self . count ] <EOL> context [ self . as_varname ] = self . qs <EOL> return '<STR_LIT>' <EOL> def _get_content_types ( tagname , tokens ) : <EOL> content_types = [ ] <EOL> for token in tokens : <EOL> try : <EOL> app , model = token . split ( '<STR_LIT:.>' ) <EOL> content_types . append ( <EOL> ContentType . objects . get ( app_label = app , model = model ) ) <EOL> except ValueError : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % ( <EOL> token , tagname ) ) <EOL> except ContentType . DoesNotExist : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % ( <EOL> tagname , app , model ) ) <EOL> return content_types <EOL> def render_last_xtdcomments ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . contents . split ( ) <EOL> try : <EOL> count = tokens [ <NUM_LIT:1> ] <EOL> except ValueError : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if tokens [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> try : <EOL> token_using = tokens . index ( "<STR_LIT>" ) <EOL> content_types = _get_content_types ( tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:3> : token_using ] ) <EOL> try : <EOL> template = tokens [ token_using + <NUM_LIT:1> ] . strip ( '<STR_LIT>' ) <EOL> except IndexError : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> content_types = _get_content_types ( tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:3> : ] ) <EOL> template = None <EOL> return RenderLastXtdCommentsNode ( count , content_types , template ) <EOL> def get_last_xtdcomments ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . contents . split ( ) <EOL> try : <EOL> count = int ( tokens [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if tokens [ <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> as_varname = tokens [ <NUM_LIT:3> ] <EOL> if tokens [ <NUM_LIT:4> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> content_types = _get_content_types ( tokens [ <NUM_LIT:0> ] , tokens [ <NUM_LIT:5> : ] ) <EOL> return GetLastXtdCommentsNode ( count , as_varname , content_types ) <EOL> def render_with_filter ( markup_filter , lines ) : <EOL> try : <EOL> if formatter : <EOL> return mark_safe ( formatter ( "<STR_LIT:\n>" . join ( lines ) , filter_name = markup_filter ) ) <EOL> else : <EOL> raise TemplateSyntaxError ( <EOL> "<STR_LIT>" ) <EOL> except ValueError as exc : <EOL> output = "<STR_LIT>" % exc <EOL> return output + value <EOL> def render_markup_comment ( value ) : <EOL> """<STR_LIT>""" <EOL> lines = value . splitlines ( ) <EOL> rawstr = r"""<STR_LIT>""" <EOL> match_obj = re . search ( rawstr , lines [ <NUM_LIT:0> ] ) <EOL> if match_obj : <EOL> markup_filter = match_obj . group ( '<STR_LIT>' ) <EOL> return render_with_filter ( markup_filter , lines [ <NUM_LIT:1> : ] ) <EOL> elif settings . COMMENTS_XTD_MARKUP_FALLBACK_FILTER : <EOL> markup_filter = settings . COMMENTS_XTD_MARKUP_FALLBACK_FILTER <EOL> return render_with_filter ( markup_filter , lines ) <EOL> else : <EOL> return value <EOL> register . tag ( get_xtdcomment_count ) <EOL> register . tag ( render_last_xtdcomments ) <EOL> register . tag ( get_last_xtdcomments ) <EOL> register . filter ( render_markup_comment ) </s>
