comparison gpp/forums/unread.py @ 370:e9a066db3f54

Adding some try/except around some key points in the code to protect against some IntegrityErrors. We log when these happens but otherwise keep going. Tickets: #160 and #169.
author Brian Neal <bgneal@gmail.com>
date Sun, 06 Mar 2011 00:40:50 +0000
parents 7e19180b128d
children 42a4e66972d5
comparison
equal deleted inserted replaced
369:1688873085e0 370:e9a066db3f54
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 import logging
8
9 from django.db import IntegrityError
8 from django.db.models import Q 10 from django.db.models import Q
9 11
10 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum 12 from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum
11 13
12 14
42 # One doesn't exist, create a default one for next time, 44 # One doesn't exist, create a default one for next time,
43 # mark it as having no unread topics, and bail. 45 # mark it as having no unread topics, and bail.
44 flv = ForumLastVisit(user=user, forum=forum) 46 flv = ForumLastVisit(user=user, forum=forum)
45 flv.begin_date = now 47 flv.begin_date = now
46 flv.end_date = now 48 flv.end_date = now
47 flv.save() 49
50 # There is a race condition and sometimes another thread
51 # saves a record before we do; just log this if it happens.
52 try:
53 flv.save()
54 except IntegrityError:
55 logging.exception('get_forum_unread_status')
56
48 forum.has_unread = False 57 forum.has_unread = False
49 continue 58 continue
50 59
51 # If the last visit record was too far in the past, 60 # If the last visit record was too far in the past,
52 # catch that user up and mark as no unreads. 61 # catch that user up and mark as no unreads.
125 # One doesn't exist, create a default one for next time, 134 # One doesn't exist, create a default one for next time,
126 # mark it as having no unread topics, and bail. 135 # mark it as having no unread topics, and bail.
127 flv = ForumLastVisit(user=user, forum=forum) 136 flv = ForumLastVisit(user=user, forum=forum)
128 flv.begin_date = now 137 flv.begin_date = now
129 flv.end_date = now 138 flv.end_date = now
130 flv.save() 139
140 # There is a race condition and sometimes another thread
141 # saves a record before we do; just log this if it happens.
142 try:
143 flv.save()
144 except IntegrityError:
145 logging.exception('get_topic_unread_status')
146
131 for topic in topics: 147 for topic in topics:
132 topic.has_unread = False 148 topic.has_unread = False
133 return 149 return
134 150
135 # Are all the posts before our window? If so, all have been read. 151 # Are all the posts before our window? If so, all have been read.