changeset 374:dd673fae508d

Fixing #156. Making it possible to goto the first unread post in a thread. Made the 'New' icon a link; when displayed on a topic, takes you to the first unread post.
author Brian Neal <bgneal@gmail.com>
date Mon, 07 Mar 2011 01:22:49 +0000 (2011-03-07)
parents 54cbdef18cf4
children fe1896e14c11
files gpp/forums/urls.py gpp/forums/views/main.py gpp/templates/forums/index.html gpp/templates/forums/topic_icons_tag.html gpp/templates/forums/topic_list.html
diffstat 5 files changed, 47 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/forums/urls.py	Sun Mar 06 21:50:10 2011 +0000
+++ b/gpp/forums/urls.py	Mon Mar 07 01:22:49 2011 +0000
@@ -7,6 +7,7 @@
     url(r'^$', 'index', name='forums-index'),
     url(r'^new-topic-success/(?P<tid>\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'),
     url(r'^topic/(?P<id>\d+)/$', 'topic_index', name='forums-topic_index'),
+    url(r'^topic/(?P<id>\d+)/unread/$', 'topic_unread', name='forums-topic_unread'),
     url(r'^delete-post/$', 'delete_post', name='forums-delete_post'),
     url(r'^edit/(?P<id>\d+)/$', 'edit_post', name='forums-edit_post'),
     url(r'^flag-post/$', 'flag_post', name='forums-flag_post'),
--- a/gpp/forums/views/main.py	Sun Mar 06 21:50:10 2011 +0000
+++ b/gpp/forums/views/main.py	Mon Mar 07 01:22:49 2011 +0000
@@ -242,6 +242,32 @@
         context_instance=RequestContext(request))
 
 
+def topic_unread(request, id):
+    """
+    This view redirects to the first post the user hasn't read, if we can
+    figure that out. Otherwise we redirect to the topic.
+
+    """
+    topic_url = reverse('forums-topic_index', kwargs={'id': id})
+
+    if request.user.is_authenticated():
+        try:
+            tlv = TopicLastVisit.objects.get(user=request.user, topic=id)
+        except TopicLastVisit.DoesNotExist:
+            return HttpResponseRedirect(topic_url)
+
+        posts = Post.objects.filter(topic=id, creation_date__gt=tlv.last_visit)
+        if posts:
+            return _goto_post(posts[0])
+        else:
+            # just go to the last post in the topic
+            topic = get_object_or_404(Topic.objects.select_related('last_post'), pk=id)
+            return _goto_post(topic.last_post)
+
+    # user isn't authenticated, just go to the topic
+    return HttpResponseRedirect(topic_url)
+
+
 @login_required
 def new_topic(request, slug):
     """
@@ -320,6 +346,19 @@
     return HttpResponseBadRequest("Oops, did you forget some text?");
 
 
+def _goto_post(post):
+    """
+    Calculate what page the given post is on in its parent topic, then
+    return a redirect to it.
+
+    """
+    count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
+    page = count / POSTS_PER_PAGE + 1
+    url = (reverse('forums-topic_index', kwargs={'id': post.topic.id}) +
+        '?page=%s#p%s' % (page, post.id))
+    return HttpResponseRedirect(url)
+
+
 def goto_post(request, post_id):
     """
     This function calculates what page a given post is on, then redirects
@@ -327,11 +366,7 @@
     Post objects.
     """
     post = get_object_or_404(Post.objects.select_related(), pk=post_id)
-    count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
-    page = count / POSTS_PER_PAGE + 1
-    url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
-        '?page=%s#p%s' % (page, post.id)
-    return HttpResponseRedirect(url)
+    return _goto_post(post)
 
 
 @require_POST
@@ -768,6 +803,7 @@
         'title': 'Topics With Unread Posts',
         'page': page,
         'page_nav': page_nav,
+        'unread': True,
         },
         context_instance=RequestContext(request))
 
@@ -796,6 +832,7 @@
         'title': 'Unanswered Topics',
         'page': page,
         'page_nav': page_nav,
+        'unread': False,
         },
         context_instance=RequestContext(request))
 
--- a/gpp/templates/forums/index.html	Sun Mar 06 21:50:10 2011 +0000
+++ b/gpp/templates/forums/index.html	Mon Mar 07 01:22:49 2011 +0000
@@ -31,7 +31,7 @@
             <tr class="{% cycle 'odd' 'even' %}">
                <td>
                   {% if forum.has_unread %}
-                  <img src="{{ STATIC_URL }}icons/new.png" alt="New Posts" title="New Posts" class="forums-topic-icon" />
+                  <a href="{{ forum.get_absolute_url }}"><img src="{{ STATIC_URL }}icons/new.png" alt="New Posts" title="New Posts" class="forums-topic-icon" /></a>
                   {% endif %}
                   <h4><a href="{{ forum.get_absolute_url }}">{{ forum.name }}</a></h4>
                   <p>{{ forum.description }}</p>
--- a/gpp/templates/forums/topic_icons_tag.html	Sun Mar 06 21:50:10 2011 +0000
+++ b/gpp/templates/forums/topic_icons_tag.html	Mon Mar 07 01:22:49 2011 +0000
@@ -1,4 +1,5 @@
-{% if topic.has_unread %}<img src="{{ STATIC_URL }}icons/new.png" alt="New Posts" title="New Posts" class="forums-topic-icon" />{% endif %}
+{% load url from future %}
+{% if topic.has_unread %}<a href="{% url 'forums-topic_unread' id=topic.id %}"><img src="{{ STATIC_URL }}icons/new.png" alt="New Posts" title="New Posts" class="forums-topic-icon" /></a>{% endif %}
 {% if topic.sticky %}<img src="{{ STATIC_URL }}icons/asterisk_orange.png" alt="Sticky" title="Sticky" class="forums-topic-icon" />{% endif %}
 {% if topic.locked %}<img src="{{ STATIC_URL }}icons/lock.png" alt="Locked" title="Locked"
 class="forums-topic-icon" />{% endif %}
--- a/gpp/templates/forums/topic_list.html	Sun Mar 06 21:50:10 2011 +0000
+++ b/gpp/templates/forums/topic_list.html	Mon Mar 07 01:22:49 2011 +0000
@@ -27,7 +27,7 @@
          </td>
          <td class="col-1">
             {% topic_icons topic %}
-            <h4><a href="{{ topic.get_absolute_url }}">{{ topic.name }}</a></h4>
+            <h4>{% if unread %}<a href="{% url 'forums-topic_unread' id=topic.id %}">{% else %}<a href="{{ topic.get_absolute_url }}">{% endif %}{{ topic.name }}</a></h4>
             {% if topic.page_range %}
                {% topic_page_range topic %}
             {% endif %}