comparison gpp/forums/unread.py @ 307:7e19180b128d

Fixing #97; adding a management command to remove old forum and topic last visit records.
author Brian Neal <bgneal@gmail.com>
date Sun, 16 Jan 2011 20:18:26 +0000
parents d77e0dc772ad
children e9a066db3f54
comparison
equal deleted inserted replaced
306:6ca2c474d92f 307:7e19180b128d
1 """ 1 """
2 This file contains routines for implementing the "has unread" feature. 2 This file contains routines for implementing the "has unread" feature.
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 django.db.models import Q 8 from django.db.models import Q
9 9
10 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum 10 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum
11 11
12 12
13 THRESHOLD = datetime.timedelta(days=7) 13 THRESHOLD = datetime.timedelta(days=14)
14 14
15 ####################################################################### 15 #######################################################################
16 16
17 def get_forum_unread_status(qs, user): 17 def get_forum_unread_status(qs, user):
18 if not user.is_authenticated(): 18 if not user.is_authenticated():
23 now = datetime.datetime.now() 23 now = datetime.datetime.now()
24 min_date = now - THRESHOLD 24 min_date = now - THRESHOLD
25 25
26 # retrieve ForumLastVisit records in one SQL query 26 # retrieve ForumLastVisit records in one SQL query
27 forum_ids = [forum.id for forum in qs] 27 forum_ids = [forum.id for forum in qs]
28 flvs = ForumLastVisit.objects.filter(user=user, 28 flvs = ForumLastVisit.objects.filter(user=user,
29 forum__in=forum_ids).select_related() 29 forum__in=forum_ids).select_related()
30 flvs = dict([(flv.forum.id, flv) for flv in flvs]) 30 flvs = dict([(flv.forum.id, flv) for flv in flvs])
31 31
32 for forum in qs: 32 for forum in qs:
33 # Edge case: forum has no posts 33 # Edge case: forum has no posts
72 flv.begin_date = min_date 72 flv.begin_date = min_date
73 flv.save() 73 flv.save()
74 TopicLastVisit.objects.filter(user=user, topic__forum=forum, 74 TopicLastVisit.objects.filter(user=user, topic__forum=forum,
75 last_visit__lt=min_date).delete() 75 last_visit__lt=min_date).delete()
76 76
77 topics = Topic.objects.filter(forum=forum, 77 topics = Topic.objects.filter(forum=forum,
78 creation_date__gt=flv.begin_date) 78 creation_date__gt=flv.begin_date)
79 tracked_topics = TopicLastVisit.objects.filter(user=user, 79 tracked_topics = TopicLastVisit.objects.filter(user=user,
80 topic__forum=forum, last_visit__gt=flv.begin_date) 80 topic__forum=forum, last_visit__gt=flv.begin_date)
81 81
82 # If the number of topics created since our window was started 82 # If the number of topics created since our window was started