comparison gpp/forums/views.py @ 113:d97ceb95ce02

Forums: ForumLastVisit logic in place. Need to add code for topics and posts next.
author Brian Neal <bgneal@gmail.com>
date Sun, 11 Oct 2009 19:10:54 +0000
parents d1b0b86441c0
children 535d02d1c017
comparison
equal deleted inserted replaced
112:d1b0b86441c0 113:d97ceb95ce02
1 """ 1 """
2 Views for the forums application. 2 Views for the forums application.
3 """ 3 """
4 import datetime
5
4 from django.contrib.auth.decorators import login_required 6 from django.contrib.auth.decorators import login_required
5 from django.http import Http404 7 from django.http import Http404
6 from django.http import HttpResponse 8 from django.http import HttpResponse
7 from django.http import HttpResponseBadRequest 9 from django.http import HttpResponseBadRequest
8 from django.http import HttpResponseForbidden 10 from django.http import HttpResponseForbidden
16 from django.views.decorators.http import require_POST 18 from django.views.decorators.http import require_POST
17 from django.utils.text import wrap 19 from django.utils.text import wrap
18 20
19 from core.paginator import DiggPaginator 21 from core.paginator import DiggPaginator
20 from core.functions import email_admins 22 from core.functions import email_admins
21 from forums.models import Forum 23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
22 from forums.models import Topic 24 ForumLastVisit
23 from forums.models import Post
24 from forums.models import FlaggedPost
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm
26 from forums.unread import get_forum_unread_status
26 27
27 ####################################################################### 28 #######################################################################
28 29
29 TOPICS_PER_PAGE = 50 30 TOPICS_PER_PAGE = 50
30 POSTS_PER_PAGE = 2 31 POSTS_PER_PAGE = 20
31 32
32 def create_topic_paginator(topics): 33 def create_topic_paginator(topics):
33 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2) 34 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
34 35
35 def create_post_paginator(posts): 36 def create_post_paginator(posts):
40 def index(request): 41 def index(request):
41 """ 42 """
42 This view displays all the forums available, ordered in each category. 43 This view displays all the forums available, ordered in each category.
43 """ 44 """
44 forums = Forum.objects.forums_for_user(request.user) 45 forums = Forum.objects.forums_for_user(request.user)
46 get_forum_unread_status(forums, request.user)
45 cats = {} 47 cats = {}
46 for forum in forums: 48 for forum in forums:
47 cat = cats.setdefault(forum.category.id, { 49 cat = cats.setdefault(forum.category.id, {
48 'cat': forum.category, 50 'cat': forum.category,
49 'forums': [], 51 'forums': [],
109 page = paginator.page(page_num) 111 page = paginator.page(page_num)
110 except InvalidPage: 112 except InvalidPage:
111 raise Http404 113 raise Http404
112 114
113 last_page = page_num == paginator.num_pages 115 last_page = page_num == paginator.num_pages
116
117 if request.user.is_authenticated() and last_page:
118 _update_last_visit(request.user, topic)
114 119
115 # we do this for the template since it is rendered twice 120 # we do this for the template since it is rendered twice
116 page_nav = render_to_string('forums/pagination.html', {'page': page}) 121 page_nav = render_to_string('forums/pagination.html', {'page': page})
117 122
118 can_moderate = _can_moderate(topic.forum, request.user) 123 can_moderate = _can_moderate(topic.forum, request.user)
189 if not _can_post_in_topic(form.topic, request.user): 194 if not _can_post_in_topic(form.topic, request.user):
190 return HttpResponseForbidden("You don't have permission to post in this topic.") 195 return HttpResponseForbidden("You don't have permission to post in this topic.")
191 196
192 post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) 197 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
193 _bump_post_count(request.user) 198 _bump_post_count(request.user)
199 _update_last_visit(request.user, form.topic)
194 return render_to_response('forums/display_post.html', { 200 return render_to_response('forums/display_post.html', {
195 'post': post, 201 'post': post,
202 'can_moderate': _can_moderate(form.topic.forum, request.user),
196 }, 203 },
197 context_instance=RequestContext(request)) 204 context_instance=RequestContext(request))
198 205
199 return HttpResponseBadRequest("Invalid post."); 206 return HttpResponseBadRequest("Invalid post.");
200 207
366 post.topic = topic 373 post.topic = topic
367 post.user = request.user 374 post.user = request.user
368 post.user_ip = request.META.get("REMOTE_ADDR", "") 375 post.user_ip = request.META.get("REMOTE_ADDR", "")
369 post.save() 376 post.save()
370 _bump_post_count(request.user) 377 _bump_post_count(request.user)
378 _update_last_visit(request.user, topic)
371 return HttpResponseRedirect(post.get_absolute_url()) 379 return HttpResponseRedirect(post.get_absolute_url())
372 else: 380 else:
373 quote_id = request.GET.get('quote') 381 quote_id = request.GET.get('quote')
374 if quote_id: 382 if quote_id:
375 quote_post = get_object_or_404(Post.objects.select_related(), 383 quote_post = get_object_or_404(Post.objects.select_related(),
513 'form': form, 521 'form': form,
514 }, 522 },
515 context_instance=RequestContext(request)) 523 context_instance=RequestContext(request))
516 524
517 525
526 @login_required
527 @require_POST
528 def forum_catchup(request, slug):
529 """
530 This view marks all the topics in the forum as being read.
531 """
532 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
533
534 if not forum.category.can_access(request.user):
535 return HttpResponseForbidden()
536
537 forum.catchup(request.user)
538 return HttpResponseRedirect(forum.get_absolute_url())
539
540
518 def _can_moderate(forum, user): 541 def _can_moderate(forum, user):
519 """ 542 """
520 Determines if a user has permission to moderate a given forum. 543 Determines if a user has permission to moderate a given forum.
521 """ 544 """
522 return user.is_authenticated() and ( 545 return user.is_authenticated() and (
603 topics = Topic.objects.filter(pk__in=topic_ids).select_related() 626 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
604 for topic in topics: 627 for topic in topics:
605 if topic.forum == old_forum: 628 if topic.forum == old_forum:
606 _move_topic(topic, old_forum, new_forum) 629 _move_topic(topic, old_forum, new_forum)
607 630
631
632 def _update_last_visit(user, topic):
633 """
634 Does the bookkeeping for the last visit status for the user to the
635 topic/forum.
636 """
637 now = datetime.datetime.now()
638 try:
639 flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
640 except ForumLastVisit.DoesNotExist:
641 flv = ForumLastVisit(user=user, forum=topic.forum)
642 flv.begin_date = now
643
644 flv.end_date = now
645 flv.save()
646
647 if topic.update_date > flv.begin_date:
648 try:
649 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
650 except TopicLastVisit.DoesNotExist:
651 tlv = TopicLastVisit(user=user, topic=topic)
652
653 tlv.touch()
654 tlv.save()
655