comparison gpp/forums/unread.py @ 408:7e0997b08b50

Fixes for #200; fix bug in deciding which forums have unread posts.
author Brian Neal <bgneal@gmail.com>
date Sat, 02 Apr 2011 00:12:40 +0000
parents 42a4e66972d5
children c06d836c8b84
comparison
equal deleted inserted replaced
407:42a4e66972d5 408:7e0997b08b50
5 """ 5 """
6 import datetime 6 import datetime
7 import logging 7 import logging
8 8
9 from django.db import IntegrityError 9 from django.db import IntegrityError
10 from django.db.models import Q
11 10
12 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum 11 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum
13 12
14 13
15 THRESHOLD = datetime.timedelta(days=14) 14 THRESHOLD = datetime.timedelta(days=14)
83 TopicLastVisit.objects.filter(user=user, topic__forum=forum, 82 TopicLastVisit.objects.filter(user=user, topic__forum=forum,
84 last_visit__lt=min_date).delete() 83 last_visit__lt=min_date).delete()
85 84
86 topics = Topic.objects.filter(forum=forum, 85 topics = Topic.objects.filter(forum=forum,
87 creation_date__gt=flv.begin_date) 86 creation_date__gt=flv.begin_date)
88 tracked_topics = TopicLastVisit.objects.filter(user=user, 87 tracked_topics = TopicLastVisit.objects.filter(
89 topic__forum=forum, last_visit__gt=flv.begin_date) 88 user=user,
89 topic__forum=forum,
90 last_visit__gt=flv.begin_date).select_related('topic')
90 91
91 # If the number of topics created since our window was started 92 # If the number of topics created since our window was started
92 # is greater than the tracked topic records, then there are new 93 # is greater than the tracked topic records, then there are new
93 # posts. 94 # posts.
94 if topics.count() > tracked_topics.count(): 95 if topics.count() > tracked_topics.count():
95 forum.has_unread = True 96 forum.has_unread = True
96 continue 97 continue
97 98
98 tracked_dict = dict([(t.id, t) for t in tracked_topics]) 99 tracked_dict = dict((t.topic.id, t) for t in tracked_topics)
99 100
100 for topic in topics: 101 for topic in topics:
101 if topic.id in tracked_dict: 102 if topic.id in tracked_dict:
102 if topic.update_date > tracked_dict[topic.id].last_visit: 103 if topic.update_date > tracked_dict[topic.id].last_visit:
103 forum.has_unread = True 104 forum.has_unread = True