comparison gpp/forums/views/main.py @ 285:8fd4984d5c3b

This is a first rough commit for #95, adding the ability to embed YouTube videos in forum posts. Some more polish and testing needs to happen at this point. I wanted to get all these changes off my hard drive and into the repository.
author Brian Neal <bgneal@gmail.com>
date Thu, 14 Oct 2010 02:39:35 +0000
parents 21d2ca3b4bf7
children 72fd300685d5
comparison
equal deleted inserted replaced
284:df2c81f705a8 285:8fd4984d5c3b
21 from django.db.models import F 21 from django.db.models import F
22 22
23 from core.paginator import DiggPaginator 23 from core.paginator import DiggPaginator
24 from core.functions import email_admins 24 from core.functions import email_admins
25 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ 25 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
26 ForumLastVisit 26 ForumLastVisit, Attachment
27 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ 27 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
28 SplitTopicForm 28 SplitTopicForm
29 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ 29 from forums.unread import get_forum_unread_status, get_topic_unread_status, \
30 get_post_unread_status, get_unread_topics 30 get_post_unread_status, get_unread_topics
31 31
32 from bio.models import UserProfile 32 from bio.models import UserProfile
33 import antispam 33 import antispam
34 import antispam.utils 34 import antispam.utils
35 from forums.attachments import AttachmentProcessor
35 36
36 ####################################################################### 37 #######################################################################
37 38
38 TOPICS_PER_PAGE = 50 39 TOPICS_PER_PAGE = 50
39 POSTS_PER_PAGE = 20 40 POSTS_PER_PAGE = 20
182 profiles = UserProfile.objects.filter(user__id__in=users).select_related() 183 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
183 user_profiles = dict((profile.user.id, profile) for profile in profiles) 184 user_profiles = dict((profile.user.id, profile) for profile in profiles)
184 185
185 for post in page.object_list: 186 for post in page.object_list:
186 post.user_profile = user_profiles[post.user.id] 187 post.user_profile = user_profiles[post.user.id]
188 post.attach_list = []
189
190 # Attach any attachments
191 post_ids = [post.pk for post in page.object_list]
192 attachments = Attachment.objects.filter(post__in=post_ids).select_related(
193 'embed').order_by('order')
194
195 post_dict = dict((post.pk, post) for post in page.object_list)
196 for item in attachments:
197 post_dict[item.post.id].attach_list.append(item.embed)
187 198
188 last_page = page_num == paginator.num_pages 199 last_page = page_num == paginator.num_pages
189 200
190 if request.user.is_authenticated() and last_page: 201 if request.user.is_authenticated() and last_page:
191 _update_last_visit(request.user, topic) 202 _update_last_visit(request.user, topic)
281 return HttpResponseForbidden(antispam.BUSTED_MESSAGE) 292 return HttpResponseForbidden(antispam.BUSTED_MESSAGE)
282 293
283 post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) 294 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
284 post.unread = True 295 post.unread = True
285 post.user_profile = request.user.get_profile() 296 post.user_profile = request.user.get_profile()
297 post.attach_list = post.attachments.all()
286 _bump_post_count(request.user) 298 _bump_post_count(request.user)
287 _update_last_visit(request.user, form.topic) 299 _update_last_visit(request.user, form.topic)
300
288 return render_to_response('forums/display_post.html', { 301 return render_to_response('forums/display_post.html', {
289 'post': post, 302 'post': post,
290 'can_moderate': _can_moderate(form.topic.forum, request.user), 303 'can_moderate': _can_moderate(form.topic.forum, request.user),
291 'can_reply': True, 304 'can_reply': True,
292 }, 305 },
357 if antispam.utils.spam_check(request, form.cleaned_data['body']): 370 if antispam.utils.spam_check(request, form.cleaned_data['body']):
358 return HttpResponseRedirect(reverse('antispam-suspended')) 371 return HttpResponseRedirect(reverse('antispam-suspended'))
359 post = form.save(commit=False) 372 post = form.save(commit=False)
360 post.touch() 373 post.touch()
361 post.save() 374 post.save()
375
376 # Save any attachments
377 attach_proc = AttachmentProcessor(request.POST.getlist('attachment'))
378 attach_proc.save_attachments(post)
379
362 return HttpResponseRedirect(post.get_absolute_url()) 380 return HttpResponseRedirect(post.get_absolute_url())
363 else: 381 else:
364 form = PostForm(instance=post) 382 form = PostForm(instance=post)
365 383
366 post.user_profile = request.user.get_profile() 384 post.user_profile = request.user.get_profile()
437 forum = topic.forum 455 forum = topic.forum
438 if forum.last_post == post: 456 if forum.last_post == post:
439 forum.last_post_pre_delete() 457 forum.last_post_pre_delete()
440 forum.save() 458 forum.save()
441 459
460 # delete any attachments
461 post.attachments.clear()
462
442 # Should be safe to delete the post now: 463 # Should be safe to delete the post now:
443 post.delete() 464 post.delete()
444 465
445 466
446 def _delete_topic(topic): 467 def _delete_topic(topic):
456 topic.forum.save() 477 topic.forum.save()
457 478
458 # delete subscriptions to this topic 479 # delete subscriptions to this topic
459 topic.subscribers.clear() 480 topic.subscribers.clear()
460 topic.bookmarkers.clear() 481 topic.bookmarkers.clear()
482
483 # delete all attachments
484 posts = Post.objects.filter(topic=topic)
485 for post in posts:
486 post.attachments.clear()
461 487
462 # It should be safe to just delete the topic now. This will 488 # It should be safe to just delete the topic now. This will
463 # automatically delete all posts in the topic. 489 # automatically delete all posts in the topic.
464 topic.delete() 490 topic.delete()
465 491
482 post = form.save(commit=False) 508 post = form.save(commit=False)
483 post.topic = topic 509 post.topic = topic
484 post.user = request.user 510 post.user = request.user
485 post.user_ip = request.META.get("REMOTE_ADDR", "") 511 post.user_ip = request.META.get("REMOTE_ADDR", "")
486 post.save() 512 post.save()
513
514 # Save any attachments
515 attach_proc = AttachmentProcessor(request.POST.getlist('attachment'))
516 attach_proc.save_attachments(post)
517
487 _bump_post_count(request.user) 518 _bump_post_count(request.user)
488 _update_last_visit(request.user, topic) 519 _update_last_visit(request.user, topic)
489 return HttpResponseRedirect(post.get_absolute_url()) 520 return HttpResponseRedirect(post.get_absolute_url())
490 else: 521 else:
491 quote_id = request.GET.get('quote') 522 quote_id = request.GET.get('quote')