Mercurial > public > sg101
comparison gpp/forums/unread.py @ 167:cf9f9d4c4d54
Adding a query to the forums to get all the topics with unread posts. This is for ticket #54.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 24 Jan 2010 22:33:11 +0000 |
parents | 35a0e6345815 |
children | e6d4dfdfbc64 |
comparison
equal
deleted
inserted
replaced
166:8acf5be27f18 | 167:cf9f9d4c4d54 |
---|---|
3 Forums, topics, and posts are displayed with a visual indication if they have | 3 Forums, topics, and posts are displayed with a visual indication if they have |
4 been read or not. | 4 been read or not. |
5 """ | 5 """ |
6 import datetime | 6 import datetime |
7 | 7 |
8 from forums.models import ForumLastVisit, TopicLastVisit, Topic | 8 from django.db.models import Q |
9 | |
10 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum | |
9 | 11 |
10 | 12 |
11 THRESHOLD = datetime.timedelta(days=7) | 13 THRESHOLD = datetime.timedelta(days=7) |
12 | 14 |
13 ####################################################################### | 15 ####################################################################### |
184 for post in posts: | 186 for post in posts: |
185 post.unread = post.creation_date > flv.end_date | 187 post.unread = post.creation_date > flv.end_date |
186 else: | 188 else: |
187 for post in posts: | 189 for post in posts: |
188 post.unread = post.creation_date > tlv.last_visit | 190 post.unread = post.creation_date > tlv.last_visit |
191 | |
192 ####################################################################### | |
193 | |
194 def get_unread_topics(user): | |
195 """Returns a list of topics the user hasn't read yet.""" | |
196 #import pdb; pdb.set_trace() | |
197 | |
198 # This is only available to authenticated users | |
199 if not user.is_authenticated(): | |
200 return [] | |
201 | |
202 now = datetime.datetime.now() | |
203 | |
204 # Obtain list of forums the user can view | |
205 forums = Forum.objects.forums_for_user(user) | |
206 | |
207 # Get forum last visit records for the forum ids | |
208 flvs = ForumLastVisit.objects.filter(user=user, | |
209 forum__in=forums).select_related() | |
210 flvs = dict([(flv.forum.id, flv) for flv in flvs]) | |
211 | |
212 unread_topics = [] | |
213 topics = Topic.objects.none() | |
214 for forum in forums: | |
215 # if the user hasn't visited the forum, create a last | |
216 # visit record set to "now" | |
217 if not forum.id in flvs: | |
218 flv = ForumLastVisit(user=user, forum=forum, begin_date=now, | |
219 end_date=now) | |
220 flv.save() | |
221 else: | |
222 flv = flvs[forum.id] | |
223 topics |= Topic.objects.filter(forum=forum, | |
224 update_date__gt=flv.begin_date).order_by('-update_date').select_related() | |
225 | |
226 if topics is not None: | |
227 # get all topic last visit records for the topics of interest | |
228 | |
229 tlvs = TopicLastVisit.objects.filter(user=user, topic__in=topics) | |
230 tlvs = dict([(tlv.topic.id, tlv) for tlv in tlvs]) | |
231 | |
232 for topic in topics: | |
233 if topic.id in tlvs: | |
234 tlv = tlvs[topic.id] | |
235 if topic.update_date > tlv.last_visit: | |
236 unread_topics.append(topic) | |
237 else: | |
238 unread_topics.append(topic) | |
239 | |
240 return unread_topics |