From abcf997713fe87a68c8ce7035c9e9dc2266145ea Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Mon, 4 Jan 2010 02:28:09 +0000 Subject: [PATCH] Fixed #11100 - Added get_comment_permalink template tag to comments app to be able to customize the anchor pattern of a comment from the template. Thanks to Idan Gazit for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12080 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../contrib/comments/templatetags/comments.py | 15 ++++++++ docs/ref/contrib/comments/index.txt | 38 +++++++++++++++++++ .../comment_tests/tests/templatetag_tests.py | 21 ++++++++++ 3 files changed, 74 insertions(+) diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py index 9f8180ab08..1ebc9dba34 100644 --- a/django/contrib/comments/templatetags/comments.py +++ b/django/contrib/comments/templatetags/comments.py @@ -252,8 +252,23 @@ def comment_form_target(): """ return comments.get_form_target() +#@register.simple_tag +def get_comment_permalink(comment, anchor_pattern=None): + """ + Get the permalink for a comment, optionally specifying the format of the + named anchor to be appended to the end of the URL. + + Example:: + {{ get_comment_permalink comment "#c%(id)s-by-%(user_name)s" }} + """ + + if anchor_pattern: + return comment.get_absolute_url(anchor_pattern) + return comment.get_absolute_url() + register.tag(get_comment_count) register.tag(get_comment_list) register.tag(get_comment_form) register.tag(render_comment_form) register.simple_tag(comment_form_target) +register.simple_tag(get_comment_permalink) \ No newline at end of file diff --git a/docs/ref/contrib/comments/index.txt b/docs/ref/contrib/comments/index.txt index 54fb7a192d..f66d14d0c4 100644 --- a/docs/ref/contrib/comments/index.txt +++ b/docs/ref/contrib/comments/index.txt @@ -104,6 +104,44 @@ This returns a list of :class:`~django.contrib.comments.models.Comment` objects; see :ref:`the comment model documentation ` for details. +.. templatetag:: get_comment_permalink + +Linking to comments +------------------- + +To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`:: + + {% get_comment_permalink comment_obj [format_string] %} + +By default, the named anchor that will be appended to the URL will be the letter +'c' followed by the comment id, for example 'c82'. You may specify a custom +format string if you wish to override this behavior:: + + {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%} + +The format string is a standard python format string. Valid mapping keys +include any attributes of the comment object. + +Regardless of whether you specify a custom anchor pattern, you must supply a +matching named anchor at a suitable place in your template. + +For example:: + + {% for comment in comment_list %} + + + permalink for comment #{{ forloop.counter }} + + ... + {% endfor %} + +.. warning:: + + There's a known bug in Safari/Webkit which causes the named anchor to be + forgotten following a redirect. The practical impact for comments is that + the Safari/webkit browsers will arrive at the correct page but will not + scroll to the named anchor. + .. templatetag:: get_comment_count Counting comments diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py index a1187ca732..7cb2148b54 100644 --- a/tests/regressiontests/comment_tests/tests/templatetag_tests.py +++ b/tests/regressiontests/comment_tests/tests/templatetag_tests.py @@ -1,5 +1,6 @@ from django.contrib.comments.forms import CommentForm from django.contrib.comments.models import Comment +from django.contrib.contenttypes.models import ContentType from django.template import Template, Context from regressiontests.comment_tests.models import Article, Author from regressiontests.comment_tests.tests import CommentTestCase @@ -63,3 +64,23 @@ class CommentTemplateTagTests(CommentTestCase): def testGetCommentListFromObject(self): self.testGetCommentList("{% get_comment_list for a as cl %}") + + def testGetCommentPermalink(self): + self.createSomeComments() + t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}" + t += "{% get_comment_permalink cl.0 %}" + ct = ContentType.objects.get_for_model(Author) + author = Author.objects.get(pk=1) + ctx, out = self.render(t, author=author) + self.assertEqual(out, "/cr/%s/%s/#c2" % (ct.id, author.id)) + + def testGetCommentPermalinkFormatted(self): + self.createSomeComments() + t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}" + t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}" + ct = ContentType.objects.get_for_model(Author) + author = Author.objects.get(pk=1) + ctx, out = self.render(t, author=author) + self.assertEqual(out, "/cr/%s/%s/#c2-by-Joe Somebody" % (ct.id, author.id)) + +