Fixes an issue where the organization home page would throw a 505 when no projects...
[Melange.git] / app / django / contrib / sessions / backends / db.py
blob85c4a0dd3c8fe20d4bb32ba64a25dc99ce2fb892
1 import datetime
2 from django.contrib.sessions.models import Session
3 from django.contrib.sessions.backends.base import SessionBase, CreateError
4 from django.core.exceptions import SuspiciousOperation
5 from django.db import IntegrityError, transaction
6 from django.utils.encoding import force_unicode
8 class SessionStore(SessionBase):
9 """
10 Implements database session store.
11 """
12 def load(self):
13 try:
14 s = Session.objects.get(
15 session_key = self.session_key,
16 expire_date__gt=datetime.datetime.now()
18 return self.decode(force_unicode(s.session_data))
19 except (Session.DoesNotExist, SuspiciousOperation):
20 self.create()
21 return {}
23 def exists(self, session_key):
24 try:
25 Session.objects.get(session_key=session_key)
26 except Session.DoesNotExist:
27 return False
28 return True
30 def create(self):
31 while True:
32 self.session_key = self._get_new_session_key()
33 try:
34 # Save immediately to ensure we have a unique entry in the
35 # database.
36 self.save(must_create=True)
37 except CreateError:
38 # Key wasn't unique. Try again.
39 continue
40 self.modified = True
41 self._session_cache = {}
42 return
44 def save(self, must_create=False):
45 """
46 Saves the current session data to the database. If 'must_create' is
47 True, a database error will be raised if the saving operation doesn't
48 create a *new* entry (as opposed to possibly updating an existing
49 entry).
50 """
51 obj = Session(
52 session_key = self.session_key,
53 session_data = self.encode(self._get_session(no_load=must_create)),
54 expire_date = self.get_expiry_date()
56 sid = transaction.savepoint()
57 try:
58 obj.save(force_insert=must_create)
59 except IntegrityError:
60 if must_create:
61 transaction.savepoint_rollback(sid)
62 raise CreateError
63 raise
65 def delete(self, session_key=None):
66 if session_key is None:
67 if self._session_key is None:
68 return
69 session_key = self._session_key
70 try:
71 Session.objects.get(session_key=session_key).delete()
72 except Session.DoesNotExist:
73 pass