Mercurial > public > sg101
comparison gpp/forums/unread.py @ 114:535d02d1c017
Forums: Implemented unread status for topics and posts.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 11 Oct 2009 20:27:07 +0000 |
parents | d97ceb95ce02 |
children | 35a0e6345815 |
comparison
equal
deleted
inserted
replaced
113:d97ceb95ce02 | 114:535d02d1c017 |
---|---|
8 from forums.models import ForumLastVisit, TopicLastVisit, Topic | 8 from forums.models import ForumLastVisit, TopicLastVisit, Topic |
9 | 9 |
10 | 10 |
11 THRESHOLD = datetime.timedelta(days=7) | 11 THRESHOLD = datetime.timedelta(days=7) |
12 | 12 |
13 ####################################################################### | |
13 | 14 |
14 def get_forum_unread_status(qs, user): | 15 def get_forum_unread_status(qs, user): |
15 if not user.is_authenticated(): | 16 if not user.is_authenticated(): |
16 for forum in qs: | 17 for forum in qs: |
17 forum.has_unread = False | 18 forum.has_unread = False |
95 | 96 |
96 # If we made it through the above loop without continuing, then | 97 # If we made it through the above loop without continuing, then |
97 # we are all caught up. | 98 # we are all caught up. |
98 forum.catchup(user, flv) | 99 forum.catchup(user, flv) |
99 forum.has_unread = False | 100 forum.has_unread = False |
101 | |
102 ####################################################################### | |
103 | |
104 def get_topic_unread_status(forum, topics, user): | |
105 | |
106 # Edge case: no topics | |
107 if forum.last_post is None: | |
108 return | |
109 | |
110 # This service isn't provided to unauthenticated users | |
111 if not user.is_authenticated(): | |
112 for topic in topics: | |
113 topic.has_unread = False | |
114 return | |
115 | |
116 now = datetime.datetime.now() | |
117 | |
118 # Get the ForumLastVisit record | |
119 try: | |
120 flv = ForumLastVisit.objects.get(forum=forum, user=user) | |
121 except ForumLastVisit.DoesNotExist: | |
122 # One doesn't exist, create a default one for next time, | |
123 # mark it as having no unread topics, and bail. | |
124 flv = ForumLastVisit(user=user, forum=forum) | |
125 flv.begin_date = now | |
126 flv.end_date = now | |
127 flv.save() | |
128 for topic in topics: | |
129 topic.has_unread = False | |
130 return | |
131 | |
132 # Are all the posts before our window? If so, all have been read. | |
133 if forum.last_post.creation_date < flv.begin_date: | |
134 for topic in topics: | |
135 topic.has_unread = False | |
136 return | |
137 | |
138 topic_ids = [topic.id for topic in topics] | |
139 tlvs = TopicLastVisit.objects.filter(user=user, topic__id__in=topic_ids) | |
140 tlvs = dict([(tlv.topic.id, tlv) for tlv in tlvs]) | |
141 | |
142 # Otherwise we have to go through the topics one by one: | |
143 for topic in topics: | |
144 if topic.update_date < flv.begin_date: | |
145 topic.has_unread = False | |
146 elif topic.update_date > flv.end_date: | |
147 topic.has_unread = True | |
148 elif topic.id in tlvs: | |
149 topic.has_unread = topic.update_date > tlvs[topic.id].last_visit | |
150 else: | |
151 topic.has_unread = True | |
152 | |
153 ####################################################################### | |
154 | |
155 def get_post_unread_status(topic, posts, user): | |
156 # This service isn't provided to unauthenticated users | |
157 if not user.is_authenticated(): | |
158 for post in posts: | |
159 post.unread = False | |
160 return | |
161 | |
162 # Get the ForumLastVisit record | |
163 try: | |
164 flv = ForumLastVisit.objects.get(forum=topic.forum, user=user) | |
165 except ForumLastVisit.DoesNotExist: | |
166 # One doesn't exist, all posts are old. | |
167 for post in posts: | |
168 post.unread = False | |
169 return | |
170 | |
171 # Are all the posts before our window? If so, all have been read. | |
172 if topic.last_post.creation_date < flv.begin_date: | |
173 for post in posts: | |
174 post.unread = False | |
175 return | |
176 | |
177 # Do we have a topic last visit record for this topic? | |
178 | |
179 try: | |
180 tlv = TopicLastVisit.objects.get(user=user, topic=topic) | |
181 except TopicLastVisit.DoesNotExist: | |
182 # No we don't, we could be all caught up, or all are new | |
183 for post in posts: | |
184 post.unread = post.creation_date > flv.end_date | |
185 else: | |
186 for post in posts: | |
187 post.unread = post.creation_date > tlv.last_visit |