5
5
from django.template import RequestContext, Context, Template
6
6
from django.utils.http import urlquote
7
7
from django.conf import settings
8
from threadedcomments.forms import FreeThreadedCommentForm, ThreadedCommentForm
9
from threadedcomments.models import ThreadedComment, FreeThreadedComment, DEFAULT_MAX_COMMENT_LENGTH
8
from threadedcomments.forms import ThreadedCommentForm
9
from threadedcomments.models import ThreadedComment, DEFAULT_MAX_COMMENT_LENGTH
10
10
from threadedcomments.utils import JSONResponse, XMLResponse
11
11
from wl_utils import get_real_ip
62
def free_comment(request, content_type=None, object_id=None, edit_id=None, parent_id=None, add_messages=False, ajax=False, model=FreeThreadedComment, form_class=FreeThreadedCommentForm, context_processors=[], extra_context={}):
63
"""Receives POST data and either creates a new ``ThreadedComment`` or
64
``FreeThreadedComment``, or edits an old one based upon the specified
63
def comment(request, content_type=None, object_id=None, edit_id=None, parent_id=None, add_messages=False, ajax=False, context_processors=[], extra_context={}):
64
"""Receives POST data and creates a new ``ThreadedComment``, or
65
edits an old one based upon the specified parameters.
67
67
If there is a 'preview' key in the POST request, a preview will be forced and the
68
68
comment will not be saved until a 'preview' key is no longer in the POST request.
87
89
if form.is_valid():
88
90
new_comment = form.save(commit=False)
90
new_comment.ip_address = get_real_ip(request)
91
92
new_comment.content_type = get_object_or_404(
92
93
ContentType, id=int(content_type))
93
94
new_comment.object_id = int(object_id)
94
if model == ThreadedComment:
95
new_comment.user = request.user
96
new_comment.user = request.user
97
99
new_comment.parent = get_object_or_404(model, id=int(parent_id))
98
100
new_comment.save()
99
if model == ThreadedComment:
101
request.user.message_set.create(
102
message='Your message has been posted successfully.')
104
request.session['successful_data'] = {
105
'name': form.cleaned_data['name'],
106
'website': form.cleaned_data['website'],
107
'email': form.cleaned_data['email'],
102
request.user.message_set.create(
103
message='Your message has been posted successfully.')
109
105
if ajax == 'json':
110
106
return JSONResponse([new_comment, ])
111
107
elif ajax == 'xml':
129
125
return XMLResponse(response_str, is_iterable=False)
131
127
return _preview(request, context_processors, extra_context, form_class=form_class)
134
def comment(*args, **kwargs):
135
"""Thin wrapper around free_comment which adds login_required status and
136
also assigns the ``model`` to be ``ThreadedComment``."""
137
kwargs['model'] = ThreadedComment
138
kwargs['form_class'] = ThreadedCommentForm
139
return free_comment(*args, **kwargs)
140
# Require login to be required, as request.user must exist and be valid.
141
comment = login_required(comment)
144
def can_delete_comment(comment, user):
145
"""Default callback function to determine wether the given user has the
146
ability to delete the given comment."""
147
if user.is_staff or user.is_superuser:
149
if hasattr(comment, 'user') and comment.user == user:
153
# Todo: Next one is not used so far and may need adjustments to the render()
154
def comment_delete(request, object_id, model=ThreadedComment, extra_context={}, context_processors=[], permission_callback=can_delete_comment):
155
"""Deletes the specified comment, which can be either a
156
``FreeThreadedComment`` or a ``ThreadedComment``.
158
If it is a POST request, then the comment will be deleted outright,
159
however, if it is a GET request, a confirmation page will be shown.
162
tc = get_object_or_404(model, id=int(object_id))
163
if not permission_callback(tc, request.user):
164
login_url = settings.LOGIN_URL
165
current_url = urlquote(request.get_full_path())
166
return HttpResponseRedirect('%s?next=%s' % (login_url, current_url))
167
if request.method == 'POST':
169
return HttpResponseRedirect(_get_next(request))
171
if model == ThreadedComment:
172
is_free_threaded_comment = False
173
is_threaded_comment = True
175
is_free_threaded_comment = True
176
is_threaded_comment = False
178
extra_context.update(
180
'is_free_threaded_comment': is_free_threaded_comment,
181
'is_threaded_comment': is_threaded_comment,
182
'next': _get_next(request),
185
return render(request,
186
'threadedcomments/confirm_delete.html',