# HG changeset patch # User Brian Neal # Date 1255292827 0 # Node ID 535d02d1c017841c15e6b818e306a155188ccec2 # Parent d97ceb95ce02056f8be0a843b2bc6ddfad1e399e Forums: Implemented unread status for topics and posts. diff -r d97ceb95ce02 -r 535d02d1c017 gpp/forums/unread.py --- a/gpp/forums/unread.py Sun Oct 11 19:10:54 2009 +0000 +++ b/gpp/forums/unread.py Sun Oct 11 20:27:07 2009 +0000 @@ -10,6 +10,7 @@ THRESHOLD = datetime.timedelta(days=7) +####################################################################### def get_forum_unread_status(qs, user): if not user.is_authenticated(): @@ -97,3 +98,90 @@ # we are all caught up. forum.catchup(user, flv) forum.has_unread = False + +####################################################################### + +def get_topic_unread_status(forum, topics, user): + + # Edge case: no topics + if forum.last_post is None: + return + + # This service isn't provided to unauthenticated users + if not user.is_authenticated(): + for topic in topics: + topic.has_unread = False + return + + now = datetime.datetime.now() + + # Get the ForumLastVisit record + try: + flv = ForumLastVisit.objects.get(forum=forum, user=user) + except ForumLastVisit.DoesNotExist: + # One doesn't exist, create a default one for next time, + # mark it as having no unread topics, and bail. + flv = ForumLastVisit(user=user, forum=forum) + flv.begin_date = now + flv.end_date = now + flv.save() + for topic in topics: + topic.has_unread = False + return + + # Are all the posts before our window? If so, all have been read. + if forum.last_post.creation_date < flv.begin_date: + for topic in topics: + topic.has_unread = False + return + + topic_ids = [topic.id for topic in topics] + tlvs = TopicLastVisit.objects.filter(user=user, topic__id__in=topic_ids) + tlvs = dict([(tlv.topic.id, tlv) for tlv in tlvs]) + + # Otherwise we have to go through the topics one by one: + for topic in topics: + if topic.update_date < flv.begin_date: + topic.has_unread = False + elif topic.update_date > flv.end_date: + topic.has_unread = True + elif topic.id in tlvs: + topic.has_unread = topic.update_date > tlvs[topic.id].last_visit + else: + topic.has_unread = True + +####################################################################### + +def get_post_unread_status(topic, posts, user): + # This service isn't provided to unauthenticated users + if not user.is_authenticated(): + for post in posts: + post.unread = False + return + + # Get the ForumLastVisit record + try: + flv = ForumLastVisit.objects.get(forum=topic.forum, user=user) + except ForumLastVisit.DoesNotExist: + # One doesn't exist, all posts are old. + for post in posts: + post.unread = False + return + + # Are all the posts before our window? If so, all have been read. + if topic.last_post.creation_date < flv.begin_date: + for post in posts: + post.unread = False + return + + # Do we have a topic last visit record for this topic? + + try: + tlv = TopicLastVisit.objects.get(user=user, topic=topic) + except TopicLastVisit.DoesNotExist: + # No we don't, we could be all caught up, or all are new + for post in posts: + post.unread = post.creation_date > flv.end_date + else: + for post in posts: + post.unread = post.creation_date > tlv.last_visit diff -r d97ceb95ce02 -r 535d02d1c017 gpp/forums/views.py --- a/gpp/forums/views.py Sun Oct 11 19:10:54 2009 +0000 +++ b/gpp/forums/views.py Sun Oct 11 20:27:07 2009 +0000 @@ -23,7 +23,8 @@ from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \ ForumLastVisit from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm -from forums.unread import get_forum_unread_status +from forums.unread import get_forum_unread_status, get_topic_unread_status, \ + get_post_unread_status ####################################################################### @@ -71,6 +72,8 @@ return HttpResponseForbidden() topics = forum.topics.select_related('user', 'last_post', 'last_post__user') + get_topic_unread_status(forum, topics, request.user) + paginator = create_topic_paginator(topics) page_num = int(request.GET.get('page', 1)) try: @@ -105,6 +108,8 @@ topic.save() posts = topic.posts.select_related() + get_post_unread_status(topic, posts, request.user) + paginator = create_post_paginator(posts) page_num = int(request.GET.get('page', 1)) try: @@ -195,6 +200,7 @@ return HttpResponseForbidden("You don't have permission to post in this topic.") post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) + post.unread = True _bump_post_count(request.user) _update_last_visit(request.user, form.topic) return render_to_response('forums/display_post.html', { diff -r d97ceb95ce02 -r 535d02d1c017 gpp/templates/forums/display_post.html --- a/gpp/templates/forums/display_post.html Sun Oct 11 19:10:54 2009 +0000 +++ b/gpp/templates/forums/display_post.html Sun Oct 11 20:27:07 2009 +0000 @@ -11,6 +11,7 @@
+ {% if post.unread %}New{% endif %} Link Posted on {{ post.creation_date|date:"M d, Y H:i" }} {% if can_moderate %}from IP: {{ post.user_ip }}{% endif %} diff -r d97ceb95ce02 -r 535d02d1c017 gpp/templates/forums/forum_index.html --- a/gpp/templates/forums/forum_index.html Sun Oct 11 19:10:54 2009 +0000 +++ b/gpp/templates/forums/forum_index.html Sun Oct 11 20:27:07 2009 +0000 @@ -30,7 +30,9 @@ {% for topic in page.object_list %} - {% if topic.sticky %}Sticky{% endif %} + + {% if topic.has_unread %}New Posts{% endif %} + {% if topic.sticky %}Sticky{% endif %} {% if topic.locked %}Locked{% endif %}

{{ topic.name }}