comparison gpp/forums/views.py @ 169:7071b196ddd5

Implement #54; add a forum query to display a user's posts.
author Brian Neal <bgneal@gmail.com>
date Sun, 31 Jan 2010 04:52:08 +0000
parents e6d4dfdfbc64
children 6f14970b103a
comparison
equal deleted inserted replaced
168:e6d4dfdfbc64 169:7071b196ddd5
11 from django.http import HttpResponseRedirect 11 from django.http import HttpResponseRedirect
12 from django.core.urlresolvers import reverse 12 from django.core.urlresolvers import reverse
13 from django.core.paginator import InvalidPage 13 from django.core.paginator import InvalidPage
14 from django.shortcuts import get_object_or_404 14 from django.shortcuts import get_object_or_404
15 from django.shortcuts import render_to_response 15 from django.shortcuts import render_to_response
16 from django.shortcuts import redirect
16 from django.template.loader import render_to_string 17 from django.template.loader import render_to_string
17 from django.template import RequestContext 18 from django.template import RequestContext
18 from django.views.decorators.http import require_POST 19 from django.views.decorators.http import require_POST
19 from django.utils.text import wrap 20 from django.utils.text import wrap
20 21
73 This view displays all the forums available, ordered in each category. 74 This view displays all the forums available, ordered in each category.
74 """ 75 """
75 # check for special forum queries 76 # check for special forum queries
76 query = request.GET.get("query") 77 query = request.GET.get("query")
77 if query == "unread": 78 if query == "unread":
78 return HttpResponseRedirect(reverse('forums-unread_topics')) 79 return redirect('forums-unread_topics')
79 elif query == "unanswered": 80 elif query == "unanswered":
80 return HttpResponseRedirect(reverse('forums-unanswered_topics')) 81 return redirect('forums-unanswered_topics')
82 elif query == "mine":
83 return redirect('forums-my_posts')
81 84
82 forums = Forum.objects.forums_for_user(request.user) 85 forums = Forum.objects.forums_for_user(request.user)
83 get_forum_unread_status(forums, request.user) 86 get_forum_unread_status(forums, request.user)
84 cats = {} 87 cats = {}
85 for forum in forums: 88 for forum in forums:
695 'page_nav': page_nav, 698 'page_nav': page_nav,
696 }, 699 },
697 context_instance=RequestContext(request)) 700 context_instance=RequestContext(request))
698 701
699 702
703 @login_required
704 def my_posts(request):
705 """Displays a list of posts the requesting user made."""
706
707 forum_ids = Forum.objects.forum_ids_for_user(request.user)
708 posts = Post.objects.filter(user=request.user,
709 topic__forum__id__in=forum_ids).order_by(
710 '-creation_date').select_related()
711
712 paginator = create_post_paginator(posts)
713 page_num = get_page_num(request)
714 try:
715 page = paginator.page(page_num)
716 except InvalidPage:
717 raise Http404
718
719 # we do this for the template since it is rendered twice
720 page_nav = render_to_string('forums/pagination.html', {'page': page})
721
722 return render_to_response('forums/post_list.html', {
723 'title': 'My Posts',
724 'page': page,
725 'page_nav': page_nav,
726 },
727 context_instance=RequestContext(request))
728
729
700 def _can_moderate(forum, user): 730 def _can_moderate(forum, user):
701 """ 731 """
702 Determines if a user has permission to moderate a given forum. 732 Determines if a user has permission to moderate a given forum.
703 """ 733 """
704 return user.is_authenticated() and ( 734 return user.is_authenticated() and (