Ticket #10285: render_comment_list.diff

File render_comment_list.diff, 4.4 KB (added by Kyle Fuller, 16 years ago)

Complete diff of all changes

  • django/contrib/comments/templatetags/comments.py

     
    113113    def get_context_value_from_queryset(self, context, qs):
    114114        return list(qs)
    115115
     116class RenderCommentListNode(CommentListNode):
     117    """ Render the comment list directly """
     118   
     119    #@classmethod
     120    def handle_token(cls, parser, token):
     121        """Class method to parse render_comment_list and return a Node."""
     122        tokens = token.contents.split()
     123        if tokens[1] != 'for':
     124            raise template.TemplateSyntaxError("Second argument in %r tag must be 'for'" % tokens[0])
     125
     126        # {% render_comment_list for obj %}
     127        if len(tokens) == 3:
     128            return cls(object_expr=parser.compile_filter(tokens[2]))
     129
     130        # {% render_comment_list for app.models pk %}
     131        elif len(tokens) == 4:
     132            return cls(
     133                ctype = BaseCommentNode.lookup_content_type(tokens[2], tokens[0]),
     134                object_pk_expr = parser.compile_filter(tokens[3])
     135            )
     136    handle_token = classmethod(handle_token)
     137   
     138    def render(self, context):
     139        ctype, object_pk = self.get_target_ctype_pk(context)
     140        if object_pk:
     141            template_search_list = [
     142                "comments/%s/%s/list.html" % (ctype.app_label, ctype.model),
     143                "comments/%s/list.html" % ctype.app_label,
     144                "comments/list.html"
     145            ]
     146            qs = self.get_query_set(context)
     147            context.push()
     148            liststr = render_to_string(template_search_list, {"comment_list" : self.get_context_value_from_queryset(context, qs)}, context)
     149            context.pop()
     150            return liststr
     151        else:
     152            return ''
     153
    116154class CommentCountNode(BaseCommentNode):
    117155    """Insert a count of comments into the context."""
    118156    def get_context_value_from_queryset(self, context, qs):
     
    217255    return CommentListNode.handle_token(parser, token)
    218256
    219257#@register.tag
     258def render_comment_list(parser, token):
     259    """
     260    Render the comment list (as returned by ``{% render_comment_list %}``) through
     261    the ``comments/list.html`` template
     262       
     263    Syntax::
     264        {% render_comment_list for [object] %}
     265        {% render_comment_list for [app].[model] [object_id] %}
     266    """
     267    return RenderCommentListNode.handle_token(parser, token)
     268
     269#@register.tag
    220270def get_comment_form(parser, token):
    221271    """
    222272    Get a (new) form object to post a new comment.
     
    254304
    255305register.tag(get_comment_count)
    256306register.tag(get_comment_list)
     307register.tag(render_comment_list)
    257308register.tag(get_comment_form)
    258309register.tag(render_comment_form)
    259310register.simple_tag(comment_form_target)
  • django/contrib/comments/templates/comments/list.html

     
     1<dl id="comments">
     2{% for comment in comment_list %}
     3    <dt id="c{{ comment.id }}">
     4        {{ comment.submit_date }} - {{ comment.name }}
     5    </dt>
     6    <dd>
     7        <p>{{ comment.comment }}</p>
     8    </dd>
     9{% endfor %}
     10</dl>
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

     
    6363
    6464    def testGetCommentListFromObject(self):
    6565        self.testGetCommentList("{% get_comment_list for a as cl %}")
     66   
     67    def testRenderCommentList(self, tag=None):
     68        t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
     69        ctx, out = self.render(t, a=Article.objects.get(pk=1))
     70        self.assert_(out.strip().startswith("<dl id=\"comments\">"))
     71        self.assert_(out.strip().endswith("</dl>"))
     72   
     73    def testRenderCommentListFromLiteral(self):
     74        self.testRenderCommentList("{% render_comment_list for comment_tests.article 1 %}")
     75   
     76    def testRenderCommentListFromObject(self):
     77        self.testRenderCommentList("{% render_comment_list for a %}")
Back to Top