changeset 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 b1766b1bda45
children a88041a84957
files gpp/forums/urls.py gpp/forums/views.py gpp/templates/bio/view_profile.html
diffstat 3 files changed, 23 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/urls.py	Fri Feb 12 04:24:47 2010 +0000
+++ b/gpp/forums/urls.py	Sun Feb 28 22:20:15 2010 +0000
@@ -22,6 +22,7 @@
     url(r'^my-posts/$', 'my_posts', name='forums-my_posts'),
     url(r'^post/(\d+)/$', 'goto_post', name='forums-goto_post'),
     url(r'^post/new/(?P<topic_id>\d+)/$', 'new_post', name='forums-new_post'),
+    url(r'^posts/(?P<username>[\w\d-]+)/$', 'posts_for_user', name='forums-posts_for_user'),
     url(r'^quick-reply/$', 'quick_reply_ajax', name='forums-quick_reply'),
     url(r'^unanswered/$', 'unanswered_topics', name='forums-unanswered_topics'),
     url(r'^unread/$', 'unread_topics', name='forums-unread_topics'),
--- a/gpp/forums/views.py	Fri Feb 12 04:24:47 2010 +0000
+++ b/gpp/forums/views.py	Sun Feb 28 22:20:15 2010 +0000
@@ -4,6 +4,7 @@
 import datetime
 
 from django.contrib.auth.decorators import login_required
+from django.contrib.auth.models import User
 from django.http import Http404
 from django.http import HttpResponse
 from django.http import HttpResponseBadRequest
@@ -714,9 +715,25 @@
 @login_required
 def my_posts(request):
     """Displays a list of posts the requesting user made."""
+    return _user_posts(request, request.user, request.user, 'My Posts')
 
-    forum_ids = Forum.objects.forum_ids_for_user(request.user)
-    posts = Post.objects.filter(user=request.user,
+
+@login_required
+def posts_for_user(request, username):
+    """Displays a list of posts by the given user.
+    Only the forums that the requesting user can see are examined.
+    """
+    target_user = get_object_or_404(User, username=username)
+    return _user_posts(request, target_user, request.user, 'Posts by %s' % username)
+
+
+def _user_posts(request, target_user, req_user, page_title):
+    """Displays a list of posts made by the target user. 
+    req_user is the user trying to view the posts. Only the forums
+    req_user can see are searched.
+    """
+    forum_ids = Forum.objects.forum_ids_for_user(req_user)
+    posts = Post.objects.filter(user=target_user,
             topic__forum__id__in=forum_ids).order_by(
                     '-creation_date').select_related()
 
@@ -731,7 +748,7 @@
     page_nav = render_to_string('forums/pagination.html', {'page': page})
 
     return render_to_response('forums/post_list.html', {
-        'title': 'My Posts',
+        'title': page_title,
         'page': page,
         'page_nav': page_nav,
         },
--- a/gpp/templates/bio/view_profile.html	Fri Feb 12 04:24:47 2010 +0000
+++ b/gpp/templates/bio/view_profile.html	Sun Feb 28 22:20:15 2010 +0000
@@ -65,6 +65,8 @@
 {% if user.is_authenticated %}
 <ul class="icon-list">
    <li><a href="{% url messages-compose_to subject.username %}"><img src="{{ MEDIA_URL }}icons/note.png" alt="PM" title="Send Private Message" /></a> <a href="{% url messages-compose_to subject.username %}">Send a private message to {{ subject.username }}</a></li>
+   <li><a href="{% url forums-posts_for_user username=subject.username %}"><img src="{{ MEDIA_URL }}icons/comments.png"
+      alt="Forum Posts" title="View forum posts by {{ subject.username }}" /></a> <a href="{% url forums-posts_for_user username=subject.username %}">View forum posts by {{ subject.username }}</a></li>
    <li><img src="{{ MEDIA_URL }}icons/flag_red.png" alt="Flag" />
       <a href="#" class="profile-flag" id="fp-{{ profile.id }}">Report this profile</a></li>
 </ul>