<s> from django import template <EOL> from django . template . loader import render_to_string <EOL> from django . conf import settings <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib import comments <EOL> from django . utils import six <EOL> from django . utils . deprecation import RenameMethodsBase <EOL> from django . utils . encoding import smart_text <EOL> register = template . Library ( ) <EOL> class RenameBaseCommentNodeMethods ( RenameMethodsBase ) : <EOL> renamed_methods = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , PendingDeprecationWarning ) , <EOL> ) <EOL> class BaseCommentNode ( six . with_metaclass ( RenameBaseCommentNodeMethods , template . Node ) ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if len ( tokens ) == <NUM_LIT:5> : <EOL> if tokens [ <NUM_LIT:3> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> return cls ( <EOL> object_expr = parser . compile_filter ( tokens [ <NUM_LIT:2> ] ) , <EOL> as_varname = tokens [ <NUM_LIT:4> ] , <EOL> ) <EOL> elif len ( tokens ) == <NUM_LIT:6> : <EOL> if tokens [ <NUM_LIT:4> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> return cls ( <EOL> ctype = BaseCommentNode . lookup_content_type ( tokens [ <NUM_LIT:2> ] , tokens [ <NUM_LIT:0> ] ) , <EOL> object_pk_expr = parser . compile_filter ( tokens [ <NUM_LIT:3> ] ) , <EOL> as_varname = tokens [ <NUM_LIT:5> ] <EOL> ) <EOL> else : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> @ staticmethod <EOL> def lookup_content_type ( token , tagname ) : <EOL> try : <EOL> app , model = token . split ( '<STR_LIT:.>' ) <EOL> return ContentType . objects . get_by_natural_key ( app , model ) <EOL> except ValueError : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tagname ) <EOL> except ContentType . DoesNotExist : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % ( tagname , app , model ) ) <EOL> def __init__ ( self , ctype = None , object_pk_expr = None , object_expr = None , as_varname = None , comment = None ) : <EOL> if ctype is None and object_expr is None : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" ) <EOL> self . comment_model = comments . get_model ( ) <EOL> self . as_varname = as_varname <EOL> self . ctype = ctype <EOL> self . object_pk_expr = object_pk_expr <EOL> self . object_expr = object_expr <EOL> self . comment = comment <EOL> def render ( self , context ) : <EOL> qs = self . get_queryset ( context ) <EOL> context [ self . as_varname ] = self . get_context_value_from_queryset ( context , qs ) <EOL> return '<STR_LIT>' <EOL> def get_queryset ( self , context ) : <EOL> ctype , object_pk = self . get_target_ctype_pk ( context ) <EOL> if not object_pk : <EOL> return self . comment_model . objects . none ( ) <EOL> qs = self . comment_model . objects . filter ( <EOL> content_type = ctype , <EOL> object_pk = smart_text ( object_pk ) , <EOL> site__pk = settings . SITE_ID , <EOL> ) <EOL> field_names = [ f . name for f in self . comment_model . _meta . fields ] <EOL> if '<STR_LIT>' in field_names : <EOL> qs = qs . filter ( is_public = True ) <EOL> if getattr ( settings , '<STR_LIT>' , True ) and '<STR_LIT>' in field_names : <EOL> qs = qs . filter ( is_removed = False ) <EOL> return qs <EOL> def get_target_ctype_pk ( self , context ) : <EOL> if self . object_expr : <EOL> try : <EOL> obj = self . object_expr . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return None , None <EOL> return ContentType . objects . get_for_model ( obj ) , obj . pk <EOL> else : <EOL> return self . ctype , self . object_pk_expr . resolve ( context , ignore_failures = True ) <EOL> def get_context_value_from_queryset ( self , context , qs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class CommentListNode ( BaseCommentNode ) : <EOL> """<STR_LIT>""" <EOL> def get_context_value_from_queryset ( self , context , qs ) : <EOL> return list ( qs ) <EOL> class CommentCountNode ( BaseCommentNode ) : <EOL> """<STR_LIT>""" <EOL> def get_context_value_from_queryset ( self , context , qs ) : <EOL> return qs . count ( ) <EOL> class CommentFormNode ( BaseCommentNode ) : <EOL> """<STR_LIT>""" <EOL> def get_form ( self , context ) : <EOL> obj = self . get_object ( context ) <EOL> if obj : <EOL> return comments . get_form ( ) ( obj ) <EOL> else : <EOL> return None <EOL> def get_object ( self , context ) : <EOL> if self . object_expr : <EOL> try : <EOL> return self . object_expr . resolve ( context ) <EOL> except template . VariableDoesNotExist : <EOL> return None <EOL> else : <EOL> object_pk = self . object_pk_expr . resolve ( context , <EOL> ignore_failures = True ) <EOL> return self . ctype . get_object_for_this_type ( pk = object_pk ) <EOL> def render ( self , context ) : <EOL> context [ self . as_varname ] = self . get_form ( context ) <EOL> return '<STR_LIT>' <EOL> class RenderCommentFormNode ( CommentFormNode ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if len ( tokens ) == <NUM_LIT:3> : <EOL> return cls ( object_expr = parser . compile_filter ( tokens [ <NUM_LIT:2> ] ) ) <EOL> elif len ( tokens ) == <NUM_LIT:4> : <EOL> return cls ( <EOL> ctype = BaseCommentNode . lookup_content_type ( tokens [ <NUM_LIT:2> ] , tokens [ <NUM_LIT:0> ] ) , <EOL> object_pk_expr = parser . compile_filter ( tokens [ <NUM_LIT:3> ] ) <EOL> ) <EOL> def render ( self , context ) : <EOL> ctype , object_pk = self . get_target_ctype_pk ( context ) <EOL> if object_pk : <EOL> template_search_list = [ <EOL> "<STR_LIT>" % ( ctype . app_label , ctype . model ) , <EOL> "<STR_LIT>" % ctype . app_label , <EOL> "<STR_LIT>" <EOL> ] <EOL> context . push ( ) <EOL> formstr = render_to_string ( template_search_list , { "<STR_LIT>" : self . get_form ( context ) } , context ) <EOL> context . pop ( ) <EOL> return formstr <EOL> else : <EOL> return '<STR_LIT>' <EOL> class RenderCommentListNode ( CommentListNode ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def handle_token ( cls , parser , token ) : <EOL> """<STR_LIT>""" <EOL> tokens = token . split_contents ( ) <EOL> if tokens [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise template . TemplateSyntaxError ( "<STR_LIT>" % tokens [ <NUM_LIT:0> ] ) <EOL> if len ( tokens ) == <NUM_LIT:3> : <EOL> return cls ( object_expr = parser . compile_filter ( tokens [ <NUM_LIT:2> ] ) ) <EOL> elif len ( tokens ) == <NUM_LIT:4> : <EOL> return cls ( <EOL> ctype = BaseCommentNode . lookup_content_type ( tokens [ <NUM_LIT:2> ] , tokens [ <NUM_LIT:0> ] ) , <EOL> object_pk_expr = parser . compile_filter ( tokens [ <NUM_LIT:3> ] ) <EOL> ) <EOL> def render ( self , context ) : <EOL> ctype , object_pk = self . get_target_ctype_pk ( context ) <EOL> if object_pk : <EOL> template_search_list = [ <EOL> "<STR_LIT>" % ( ctype . app_label , ctype . model ) , <EOL> "<STR_LIT>" % ctype . app_label , <EOL> "<STR_LIT>" <EOL> ] <EOL> qs = self . get_queryset ( context ) <EOL> context . push ( ) <EOL> liststr = render_to_string ( template_search_list , { <EOL> "<STR_LIT>" : self . get_context_value_from_queryset ( context , qs ) <EOL> } , context ) <EOL> context . pop ( ) <EOL> return liststr <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ register . tag <EOL> def get_comment_count ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return CommentCountNode . handle_token ( parser , token ) <EOL> @ register . tag <EOL> def get_comment_list ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return CommentListNode . handle_token ( parser , token ) <EOL> @ register . tag <EOL> def render_comment_list ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return RenderCommentListNode . handle_token ( parser , token ) <EOL> @ register . tag <EOL> def get_comment_form ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return CommentFormNode . handle_token ( parser , token ) <EOL> @ register . tag <EOL> def render_comment_form ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> return RenderCommentFormNode . handle_token ( parser , token ) <EOL> @ register . simple_tag <EOL> def comment_form_target ( ) : <EOL> """<STR_LIT>""" <EOL> return comments . get_form_target ( ) <EOL> @ register . simple_tag <EOL> def get_comment_permalink ( comment , anchor_pattern = None ) : <EOL> """<STR_LIT>""" <EOL> if anchor_pattern : <EOL> return comment . get_absolute_url ( anchor_pattern ) <EOL> return comment . get_absolute_url ( ) </s>
