Mercurial > public > sg101
comparison gpp/forums/views.py @ 115:0ce0104c7df3
Forums: split topic.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 22 Oct 2009 02:57:18 +0000 |
parents | 535d02d1c017 |
children | a3633f39f3ce |
comparison
equal
deleted
inserted
replaced
114:535d02d1c017 | 115:0ce0104c7df3 |
---|---|
20 | 20 |
21 from core.paginator import DiggPaginator | 21 from core.paginator import DiggPaginator |
22 from core.functions import email_admins | 22 from core.functions import email_admins |
23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ | 23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ |
24 ForumLastVisit | 24 ForumLastVisit |
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm | 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ |
26 SplitTopicForm | |
26 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ | 27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ |
27 get_post_unread_status | 28 get_post_unread_status |
28 | 29 |
29 ####################################################################### | 30 ####################################################################### |
30 | 31 |
269 return HttpResponseForbidden("You don't have permission to edit that post.") | 270 return HttpResponseForbidden("You don't have permission to edit that post.") |
270 | 271 |
271 if request.method == "POST": | 272 if request.method == "POST": |
272 form = PostForm(request.POST, instance=post) | 273 form = PostForm(request.POST, instance=post) |
273 if form.is_valid(): | 274 if form.is_valid(): |
274 form.save() | 275 post = form.save(commit=False) |
276 post.touch() | |
277 post.save() | |
275 return HttpResponseRedirect(post.get_absolute_url()) | 278 return HttpResponseRedirect(post.get_absolute_url()) |
276 else: | 279 else: |
277 form = PostForm(instance=post) | 280 form = PostForm(instance=post) |
278 | 281 |
279 return render_to_response('forums/edit_post.html', { | 282 return render_to_response('forums/edit_post.html', { |
542 | 545 |
543 forum.catchup(request.user) | 546 forum.catchup(request.user) |
544 return HttpResponseRedirect(forum.get_absolute_url()) | 547 return HttpResponseRedirect(forum.get_absolute_url()) |
545 | 548 |
546 | 549 |
550 @login_required | |
551 def mod_topic_split(request, id): | |
552 """ | |
553 This view function allows moderators to split posts off to a new topic. | |
554 """ | |
555 topic = get_object_or_404(Topic.objects.select_related(), pk=id) | |
556 if not _can_moderate(topic.forum, request.user): | |
557 return HttpResponseRedirect(topic.get_absolute_url()) | |
558 | |
559 if request.method == "POST": | |
560 form = SplitTopicForm(request.user, request.POST) | |
561 if form.is_valid(): | |
562 if form.split_at: | |
563 _split_topic_at(topic, form.post_ids[0], | |
564 form.cleaned_data['forums'], | |
565 form.cleaned_data['name']) | |
566 else: | |
567 _split_topic(topic, form.post_ids, | |
568 form.cleaned_data['forums'], | |
569 form.cleaned_data['name']) | |
570 | |
571 return HttpResponseRedirect(topic.get_absolute_url()) | |
572 else: | |
573 form = SplitTopicForm(request.user) | |
574 | |
575 posts = topic.posts.select_related() | |
576 | |
577 return render_to_response('forums/mod_split_topic.html', { | |
578 'forum': topic.forum, | |
579 'topic': topic, | |
580 'posts': posts, | |
581 'form': form, | |
582 }, | |
583 context_instance=RequestContext(request)) | |
584 | |
585 | |
547 def _can_moderate(forum, user): | 586 def _can_moderate(forum, user): |
548 """ | 587 """ |
549 Determines if a user has permission to moderate a given forum. | 588 Determines if a user has permission to moderate a given forum. |
550 """ | 589 """ |
551 return user.is_authenticated() and ( | 590 return user.is_authenticated() and ( |
657 tlv = TopicLastVisit(user=user, topic=topic) | 696 tlv = TopicLastVisit(user=user, topic=topic) |
658 | 697 |
659 tlv.touch() | 698 tlv.touch() |
660 tlv.save() | 699 tlv.save() |
661 | 700 |
701 | |
702 def _split_topic_at(topic, post_id, new_forum, new_name): | |
703 """ | |
704 This function splits the post given by post_id and all posts that come | |
705 after it in the given topic to a new topic in a new forum. | |
706 It is assumed the caller has been checked for moderator rights. | |
707 """ | |
708 post = get_object_or_404(Post, id=post_id) | |
709 if post.topic == topic: | |
710 post_ids = Post.objects.filter(topic=topic, | |
711 creation_date__gte=post.creation_date).values_list('id', flat=True) | |
712 _split_topic(topic, post_ids, new_forum, new_name) | |
713 | |
714 | |
715 def _split_topic(topic, post_ids, new_forum, new_name): | |
716 """ | |
717 This function splits the posts given by the post_ids list in the | |
718 given topic to a new topic in a new forum. | |
719 It is assumed the caller has been checked for moderator rights. | |
720 """ | |
721 posts = Post.objects.filter(topic=topic, id__in=post_ids) | |
722 if len(posts) > 0: | |
723 new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user) | |
724 new_topic.save() | |
725 for post in posts: | |
726 post.topic = new_topic | |
727 post.save() | |
728 | |
729 topic.post_count_update() | |
730 topic.save() | |
731 new_topic.post_count_update() | |
732 new_topic.save() | |
733 topic.forum.sync() | |
734 topic.forum.save() | |
735 new_forum.sync() | |
736 new_forum.save() |