Ticket #11100: r10753_get_comment_permalink.diff

File r10753_get_comment_permalink.diff, 4.0 KB (added by Idan Gazit, 15 years ago)
  • django/contrib/comments/templatetags/comments.py

    diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
    index 9f8180a..4e84ee2 100644
    a b def comment_form_target():  
    252252    """
    253253    return comments.get_form_target()
    254254
     255#@register.simple_tag
     256def get_comment_permalink(comment, anchor_pattern=None):
     257    """
     258    Get the permalink for a comment, optionally specifying the format of the
     259    named anchor to be appended to the end of the URL.
     260   
     261    Example::
     262        {{ get_comment_permalink comment "#c%(id)s-by-%(user_name)s" }}
     263    """
     264   
     265    if anchor_pattern:
     266        return comment.get_absolute_url(anchor_pattern)
     267    return comment.get_absolute_url()
     268
    255269register.tag(get_comment_count)
    256270register.tag(get_comment_list)
    257271register.tag(get_comment_form)
    258272register.tag(render_comment_form)
    259273register.simple_tag(comment_form_target)
     274register.simple_tag(get_comment_permalink)
     275 No newline at end of file
  • docs/ref/contrib/comments/index.txt

    diff --git a/docs/ref/contrib/comments/index.txt b/docs/ref/contrib/comments/index.txt
    index f6e1553..8d0e943 100644
    a b This returns a list of :class:`~django.contrib.comments.models.Comment` objects;  
    104104see :ref:`the comment model documentation <ref-contrib-comments-models>` for
    105105details.
    106106
     107.. templatetag:: get_comment_permalink
     108
     109Linking to comments
     110-------------------
     111
     112To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`::
     113   
     114    {% get_comment_permalink comment_obj [format_string] %}
     115
     116By default, the named anchor that will be appended to the URL will be the letter
     117'c' followed by the comment id, for example 'c82'. You may specify a custom
     118format string if you wish to override this behavior::
     119
     120    {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%}
     121
     122The format string is a standard python format string. Valid mapping keys
     123include any attributes of the comment object.
     124
     125Regardless of whether you specify a custom anchor pattern, you must supply a
     126matching named anchor at a suitable place in your template.
     127
     128For example::
     129
     130    {% for comment in comment_list %}
     131        <a name="c{{ comment.id }}"></a>
     132        <a href="{% get_comment_permalink comment %}">
     133            permalink for comment #{{ forloop.counter }}
     134        </a>
     135        ...
     136    {% endfor %}
     137   
     138.. warning::
     139
     140    There's a known bug in Safari / webkit which causes the named anchor to be
     141    forgotten following a redirect. The practical impact for comments is that
     142    the Safari/webkit browsers will arrive at the correct page but will not
     143    scroll to the named anchor.
     144
    107145.. templatetag:: get_comment_count
    108146
    109147Counting comments
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
    index a1187ca..ea68e67 100644
    a b class CommentTemplateTagTests(CommentTestCase):  
    6363
    6464    def testGetCommentListFromObject(self):
    6565        self.testGetCommentList("{% get_comment_list for a as cl %}")
     66       
     67    def testGetCommentPermalink(self):
     68        self.createSomeComments()
     69        t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}"
     70        t += "{% get_comment_permalink cl.0 %}"
     71        ctx, out = self.render(t, a=Author.objects.get(pk=1))
     72        self.assertEqual(out, "/cr/13/1/#c2")
     73
     74    def testGetCommentPermalinkFormatted(self):
     75        self.createSomeComments()
     76        t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}"
     77        t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}"
     78        ctx, out = self.render(t, a=Author.objects.get(pk=1))
     79        self.assertEqual(out, "/cr/13/1/#c2-by-Joe Somebody")
     80
     81
Back to Top