# HG changeset patch # User Brian Neal # Date 1299460969 0 # Node ID dd673fae508d3473fd4589cfa7fc890ba985a49b # Parent 54cbdef18cf48ee9759ac5aebdc106b847e1b971 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. diff -r 54cbdef18cf4 -r dd673fae508d gpp/forums/urls.py --- 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\d+)$', 'new_topic_thanks', name='forums-new_topic_thanks'), url(r'^topic/(?P\d+)/$', 'topic_index', name='forums-topic_index'), + url(r'^topic/(?P\d+)/unread/$', 'topic_unread', name='forums-topic_unread'), url(r'^delete-post/$', 'delete_post', name='forums-delete_post'), url(r'^edit/(?P\d+)/$', 'edit_post', name='forums-edit_post'), url(r'^flag-post/$', 'flag_post', name='forums-flag_post'), diff -r 54cbdef18cf4 -r dd673fae508d gpp/forums/views/main.py --- 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)) diff -r 54cbdef18cf4 -r dd673fae508d gpp/templates/forums/index.html --- 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 @@ {% if forum.has_unread %} - New Posts + New Posts {% endif %}

{{ forum.name }}

{{ forum.description }}

diff -r 54cbdef18cf4 -r dd673fae508d gpp/templates/forums/topic_icons_tag.html --- 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 %}New Posts{% endif %} +{% load url from future %} +{% if topic.has_unread %}New Posts{% endif %} {% if topic.sticky %}Sticky{% endif %} {% if topic.locked %}Locked{% endif %} diff -r 54cbdef18cf4 -r dd673fae508d gpp/templates/forums/topic_list.html --- 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 @@ {% topic_icons topic %} -

{{ topic.name }}

+

{% if unread %}{% else %}{% endif %}{{ topic.name }}

{% if topic.page_range %} {% topic_page_range topic %} {% endif %}