Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added get_comment_count and get_free_comment_count templatetags. Also…

… updated

the manager to have a all_for_object method which gets all related objects
to the given content object.  Also, updated tests and documentation.

git-svn-id: https://django-threadedcomments.googlecode.com/svn/trunk@61 60388069-7741-0410-adde-d57be00c0a97
  • Loading branch information...
commit 8feb48b853c186a04e5cf6ce21d8281127de0157 1 parent 5b8bf6e
floguy authored
View
8 CHANGELOG.txt
@@ -1,3 +1,11 @@
+Version 0.1.1, 24th January 2008:
+---------------------------------
+
+Added get_comment_count and get_free_comment_count templatetags. Also updated
+the manager to have a all_for_object method which gets all related objects
+to the given content object.
+
+
Version 0.1, 23th January 2008:
-------------------------------
View
12 docs/api.txt
@@ -146,6 +146,10 @@ Common Methods
Wraps the ``get`` method, and automatically fills out ``content_type``
and ``object_id`` fields based on those given by the content object.
+:``all_for_object(content_object, **kwargs)``:
+ Prepopulates a QuerySet with all comments related to the given
+ ``content_object``.
+
``ThreadedCommentManager``
--------------------------
@@ -334,6 +338,14 @@ Tags
Gets the URL to post to for the given object and optional parent free
comment, which will respond with XML (useful for AJAX).
+:``{% get_comment_count for OBJECT as CONTEXT_VAR %}``:
+ Calls ``ThreadedComment.public.all_for_object(OBJECT).count()`` and places
+ the result into the specified context variable.
+
+:``{% get_free_comment_count for OBJECT as CONTEXT_VAR %}``:
+ Calls ``FreeThreadedComment.public.all_for_object(OBJECT).count()`` and
+ places the result into the specified context variable.
+
:``{% auto_transform_markup COMMENT [as CONTEXT_VAR] %}``:
Inspects the given comment and its markup, and either outputs it using the
correct markup processor, or if a context variable is specified, outputs
View
2  setup.py
@@ -2,7 +2,7 @@
kwargs = {
'name' : 'threadedcomments',
- 'version' : '0.1',
+ 'version' : '0.1.1',
'description' : 'A simple yet flexible threaded commenting system.',
'author' : 'Eric Florenzano',
'author_email' : 'floguy@gmail.com',
View
6 threadedcomments/models.py
@@ -89,6 +89,12 @@ def get_for_object(self, content_object, **kwargs):
"""
return self.get(**self._generate_object_kwarg_dict(content_object, **kwargs))
+ def all_for_object(self, content_object, **kwargs):
+ """
+ Prepopulates a QuerySet with all comments related to the given ``content_object``.
+ """
+ return self.filter(**self._generate_object_kwarg_dict(content_object, **kwargs))
+
class PublicThreadedCommentManager(ThreadedCommentManager):
"""
A ``Manager`` which borrows all of the same methods from ``ThreadedCommentManager``,
View
51 threadedcomments/templatetags/threadedcommentstags.py
@@ -211,6 +211,52 @@ def render(self, context):
context[self.context_name] = FreeThreadedComment.public.get_tree(content_object)
return ''
+def do_get_comment_count(parser, token):
+ """
+ Gets a count of how many ThreadedComment objects are attached to the given
+ object.
+ """
+ error_message = "%r tag must be of format {%% %r for OBJECT as CONTEXT_VARIABLE %%}" % (token.contents.split()[0], token.contents.split()[0])
+ try:
+ split = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError, error_message
+ if split[1] != 'for' or split[3] != 'as':
+ raise template.TemplateSyntaxError, error_message
+ return ThreadedCommentCountNode(split[2], split[4])
+
+class ThreadedCommentCountNode(template.Node):
+ def __init__(self, content_object, context_name):
+ self.content_object = template.Variable(content_object)
+ self.context_name = context_name
+ def render(self, context):
+ content_object = self.content_object.resolve(context)
+ context[self.context_name] = ThreadedComment.public.all_for_object(content_object).count()
+ return ''
+
+def do_get_free_comment_count(parser, token):
+ """
+ Gets a count of how many FreeThreadedComment objects are attached to the
+ given object.
+ """
+ error_message = "%r tag must be of format {%% %r for OBJECT as CONTEXT_VARIABLE %%}" % (token.contents.split()[0], token.contents.split()[0])
+ try:
+ split = token.split_contents()
+ except ValueError:
+ raise template.TemplateSyntaxError, error_message
+ if split[1] != 'for' or split[3] != 'as':
+ raise template.TemplateSyntaxError, error_message
+ return FreeThreadedCommentCountNode(split[2], split[4])
+
+class FreeThreadedCommentCountNode(template.Node):
+ def __init__(self, content_object, context_name):
+ self.content_object = template.Variable(content_object)
+ self.context_name = context_name
+ def render(self, context):
+ content_object = self.content_object.resolve(context)
+ context[self.context_name] = FreeThreadedComment.public.all_for_object(content_object).count()
+ return ''
+
def oneline(value):
"""
Takes some HTML and gets rid of newlines and spaces between tags, rendering
@@ -233,4 +279,7 @@ def oneline(value):
register.tag('auto_transform_markup', do_auto_transform_markup)
register.tag('get_threaded_comment_tree', do_get_threaded_comment_tree)
-register.tag('get_free_threaded_comment_tree', do_get_free_threaded_comment_tree)
+register.tag('get_free_threaded_comment_tree', do_get_free_threaded_comment_tree)
+register.tag('get_free_threaded_comment_tree', do_get_free_threaded_comment_tree)
+register.tag('get_comment_count', do_get_comment_count)
+register.tag('get_free_comment_count', do_get_free_comment_count)
View
10 threadedcomments/tests.py
@@ -454,7 +454,7 @@
>>> from threadedcomments.templatetags import threadedcommentstags as tags
>>> topic = TestModel.objects.create(name = "Test3")
->>> c = Context({'topic' : topic, 'parent' : comment9})
+>>> c = Context({'topic' : topic, 'old_topic' : old_topic, 'parent' : comment9})
>>> Template('{% load threadedcommentstags %}{% get_comment_url topic %}').render(c)
u'/comment/9/3/'
@@ -469,7 +469,10 @@
>>> Template('{% load threadedcommentstags %}{% get_comment_url_xml topic parent %}').render(c)
u'/comment/9/3/8/xml/'
->>> c = Context({'topic' : topic, 'parent' : FreeThreadedComment.objects.latest()})
+>>> Template('{% load threadedcommentstags %}{% get_comment_count for old_topic as count %}{{ count }}').render(c)
+u'6'
+
+>>> c = Context({'topic' : topic, 'old_topic' : old_topic, 'parent' : FreeThreadedComment.objects.latest()})
>>> Template('{% load threadedcommentstags %}{% get_free_comment_url topic %}').render(c)
u'/freecomment/9/3/'
>>> Template('{% load threadedcommentstags %}{% get_free_comment_url topic parent %}').render(c)
@@ -483,6 +486,9 @@
>>> Template('{% load threadedcommentstags %}{% get_free_comment_url_xml topic parent %}').render(c)
u'/freecomment/9/3/18/xml/'
+>>> Template('{% load threadedcommentstags %}{% get_free_comment_count for old_topic as count %}{{ count }}').render(c)
+u'6'
+
>>> c = Context({'topic' : old_topic, 'parent' : FreeThreadedComment.objects.latest()})
>>> Template('{% load threadedcommentstags %}{% get_free_threaded_comment_tree for topic as tree %}[{% for item in tree %}({{ item.depth }}){{ item.comment }},{% endfor %}]').render(c)
u'[(0)test1,(0)test2,(0)test3,(1)test4,(1)test5,(1)test6,]'

0 comments on commit 8feb48b

Please sign in to comment.
Something went wrong with that request. Please try again.