Code

Ticket #11100: r10753_get_comment_permalink.diff

File r10753_get_comment_permalink.diff, 4.0 KB (added by idangazit, 5 years ago)
Line 
1diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
2index 9f8180a..4e84ee2 100644
3--- a/django/contrib/comments/templatetags/comments.py
4+++ b/django/contrib/comments/templatetags/comments.py
5@@ -252,8 +252,23 @@ def comment_form_target():
6     """
7     return comments.get_form_target()
8 
9+#@register.simple_tag
10+def get_comment_permalink(comment, anchor_pattern=None):
11+    """
12+    Get the permalink for a comment, optionally specifying the format of the
13+    named anchor to be appended to the end of the URL.
14+   
15+    Example::
16+        {{ get_comment_permalink comment "#c%(id)s-by-%(user_name)s" }}
17+    """
18+   
19+    if anchor_pattern:
20+        return comment.get_absolute_url(anchor_pattern)
21+    return comment.get_absolute_url()
22+
23 register.tag(get_comment_count)
24 register.tag(get_comment_list)
25 register.tag(get_comment_form)
26 register.tag(render_comment_form)
27 register.simple_tag(comment_form_target)
28+register.simple_tag(get_comment_permalink)
29\ No newline at end of file
30diff --git a/docs/ref/contrib/comments/index.txt b/docs/ref/contrib/comments/index.txt
31index f6e1553..8d0e943 100644
32--- a/docs/ref/contrib/comments/index.txt
33+++ b/docs/ref/contrib/comments/index.txt
34@@ -104,6 +104,44 @@ This returns a list of :class:`~django.contrib.comments.models.Comment` objects;
35 see :ref:`the comment model documentation <ref-contrib-comments-models>` for
36 details.
37 
38+.. templatetag:: get_comment_permalink
39+
40+Linking to comments
41+-------------------
42+
43+To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`::
44+   
45+    {% get_comment_permalink comment_obj [format_string] %}
46+
47+By default, the named anchor that will be appended to the URL will be the letter
48+'c' followed by the comment id, for example 'c82'. You may specify a custom
49+format string if you wish to override this behavior::
50+
51+    {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%}
52+
53+The format string is a standard python format string. Valid mapping keys
54+include any attributes of the comment object.
55+
56+Regardless of whether you specify a custom anchor pattern, you must supply a
57+matching named anchor at a suitable place in your template.
58+
59+For example::
60+
61+    {% for comment in comment_list %}
62+        <a name="c{{ comment.id }}"></a>
63+        <a href="{% get_comment_permalink comment %}">
64+            permalink for comment #{{ forloop.counter }}
65+        </a>
66+        ...
67+    {% endfor %}
68+   
69+.. warning::
70+
71+    There's a known bug in Safari / webkit which causes the named anchor to be
72+    forgotten following a redirect. The practical impact for comments is that
73+    the Safari/webkit browsers will arrive at the correct page but will not
74+    scroll to the named anchor.
75+
76 .. templatetag:: get_comment_count
77 
78 Counting comments
79diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
80index a1187ca..ea68e67 100644
81--- a/tests/regressiontests/comment_tests/tests/templatetag_tests.py
82+++ b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
83@@ -63,3 +63,19 @@ class CommentTemplateTagTests(CommentTestCase):
84 
85     def testGetCommentListFromObject(self):
86         self.testGetCommentList("{% get_comment_list for a as cl %}")
87+       
88+    def testGetCommentPermalink(self):
89+        self.createSomeComments()
90+        t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}"
91+        t += "{% get_comment_permalink cl.0 %}"
92+        ctx, out = self.render(t, a=Author.objects.get(pk=1))
93+        self.assertEqual(out, "/cr/13/1/#c2")
94+
95+    def testGetCommentPermalinkFormatted(self):
96+        self.createSomeComments()
97+        t = "{% load comments %}{% get_comment_list for comment_tests.author a.id as cl %}"
98+        t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}"
99+        ctx, out = self.render(t, a=Author.objects.get(pk=1))
100+        self.assertEqual(out, "/cr/13/1/#c2-by-Joe Somebody")
101+
102+