annotate gpp/forums/unread.py @ 153:13d052fbe4f1

Fixing #28, cosmetic issues with member map. Also fixed a bug involving the smiley and markdown interactions. Single quotes were getting HTML REMOVED by markdown.
author Brian Neal <bgneal@gmail.com>
date Sat, 19 Dec 2009 19:34:16 +0000
parents 35a0e6345815
children cf9f9d4c4d54
rev   line source
bgneal@113 1 """
bgneal@113 2 This file contains routines for implementing the "has unread" feature.
bgneal@113 3 Forums, topics, and posts are displayed with a visual indication if they have
bgneal@113 4 been read or not.
bgneal@113 5 """
bgneal@113 6 import datetime
bgneal@113 7
bgneal@113 8 from forums.models import ForumLastVisit, TopicLastVisit, Topic
bgneal@113 9
bgneal@113 10
bgneal@113 11 THRESHOLD = datetime.timedelta(days=7)
bgneal@113 12
bgneal@114 13 #######################################################################
bgneal@113 14
bgneal@113 15 def get_forum_unread_status(qs, user):
bgneal@113 16 if not user.is_authenticated():
bgneal@113 17 for forum in qs:
bgneal@113 18 forum.has_unread = False
bgneal@113 19 return
bgneal@113 20
bgneal@113 21 now = datetime.datetime.now()
bgneal@113 22 min_date = now - THRESHOLD
bgneal@113 23
bgneal@113 24 # retrieve ForumLastVisit records in one SQL query
bgneal@113 25 forum_ids = [forum.id for forum in qs]
bgneal@113 26 flvs = ForumLastVisit.objects.filter(user=user,
bgneal@113 27 forum__in=forum_ids).select_related()
bgneal@113 28 flvs = dict([(flv.forum.id, flv) for flv in flvs])
bgneal@113 29
bgneal@113 30 for forum in qs:
bgneal@113 31 # Edge case: forum has no posts
bgneal@113 32 if forum.last_post is None:
bgneal@113 33 forum.has_unread = False
bgneal@113 34 continue
bgneal@113 35
bgneal@113 36 # Get the ForumLastVisit record
bgneal@113 37 if forum.id in flvs:
bgneal@113 38 flv = flvs[forum.id]
bgneal@113 39 else:
bgneal@113 40 # One doesn't exist, create a default one for next time,
bgneal@113 41 # mark it as having no unread topics, and bail.
bgneal@113 42 flv = ForumLastVisit(user=user, forum=forum)
bgneal@113 43 flv.begin_date = now
bgneal@113 44 flv.end_date = now
bgneal@113 45 flv.save()
bgneal@113 46 forum.has_unread = False
bgneal@113 47 continue
bgneal@113 48
bgneal@113 49 # If the last visit record was too far in the past,
bgneal@113 50 # catch that user up and mark as no unreads.
bgneal@113 51 if now - flv.end_date > THRESHOLD:
bgneal@113 52 forum.catchup(user, flv)
bgneal@113 53 forum.has_unread = False
bgneal@113 54 continue
bgneal@113 55
bgneal@113 56 # Check the easy cases first. Check the last_post in the
bgneal@113 57 # forum. If created after the end_date in our window, there
bgneal@113 58 # are new posts. Likewise, if before the begin_date in our window,
bgneal@113 59 # there are no new posts.
bgneal@113 60 if forum.last_post.creation_date > flv.end_date:
bgneal@113 61 forum.has_unread = True
bgneal@113 62 elif forum.last_post.creation_date < flv.begin_date:
bgneal@113 63 if not flv.is_caught_up():
bgneal@113 64 forum.catchup(user, flv)
bgneal@113 65 forum.has_unread = False
bgneal@113 66 else:
bgneal@113 67 # Going to have to examine the topics in our window.
bgneal@113 68 # First adjust our window if it is too old.
bgneal@113 69 if now - flv.begin_date > THRESHOLD:
bgneal@113 70 flv.begin_date = min_date
bgneal@113 71 flv.save()
bgneal@113 72 TopicLastVisit.objects.filter(user=user, topic__forum=forum,
bgneal@113 73 last_visit__lt=min_date).delete()
bgneal@113 74
bgneal@148 75 topics = Topic.objects.filter(forum=forum,
bgneal@148 76 creation_date__gt=flv.begin_date)
bgneal@113 77 tracked_topics = TopicLastVisit.objects.filter(user=user,
bgneal@113 78 topic__forum=forum, last_visit__gt=flv.begin_date)
bgneal@113 79
bgneal@113 80 # If the number of topics created since our window was started
bgneal@113 81 # is greater than the tracked topic records, then there are new
bgneal@113 82 # posts.
bgneal@113 83 if topics.count() > tracked_topics.count():
bgneal@113 84 forum.has_unread = True
bgneal@113 85 continue
bgneal@113 86
bgneal@113 87 tracked_dict = dict([(t.id, t) for t in tracked_topics])
bgneal@113 88
bgneal@113 89 for topic in topics:
bgneal@113 90 if topic.id in tracked_dict:
bgneal@113 91 if topic.update_date > tracked_dict[topic.id].last_visit:
bgneal@113 92 forum.has_unread = True
bgneal@113 93 continue
bgneal@113 94 else:
bgneal@113 95 forum.has_unread = True
bgneal@113 96 continue
bgneal@113 97
bgneal@113 98 # If we made it through the above loop without continuing, then
bgneal@113 99 # we are all caught up.
bgneal@113 100 forum.catchup(user, flv)
bgneal@113 101 forum.has_unread = False
bgneal@114 102
bgneal@114 103 #######################################################################
bgneal@114 104
bgneal@114 105 def get_topic_unread_status(forum, topics, user):
bgneal@114 106
bgneal@114 107 # Edge case: no topics
bgneal@114 108 if forum.last_post is None:
bgneal@114 109 return
bgneal@114 110
bgneal@114 111 # This service isn't provided to unauthenticated users
bgneal@114 112 if not user.is_authenticated():
bgneal@114 113 for topic in topics:
bgneal@114 114 topic.has_unread = False
bgneal@114 115 return
bgneal@114 116
bgneal@114 117 now = datetime.datetime.now()
bgneal@114 118
bgneal@114 119 # Get the ForumLastVisit record
bgneal@114 120 try:
bgneal@114 121 flv = ForumLastVisit.objects.get(forum=forum, user=user)
bgneal@114 122 except ForumLastVisit.DoesNotExist:
bgneal@114 123 # One doesn't exist, create a default one for next time,
bgneal@114 124 # mark it as having no unread topics, and bail.
bgneal@114 125 flv = ForumLastVisit(user=user, forum=forum)
bgneal@114 126 flv.begin_date = now
bgneal@114 127 flv.end_date = now
bgneal@114 128 flv.save()
bgneal@114 129 for topic in topics:
bgneal@114 130 topic.has_unread = False
bgneal@114 131 return
bgneal@114 132
bgneal@114 133 # Are all the posts before our window? If so, all have been read.
bgneal@114 134 if forum.last_post.creation_date < flv.begin_date:
bgneal@114 135 for topic in topics:
bgneal@114 136 topic.has_unread = False
bgneal@114 137 return
bgneal@114 138
bgneal@114 139 topic_ids = [topic.id for topic in topics]
bgneal@114 140 tlvs = TopicLastVisit.objects.filter(user=user, topic__id__in=topic_ids)
bgneal@114 141 tlvs = dict([(tlv.topic.id, tlv) for tlv in tlvs])
bgneal@114 142
bgneal@114 143 # Otherwise we have to go through the topics one by one:
bgneal@114 144 for topic in topics:
bgneal@114 145 if topic.update_date < flv.begin_date:
bgneal@114 146 topic.has_unread = False
bgneal@114 147 elif topic.update_date > flv.end_date:
bgneal@114 148 topic.has_unread = True
bgneal@114 149 elif topic.id in tlvs:
bgneal@114 150 topic.has_unread = topic.update_date > tlvs[topic.id].last_visit
bgneal@114 151 else:
bgneal@114 152 topic.has_unread = True
bgneal@114 153
bgneal@114 154 #######################################################################
bgneal@114 155
bgneal@114 156 def get_post_unread_status(topic, posts, user):
bgneal@114 157 # This service isn't provided to unauthenticated users
bgneal@114 158 if not user.is_authenticated():
bgneal@114 159 for post in posts:
bgneal@114 160 post.unread = False
bgneal@114 161 return
bgneal@114 162
bgneal@114 163 # Get the ForumLastVisit record
bgneal@114 164 try:
bgneal@114 165 flv = ForumLastVisit.objects.get(forum=topic.forum, user=user)
bgneal@114 166 except ForumLastVisit.DoesNotExist:
bgneal@114 167 # One doesn't exist, all posts are old.
bgneal@114 168 for post in posts:
bgneal@114 169 post.unread = False
bgneal@114 170 return
bgneal@114 171
bgneal@114 172 # Are all the posts before our window? If so, all have been read.
bgneal@114 173 if topic.last_post.creation_date < flv.begin_date:
bgneal@114 174 for post in posts:
bgneal@114 175 post.unread = False
bgneal@114 176 return
bgneal@114 177
bgneal@114 178 # Do we have a topic last visit record for this topic?
bgneal@114 179
bgneal@114 180 try:
bgneal@114 181 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
bgneal@114 182 except TopicLastVisit.DoesNotExist:
bgneal@114 183 # No we don't, we could be all caught up, or all are new
bgneal@114 184 for post in posts:
bgneal@114 185 post.unread = post.creation_date > flv.end_date
bgneal@114 186 else:
bgneal@114 187 for post in posts:
bgneal@114 188 post.unread = post.creation_date > tlv.last_visit