/
views.py
151 lines (124 loc) · 5.09 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from ella.ellacomments.models import CommentOptionsObject
import operator
from django.contrib import comments
from django.contrib.comments import signals
from django.contrib.comments.views.comments import CommentPostBadRequest
from django.utils.html import escape
from django.utils.translation import ugettext as _
from django.template.defaultfilters import slugify
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, Http404
from django.db.models import Q
from django.db import transaction
from django.core.paginator import Paginator
from django.conf import settings
from ella.core.views import get_templates_from_placement
@transaction.commit_on_success
def post_comment(request, context, parent_id=None):
'Mostly copy-pasted from django.contrib.comments.views.comments'
opts = CommentOptionsObject.objects.get_for_object(context['object'])
if opts.blocked:
raise Http404('Comments is blocked for this object.')
parent = None
if parent_id:
parent = get_object_or_404(comments.get_model(), pk=parent_id)
if request.method != 'POST':
initial = {}
if parent:
if parent.title.startswith('Re:'):
initial['title'] = parent.title
else:
initial['title'] = u'Re: %s' % parent.title
form = comments.get_form()(context['object'], parent=parent_id, initial=initial)
context.update({
'parent': parent,
'form': form,
})
return render_to_response(
get_templates_from_placement('comment_form.html', context['placement']),
context,
RequestContext(request)
)
# Fill out some initial data fields from an authenticated user, if present
data = request.POST.copy()
if request.user.is_authenticated():
if not data.get('name', ''):
data["name"] = request.user.get_full_name() or request.user.username
if not data.get('email', ''):
data["email"] = request.user.email
# construct the form
form = comments.get_form()(context['object'], data=data, parent=parent_id)
# Check security information
if form.security_errors():
return CommentPostBadRequest(
"The comment form failed security verification: %s" % \
escape(str(form.security_errors())))
# Do we want to preview the comment?
preview = "preview" in data
# Check to see if the POST data overrides the view's next argument.
next = data.get("next", "%s%s/" % (context['placement'].get_absolute_url(), slugify(_('comments'))))
# If there are errors or if we requested a preview show the comment
if form.errors or preview:
context.update({
"form" : form,
'parent': parent,
"next": next,
})
return render_to_response(
get_templates_from_placement(form.errors and 'comment_form.html' or 'comment_preview.html', context['placement']),
context,
RequestContext(request)
)
# Otherwise create the comment
comment = form.get_comment_object()
comment.ip_address = request.META.get("REMOTE_ADDR", None)
if request.user.is_authenticated():
comment.user = request.user
# Signal that the comment is about to be saved
responses = signals.comment_will_be_posted.send(
sender = comment.__class__,
comment = comment,
request = request
)
for (receiver, response) in responses:
if response == False:
return CommentPostBadRequest(
"comment_will_be_posted receiver %r killed the comment" % receiver.__name__)
# Save the comment and signal that it was saved
comment.save()
signals.comment_was_posted.send(
sender = comment.__class__,
comment = comment,
request = request
)
return HttpResponseRedirect(next)
def list_comments(request, context):
# basic queryset
qs = comments.get_model().objects.for_model(context['object']).order_by('tree_path')
# only individual branches requested
if 'ids' in request.GET:
ids = request.GET.getlist('ids')
# branch is everything whose tree_path begins with the same prefix
qs = qs.filter(reduce(operator.or_, map(lambda x: Q(tree_path__startswith=x.zfill(10)), ids)))
# pagination
if 'p' in request.GET and request.GET['p'].isdigit():
page_no = int(request.GET['p'])
else:
page_no = 1
paginate_by = getattr(settings, 'COMMENTS_PAGINATE_BY', 50)
paginator = Paginator(qs, paginate_by)
if page_no > paginator.num_pages or page_no < 1:
raise Http404()
page = paginator.page(page_no)
context.update({
'comment_list': page.object_list,
'page': page,
'is_paginated': paginator.num_pages > 1,
'results_per_page': paginate_by,
})
return render_to_response(
get_templates_from_placement('comment_list.html', context['placement']),
context,
RequestContext(request)
)