2008-06-23 13:08:07 +08:00
|
|
|
import datetime
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.conf import settings
|
2007-09-16 05:29:14 +08:00
|
|
|
from django.contrib.sessions.models import Session
|
2008-08-14 11:57:18 +08:00
|
|
|
from django.contrib.sessions.backends.base import SessionBase, CreateError
|
2007-09-16 05:29:14 +08:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2010-03-25 18:29:06 +08:00
|
|
|
from django.db import IntegrityError, transaction, router
|
2008-08-24 06:59:04 +08:00
|
|
|
from django.utils.encoding import force_unicode
|
2008-06-23 13:08:07 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
class SessionStore(SessionBase):
|
|
|
|
"""
|
2008-06-23 13:08:07 +08:00
|
|
|
Implements database session store.
|
2007-09-16 05:29:14 +08:00
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, session_key=None):
|
|
|
|
super(SessionStore, self).__init__(session_key)
|
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def load(self):
|
|
|
|
try:
|
|
|
|
s = Session.objects.get(
|
2008-01-06 20:53:09 +08:00
|
|
|
session_key = self.session_key,
|
2007-09-16 05:29:14 +08:00
|
|
|
expire_date__gt=datetime.datetime.now()
|
|
|
|
)
|
2008-08-24 06:59:04 +08:00
|
|
|
return self.decode(force_unicode(s.session_data))
|
2007-09-16 05:29:14 +08:00
|
|
|
except (Session.DoesNotExist, SuspiciousOperation):
|
2008-08-14 11:57:18 +08:00
|
|
|
self.create()
|
2007-09-16 05:29:14 +08:00
|
|
|
return {}
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2007-09-16 05:29:14 +08:00
|
|
|
def exists(self, session_key):
|
|
|
|
try:
|
|
|
|
Session.objects.get(session_key=session_key)
|
|
|
|
except Session.DoesNotExist:
|
|
|
|
return False
|
|
|
|
return True
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2008-08-14 11:57:18 +08:00
|
|
|
def create(self):
|
|
|
|
while True:
|
|
|
|
self.session_key = self._get_new_session_key()
|
|
|
|
try:
|
|
|
|
# Save immediately to ensure we have a unique entry in the
|
|
|
|
# database.
|
|
|
|
self.save(must_create=True)
|
|
|
|
except CreateError:
|
|
|
|
# Key wasn't unique. Try again.
|
|
|
|
continue
|
|
|
|
self.modified = True
|
|
|
|
self._session_cache = {}
|
|
|
|
return
|
|
|
|
|
|
|
|
def save(self, must_create=False):
|
|
|
|
"""
|
|
|
|
Saves the current session data to the database. If 'must_create' is
|
|
|
|
True, a database error will be raised if the saving operation doesn't
|
|
|
|
create a *new* entry (as opposed to possibly updating an existing
|
|
|
|
entry).
|
|
|
|
"""
|
|
|
|
obj = Session(
|
2007-09-16 05:29:14 +08:00
|
|
|
session_key = self.session_key,
|
2008-08-15 03:43:08 +08:00
|
|
|
session_data = self.encode(self._get_session(no_load=must_create)),
|
2008-06-08 04:28:06 +08:00
|
|
|
expire_date = self.get_expiry_date()
|
2007-09-16 05:29:14 +08:00
|
|
|
)
|
2010-03-25 18:29:06 +08:00
|
|
|
using = router.db_for_write(Session, instance=obj)
|
|
|
|
sid = transaction.savepoint(using=using)
|
2008-08-14 11:57:18 +08:00
|
|
|
try:
|
2010-03-25 18:29:06 +08:00
|
|
|
obj.save(force_insert=must_create, using=using)
|
2008-08-14 11:57:18 +08:00
|
|
|
except IntegrityError:
|
|
|
|
if must_create:
|
2010-03-25 18:29:06 +08:00
|
|
|
transaction.savepoint_rollback(sid, using=using)
|
2008-08-14 11:57:18 +08:00
|
|
|
raise CreateError
|
|
|
|
raise
|
2008-01-06 20:53:09 +08:00
|
|
|
|
2008-08-14 11:57:46 +08:00
|
|
|
def delete(self, session_key=None):
|
|
|
|
if session_key is None:
|
2008-08-15 22:59:11 +08:00
|
|
|
if self._session_key is None:
|
|
|
|
return
|
2008-08-14 11:57:46 +08:00
|
|
|
session_key = self._session_key
|
2007-09-16 05:29:14 +08:00
|
|
|
try:
|
|
|
|
Session.objects.get(session_key=session_key).delete()
|
|
|
|
except Session.DoesNotExist:
|
2008-01-06 20:53:09 +08:00
|
|
|
pass
|