comparison gpp/forums/views.py @ 172:0fa78ef80356

Implementing #55 - Add function to view a users posts from their profile.
author Brian Neal <bgneal@gmail.com>
date Sun, 28 Feb 2010 22:20:15 +0000
parents 6f14970b103a
children 500e5875a306
comparison
equal deleted inserted replaced
171:b1766b1bda45 172:0fa78ef80356
2 Views for the forums application. 2 Views for the forums application.
3 """ 3 """
4 import datetime 4 import datetime
5 5
6 from django.contrib.auth.decorators import login_required 6 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.models import User
7 from django.http import Http404 8 from django.http import Http404
8 from django.http import HttpResponse 9 from django.http import HttpResponse
9 from django.http import HttpResponseBadRequest 10 from django.http import HttpResponseBadRequest
10 from django.http import HttpResponseForbidden 11 from django.http import HttpResponseForbidden
11 from django.http import HttpResponseRedirect 12 from django.http import HttpResponseRedirect
712 713
713 714
714 @login_required 715 @login_required
715 def my_posts(request): 716 def my_posts(request):
716 """Displays a list of posts the requesting user made.""" 717 """Displays a list of posts the requesting user made."""
717 718 return _user_posts(request, request.user, request.user, 'My Posts')
718 forum_ids = Forum.objects.forum_ids_for_user(request.user) 719
719 posts = Post.objects.filter(user=request.user, 720
721 @login_required
722 def posts_for_user(request, username):
723 """Displays a list of posts by the given user.
724 Only the forums that the requesting user can see are examined.
725 """
726 target_user = get_object_or_404(User, username=username)
727 return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
728
729
730 def _user_posts(request, target_user, req_user, page_title):
731 """Displays a list of posts made by the target user.
732 req_user is the user trying to view the posts. Only the forums
733 req_user can see are searched.
734 """
735 forum_ids = Forum.objects.forum_ids_for_user(req_user)
736 posts = Post.objects.filter(user=target_user,
720 topic__forum__id__in=forum_ids).order_by( 737 topic__forum__id__in=forum_ids).order_by(
721 '-creation_date').select_related() 738 '-creation_date').select_related()
722 739
723 paginator = create_post_paginator(posts) 740 paginator = create_post_paginator(posts)
724 page_num = get_page_num(request) 741 page_num = get_page_num(request)
729 746
730 # we do this for the template since it is rendered twice 747 # we do this for the template since it is rendered twice
731 page_nav = render_to_string('forums/pagination.html', {'page': page}) 748 page_nav = render_to_string('forums/pagination.html', {'page': page})
732 749
733 return render_to_response('forums/post_list.html', { 750 return render_to_response('forums/post_list.html', {
734 'title': 'My Posts', 751 'title': page_title,
735 'page': page, 752 'page': page,
736 'page_nav': page_nav, 753 'page_nav': page_nav,
737 }, 754 },
738 context_instance=RequestContext(request)) 755 context_instance=RequestContext(request))
739 756