Mercurial > public > sg101
comparison gpp/forums/views.py @ 108:80ab249d1adc
Forums: quoting existing posts.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 Sep 2009 03:55:50 +0000 |
parents | e94398f5e027 |
children | 07be3e39e639 |
comparison
equal
deleted
inserted
replaced
107:e94398f5e027 | 108:80ab249d1adc |
---|---|
12 from django.shortcuts import get_object_or_404 | 12 from django.shortcuts import get_object_or_404 |
13 from django.shortcuts import render_to_response | 13 from django.shortcuts import render_to_response |
14 from django.template.loader import render_to_string | 14 from django.template.loader import render_to_string |
15 from django.template import RequestContext | 15 from django.template import RequestContext |
16 from django.views.decorators.http import require_POST | 16 from django.views.decorators.http import require_POST |
17 from django.utils.text import wrap | |
17 | 18 |
18 from core.paginator import DiggPaginator | 19 from core.paginator import DiggPaginator |
19 from core.functions import email_admins | 20 from core.functions import email_admins |
20 from forums.models import Forum | 21 from forums.models import Forum |
21 from forums.models import Topic | 22 from forums.models import Topic |
143 | 144 |
144 if request.method == 'POST': | 145 if request.method == 'POST': |
145 form = NewTopicForm(request.user, forum, request.POST) | 146 form = NewTopicForm(request.user, forum, request.POST) |
146 if form.is_valid(): | 147 if form.is_valid(): |
147 topic = form.save(request.META.get("REMOTE_ADDR")) | 148 topic = form.save(request.META.get("REMOTE_ADDR")) |
149 _bump_post_count(request.user) | |
148 return HttpResponseRedirect(reverse('forums-new_topic_thanks', | 150 return HttpResponseRedirect(reverse('forums-new_topic_thanks', |
149 kwargs={'tid': topic.pk})) | 151 kwargs={'tid': topic.pk})) |
150 else: | 152 else: |
151 form = NewTopicForm(request.user, forum) | 153 form = NewTopicForm(request.user, forum) |
152 | 154 |
177 function is meant to be the target of an AJAX post, and returns | 179 function is meant to be the target of an AJAX post, and returns |
178 the HTML for the new post, which the client-side script appends | 180 the HTML for the new post, which the client-side script appends |
179 to the document. | 181 to the document. |
180 """ | 182 """ |
181 if not request.user.is_authenticated(): | 183 if not request.user.is_authenticated(): |
182 return HttpResponseForbidden() | 184 return HttpResponseForbidden('Please login or register to post.') |
183 | 185 |
184 form = NewPostForm(request.POST) | 186 form = NewPostForm(request.POST) |
185 if form.is_valid(): | 187 if form.is_valid(): |
186 if form.topic.locked or not form.topic.forum.category.can_access(request.user): | 188 if not _can_post_in_topic(form.topic, request.user): |
187 return HttpResponseForbidden() | 189 return HttpResponseForbidden("You don't have permission to post in this topic.") |
188 | 190 |
189 post = form.save(request.user, request.META.get("REMOTE_ADDR")) | 191 post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) |
192 _bump_post_count(request.user) | |
190 return render_to_response('forums/display_post.html', { | 193 return render_to_response('forums/display_post.html', { |
191 'post': post, | 194 'post': post, |
192 }, | 195 }, |
193 context_instance=RequestContext(request)) | 196 context_instance=RequestContext(request)) |
194 | 197 |
195 return HttpResponseBadRequest(); | 198 return HttpResponseBadRequest("Invalid post."); |
196 | 199 |
197 | 200 |
198 def goto_post(request, post_id): | 201 def goto_post(request, post_id): |
199 """ | 202 """ |
200 This function calculates what page a given post is on, then redirects | 203 This function calculates what page a given post is on, then redirects |
242 """ | 245 """ |
243 This view function allows authorized users to edit posts. | 246 This view function allows authorized users to edit posts. |
244 The superuser, forum moderators, and original author can edit posts. | 247 The superuser, forum moderators, and original author can edit posts. |
245 """ | 248 """ |
246 post = get_object_or_404(Post.objects.select_related(), pk=id) | 249 post = get_object_or_404(Post.objects.select_related(), pk=id) |
247 can_edit = request.user == post.user or \ | 250 |
248 request.user.is_superuser or \ | 251 can_moderate = request.user.is_superuser or \ |
249 request.user in post.topic.forum.moderators.all() | 252 request.user in post.topic.forum.moderators.all() |
253 | |
254 can_edit = can_moderate or request.user == post.user | |
250 | 255 |
251 if not can_edit: | 256 if not can_edit: |
252 return HttpResponseForbidden("You don't have permission to edit that post.") | 257 return HttpResponseForbidden("You don't have permission to edit that post.") |
253 | 258 |
254 if request.method == "POST": | 259 if request.method == "POST": |
262 return render_to_response('forums/edit_post.html', { | 267 return render_to_response('forums/edit_post.html', { |
263 'forum': post.topic.forum, | 268 'forum': post.topic.forum, |
264 'topic': post.topic, | 269 'topic': post.topic, |
265 'post': post, | 270 'post': post, |
266 'form': form, | 271 'form': form, |
267 'can_moderate': True, | 272 'can_moderate': can_moderate, |
268 }, | 273 }, |
269 context_instance=RequestContext(request)) | 274 context_instance=RequestContext(request)) |
270 | 275 |
271 | 276 |
272 @require_POST | 277 @require_POST |
341 topic.forum.save() | 346 topic.forum.save() |
342 | 347 |
343 # It should be safe to just delete the topic now. This will | 348 # It should be safe to just delete the topic now. This will |
344 # automatically delete all posts in the topic. | 349 # automatically delete all posts in the topic. |
345 topic.delete() | 350 topic.delete() |
351 | |
352 | |
353 @login_required | |
354 def new_post(request, topic_id): | |
355 """ | |
356 This function is the view for creating a normal, non-quick reply | |
357 to a topic. | |
358 """ | |
359 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id) | |
360 can_post = _can_post_in_topic(topic, request.user) | |
361 | |
362 if can_post: | |
363 if request.method == 'POST': | |
364 form = PostForm(request.POST) | |
365 if form.is_valid(): | |
366 post = form.save(commit=False) | |
367 post.topic = topic | |
368 post.user = request.user | |
369 post.user_ip = request.META.get("REMOTE_ADDR", "") | |
370 post.save() | |
371 _bump_post_count(request.user) | |
372 return HttpResponseRedirect(post.get_absolute_url()) | |
373 else: | |
374 quote_id = request.GET.get('quote') | |
375 if quote_id: | |
376 quote_post = get_object_or_404(Post.objects.select_related(), | |
377 pk=quote_id) | |
378 form = PostForm(initial={'body': _quote_message(quote_post.user.username, | |
379 quote_post.body)}) | |
380 else: | |
381 form = PostForm() | |
382 else: | |
383 form = None | |
384 | |
385 return render_to_response('forums/new_post.html', { | |
386 'forum': topic.forum, | |
387 'topic': topic, | |
388 'form': form, | |
389 'can_post': can_post, | |
390 }, | |
391 context_instance=RequestContext(request)) | |
392 | |
393 | |
394 def _can_post_in_topic(topic, user): | |
395 """ | |
396 This function returns true if the given user can post in the given topic | |
397 and false otherwise. | |
398 """ | |
399 return (not topic.locked and topic.forum.category.can_access(user)) or \ | |
400 (user.is_superuser or user in topic.forum.moderators.all()) | |
401 | |
402 | |
403 def _bump_post_count(user): | |
404 """ | |
405 Increments the forum_post_count for the given user. | |
406 """ | |
407 profile = user.get_profile() | |
408 profile.forum_post_count += 1 | |
409 profile.save() | |
410 | |
411 | |
412 def _quote_message(who, message): | |
413 """ | |
414 Builds a message reply by quoting the existing message in a | |
415 typical email-like fashion. The quoting is compatible with Markdown. | |
416 """ | |
417 header = '*%s wrote:*\n\n' % (who, ) | |
418 lines = wrap(message, 55).split('\n') | |
419 for i, line in enumerate(lines): | |
420 lines[i] = '> ' + line | |
421 return header + '\n'.join(lines) |