10
10
from threadedcomments.utils import JSONResponse, XMLResponse
11
11
from wl_utils import get_real_ip
13
14
def _adjust_max_comment_length(form, field_name='comment'):
15
Sets the maximum comment length to that default specified in the settings.
15
"""Sets the maximum comment length to that default specified in the
17
17
form.base_fields['comment'].max_length = DEFAULT_MAX_COMMENT_LENGTH
19
20
def _get_next(request):
21
The part that's the least straightforward about views in this module is how they
22
determine their redirects after they have finished computation.
21
"""The part that's the least straightforward about views in this module is
22
how they determine their redirects after they have finished computation.
24
24
In short, they will try and determine the next place to go in the following order:
30
30
3. If Django can determine the previous page from the HTTP headers, the view will
31
31
redirect to that previous page.
32
32
4. Otherwise, the view raise a 404 Not Found.
34
next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None)))
35
next = request.POST.get('next', request.GET.get(
36
'next', request.META.get('HTTP_REFERER', None)))
35
37
if not next or next == request.path:
36
raise Http404 # No next url was supplied in GET or POST.
38
raise Http404 # No next url was supplied in GET or POST.
39
42
def _preview(request, context_processors, extra_context, form_class=ThreadedCommentForm):
41
Returns a preview of the comment so that the user may decide if he or she wants to
42
edit it before submitting it permanently.
43
"""Returns a preview of the comment so that the user may decide if he or
44
she wants to edit it before submitting it permanently."""
44
45
_adjust_max_comment_length(form_class)
45
46
form = form_class(request.POST or None)
47
'next' : _get_next(request),
48
'next': _get_next(request),
50
51
if form.is_valid():
51
52
new_comment = form.save(commit=False)
54
55
context['comment'] = None
55
56
return render_to_response(
56
57
'threadedcomments/preview_comment.html',
58
context_instance = RequestContext(request, context, context_processors)
59
context_instance=RequestContext(request, context, context_processors)
61
63
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 parameters.
64
"""Receives POST data and either creates a new ``ThreadedComment`` or
65
``FreeThreadedComment``, or edits an old one based upon the specified
66
68
If there is a 'preview' key in the POST request, a preview will be forced and the
67
69
comment will not be saved until a 'preview' key is no longer in the POST request.
69
71
If it is an *AJAX* request (either XML or JSON), it will return a serialized
70
72
version of the last created ``ThreadedComment`` and there will be no redirect.
72
74
If invalid POST data is submitted, this will go to the comment preview page
73
75
where the comment may be edited until it does not contain errors.
75
78
if not edit_id and not (content_type and object_id):
76
raise Http404 # Must specify either content_type and object_id or edit_id
77
if "preview" in request.POST:
79
raise Http404 # Must specify either content_type and object_id or edit_id
80
if 'preview' in request.POST:
78
81
return _preview(request, context_processors, extra_context, form_class=form_class)
80
83
instance = get_object_or_404(model, id=edit_id)
86
89
new_comment = form.save(commit=False)
88
91
new_comment.ip_address = get_real_ip(request)
89
new_comment.content_type = get_object_or_404(ContentType, id = int(content_type))
92
new_comment.content_type = get_object_or_404(
93
ContentType, id=int(content_type))
90
94
new_comment.object_id = int(object_id)
91
95
if model == ThreadedComment:
92
96
new_comment.user = request.user
94
new_comment.parent = get_object_or_404(model, id = int(parent_id))
98
new_comment.parent = get_object_or_404(model, id=int(parent_id))
96
100
if model == ThreadedComment:
98
request.user.message_set.create(message="Your message has been posted successfully.")
102
request.user.message_set.create(
103
message='Your message has been posted successfully.')
100
105
request.session['successful_data'] = {
101
'name' : form.cleaned_data['name'],
102
'website' : form.cleaned_data['website'],
103
'email' : form.cleaned_data['email'],
106
'name': form.cleaned_data['name'],
107
'website': form.cleaned_data['website'],
108
'email': form.cleaned_data['email'],
105
110
if ajax == 'json':
106
return JSONResponse([new_comment,])
111
return JSONResponse([new_comment, ])
107
112
elif ajax == 'xml':
108
return XMLResponse([new_comment,])
113
return XMLResponse([new_comment, ])
110
115
return HttpResponseRedirect(_get_next(request))
112
return JSONResponse({'errors' : form.errors}, is_iterable=False)
117
return JSONResponse({'errors': form.errors}, is_iterable=False)
114
119
template_str = """
116
121
{% for error,name in errors %}
123
response_str = Template(template_str).render(Context({'errors' : zip(form.errors.values(), form.errors.keys())}))
128
response_str = Template(template_str).render(
129
Context({'errors': zip(form.errors.values(), form.errors.keys())}))
124
130
return XMLResponse(response_str, is_iterable=False)
126
132
return _preview(request, context_processors, extra_context, form_class=form_class)
128
135
def comment(*args, **kwargs):
130
Thin wrapper around free_comment which adds login_required status and also assigns
131
the ``model`` to be ``ThreadedComment``.
136
"""Thin wrapper around free_comment which adds login_required status and
137
also assigns the ``model`` to be ``ThreadedComment``."""
133
138
kwargs['model'] = ThreadedComment
134
139
kwargs['form_class'] = ThreadedCommentForm
135
140
return free_comment(*args, **kwargs)
136
141
# Require login to be required, as request.user must exist and be valid.
137
142
comment = login_required(comment)
139
145
def can_delete_comment(comment, user):
141
Default callback function to determine wether the given user has the
142
ability to delete the given comment.
146
"""Default callback function to determine wether the given user has the
147
ability to delete the given comment."""
144
148
if user.is_staff or user.is_superuser:
146
150
if hasattr(comment, 'user') and comment.user == user:
150
def comment_delete(request, object_id, model=ThreadedComment, extra_context = {}, context_processors = [], permission_callback=can_delete_comment):
152
Deletes the specified comment, which can be either a ``FreeThreadedComment`` or a
153
``ThreadedComment``. If it is a POST request, then the comment will be deleted
154
outright, however, if it is a GET request, a confirmation page will be shown.
155
def comment_delete(request, object_id, model=ThreadedComment, extra_context={}, context_processors=[], permission_callback=can_delete_comment):
156
"""Deletes the specified comment, which can be either a
157
``FreeThreadedComment`` or a ``ThreadedComment``.
159
If it is a POST request, then the comment will be deleted outright,
160
however, if it is a GET request, a confirmation page will be shown.
156
163
tc = get_object_or_404(model, id=int(object_id))
157
164
if not permission_callback(tc, request.user):
158
165
login_url = settings.LOGIN_URL
159
166
current_url = urlquote(request.get_full_path())
160
return HttpResponseRedirect("%s?next=%s" % (login_url, current_url))
161
if request.method == "POST":
167
return HttpResponseRedirect('%s?next=%s' % (login_url, current_url))
168
if request.method == 'POST':
163
170
return HttpResponseRedirect(_get_next(request))
170
177
is_threaded_comment = False
171
178
return render_to_response(
172
179
'threadedcomments/confirm_delete.html',
174
context_instance = RequestContext(
181
context_instance=RequestContext(
178
'is_free_threaded_comment' : is_free_threaded_comment,
179
'is_threaded_comment' : is_threaded_comment,
180
'next' : _get_next(request),
185
'is_free_threaded_comment': is_free_threaded_comment,
186
'is_threaded_comment': is_threaded_comment,
187
'next': _get_next(request),
182
189
context_processors
b'\\ No newline at end of file'