comparison gpp/core/middleware.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 a2d388ed106e
children 3fe60148f75c
comparison
equal deleted inserted replaced
369:1688873085e0 370:e9a066db3f54
1 """Common middleware for the entire project.""" 1 """Common middleware for the entire project."""
2 import datetime 2 import datetime
3 import logging
3 4
5 from django.db import IntegrityError
4 from django.contrib.auth import logout 6 from django.contrib.auth import logout
5 from django.conf import settings 7 from django.conf import settings
6 8
7 from core.models import UserLastVisit 9 from core.models import UserLastVisit
8 from core.models import AnonLastVisit 10 from core.models import AnonLastVisit
71 alv = AnonLastVisit.objects.get(ip=ip) 73 alv = AnonLastVisit.objects.get(ip=ip)
72 except AnonLastVisit.DoesNotExist: 74 except AnonLastVisit.DoesNotExist:
73 alv = AnonLastVisit(ip=ip) 75 alv = AnonLastVisit(ip=ip)
74 76
75 alv.last_visit = datetime.datetime.now() 77 alv.last_visit = datetime.datetime.now()
76 alv.save() 78
79 # There is a race condition and sometimes another thread
80 # saves a record before we do; just log this if it happens.
81 try:
82 alv.save()
83 except IntegrityError:
84 logging.exception('WhosOnline.process_response')
77 85
78 # set a cookie to expire in 10 minutes or so 86 # set a cookie to expire in 10 minutes or so
79 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT) 87 response.set_cookie(ONLINE_COOKIE, '1', max_age=ONLINE_TIMEOUT)
80 88
81 return response 89 return response