fivethreeo / django-mptt-comments
Django Mptt Comments is a simple way to display threaded comments instead of the django contrib comments.
Clone this repository (size: 23.0 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/fivethreeo/django-mptt-comments/
| commit 4: | 2c216600fcfd |
| parent 3: | e1acdd8fa8fd |
| branch: | default |
| tags: | tip |
fixed issue with type casts
4 months ago
Changed (Δ37 bytes):
raw changeset »
mptt_comments/templatetags/mptt_comments_tags.py (23 lines added, 20 lines removed)
| … | … | @@ -3,43 +3,46 @@ from django import template |
3 |
3 |
from django.conf import settings |
4 |
4 |
from django.template.loader import render_to_string |
5 |
5 |
from django.utils.safestring import mark_safe |
6 |
from django.utils.encoding import smart_unicode |
|
6 |
7 |
import mptt_comments |
7 |
8 |
|
8 |
9 |
register = template.Library() |
9 |
10 |
|
10 |
11 |
class BaseMpttCommentNode(BaseCommentNode): |
11 |
||
12 |
||
12 |
13 |
root_node = None |
13 |
||
14 |
||
14 |
15 |
def __init__(self, ctype=None, object_pk_expr=None, object_expr=None, as_varname=None, comment=None): |
15 |
16 |
super(BaseMpttCommentNode, self). __init__(ctype=ctype, object_pk_expr=object_pk_expr, object_expr=object_expr, as_varname=as_varname, comment=comment) |
16 |
17 |
self.comment_model = mptt_comments.get_model() |
17 |
||
18 |
||
18 |
19 |
def get_root_node(self, context): |
19 |
20 |
if not self.root_node: |
20 |
21 |
ctype, object_pk = self.get_target_ctype_pk(context) |
22 |
object_pk = smart_unicode(object_pk) |
|
21 |
23 |
self.root_node = self.comment_model.objects.get_root_comment(ctype, object_pk) |
22 |
24 |
return self.root_node |
23 |
25 |
objects = {} |
24 |
26 |
|
25 |
27 |
class MpttCommentFormNode(BaseMpttCommentNode): |
26 |
||
28 |
||
27 |
29 |
global objects |
28 |
||
30 |
||
29 |
31 |
"""Insert a form for the comment model into the context.""" |
30 |
||
32 |
||
31 |
33 |
def get_form(self, context): |
32 |
34 |
ctype, object_pk = self.get_target_ctype_pk(context) |
33 |
||
35 |
||
34 |
36 |
key = str(ctype)+'_'+str(object_pk) |
35 |
||
37 |
||
36 |
38 |
if objects.has_key(key): |
37 |
39 |
return mptt_comments.get_form()(objects[key], parent_comment=self.get_root_node(context)) |
38 |
40 |
elif object_pk: |
41 |
object_pk = smart_unicode(object_pk) |
|
39 |
42 |
objects[key] = ctype.get_object_for_this_type(pk=object_pk) |
40 |
43 |
return mptt_comments.get_form()(objects[key], parent_comment=self.get_root_node(context)) |
41 |
44 |
else: |
42 |
return None |
|
45 |
return None |
|
43 |
46 |
|
44 |
47 |
def render(self, context): |
45 |
48 |
context[self.as_varname] = self.get_form(context) |
| … | … | @@ -48,10 +51,10 @@ class MpttCommentFormNode(BaseMpttCommen |
48 |
51 |
class MpttCommentListNode(BaseMpttCommentNode): |
49 |
52 |
|
50 |
53 |
offset = getattr(settings, 'MPTT_COMMENTS_OFFSET', 20) |
51 |
||
54 |
||
52 |
55 |
cutoff_level = getattr(settings, 'MPTT_COMMENTS_CUTOFF', 3) |
53 |
bottom_level = 0 |
|
54 |
||
56 |
bottom_level = 0 |
|
57 |
||
55 |
58 |
def get_query_set(self, context): |
56 |
59 |
|
57 |
60 |
related = getattr(settings, 'MPTT_COMMENTS_SELECT_RELATED', None) |
| … | … | @@ -64,10 +67,10 @@ class MpttCommentListNode(BaseMpttCommen |
64 |
67 |
qs = qs.select_related(*related) |
65 |
68 |
|
66 |
69 |
return qs |
67 |
||
70 |
||
68 |
71 |
def get_context_value_from_queryset(self, context, qs): |
69 |
72 |
return list(qs[:self.offset]) |
70 |
||
73 |
||
71 |
74 |
def render(self, context): |
72 |
75 |
qs = self.get_query_set(context) |
73 |
76 |
context[self.as_varname] = self.get_context_value_from_queryset(context, qs) |
| … | … | @@ -77,8 +80,8 @@ class MpttCommentListNode(BaseMpttCommen |
77 |
80 |
context['collapse_levels_above'] = 2 |
78 |
81 |
context['cutoff_level'] = self.cutoff_level |
79 |
82 |
context['bottom_level'] = self.bottom_level |
80 |
return '' |
|
81 |
||
83 |
return '' |
|
84 |
||
82 |
85 |
def get_mptt_comment_list(parser, token): |
83 |
86 |
""" |
84 |
87 |
Gets the list of comments for the given params and populates the template |
| … | … | @@ -129,11 +132,11 @@ def children_count(comment): |
129 |
132 |
def mptt_comments_media(): |
130 |
133 |
|
131 |
134 |
return mark_safe( render_to_string( ('comments/comments_media.html',) , { }) ) |
132 |
||
135 |
||
133 |
136 |
def display_comment_toplevel_for(target): |
134 |
137 |
|
135 |
138 |
model = target.__class__ |
136 |
||
139 |
||
137 |
140 |
template_list = [ |
138 |
141 |
"comments/%s_%s_display_comments_toplevel.html" % tuple(str(model._meta).split(".")), |
139 |
142 |
"comments/%s_display_comments_toplevel.html" % model._meta.app_label, |
| … | … | @@ -142,10 +145,10 @@ def display_comment_toplevel_for(target) |
142 |
145 |
return render_to_string( |
143 |
146 |
template_list, { |
144 |
147 |
"object" : target |
145 |
} |
|
148 |
} |
|
146 |
149 |
# RequestContext(context['request'], {}) |
147 |
150 |
) |
148 |
||
151 |
||
149 |
152 |
register.filter(children_count) |
150 |
153 |
register.tag(get_mptt_comment_form) |
151 |
154 |
register.simple_tag(mptt_comment_form_target) |
