This commit is contained in:
Ross Lawley 2012-12-10 08:25:37 +00:00
commit 22d1ce6319
2 changed files with 21 additions and 4 deletions

View File

@ -124,3 +124,4 @@ that much better:
* Stefan Wójcik * Stefan Wójcik
* dimonb * dimonb
* Garry Polley * Garry Polley
* Adrian Scott

View File

@ -15,13 +15,23 @@ MONGOENGINE_SESSION_DB_ALIAS = getattr(
settings, 'MONGOENGINE_SESSION_DB_ALIAS', settings, 'MONGOENGINE_SESSION_DB_ALIAS',
DEFAULT_CONNECTION_NAME) DEFAULT_CONNECTION_NAME)
# a setting for the name of the collection used to store sessions
MONGOENGINE_SESSION_COLLECTION = getattr(
settings, 'MONGOENGINE_SESSION_COLLECTION',
'django_session')
# a setting for whether session data is stored encoded or not
MONGOENGINE_SESSION_DATA_ENCODE = getattr(
settings, 'MONGOENGINE_SESSION_DATA_ENCODE',
True)
class MongoSession(Document): class MongoSession(Document):
session_key = fields.StringField(primary_key=True, max_length=40) session_key = fields.StringField(primary_key=True, max_length=40)
session_data = fields.StringField() session_data = fields.StringField() if MONGOENGINE_SESSION_DATA_ENCODE \
else fields.DictField()
expire_date = fields.DateTimeField() expire_date = fields.DateTimeField()
meta = {'collection': 'django_session', meta = {'collection': MONGOENGINE_SESSION_COLLECTION,
'db_alias': MONGOENGINE_SESSION_DB_ALIAS, 'db_alias': MONGOENGINE_SESSION_DB_ALIAS,
'allow_inheritance': False} 'allow_inheritance': False}
@ -34,7 +44,10 @@ class SessionStore(SessionBase):
try: try:
s = MongoSession.objects(session_key=self.session_key, s = MongoSession.objects(session_key=self.session_key,
expire_date__gt=datetime.now())[0] expire_date__gt=datetime.now())[0]
return self.decode(force_unicode(s.session_data)) if MONGOENGINE_SESSION_DATA_ENCODE:
return self.decode(force_unicode(s.session_data))
else:
return s.session_data
except (IndexError, SuspiciousOperation): except (IndexError, SuspiciousOperation):
self.create() self.create()
return {} return {}
@ -57,7 +70,10 @@ class SessionStore(SessionBase):
if self.session_key is None: if self.session_key is None:
self._session_key = self._get_new_session_key() self._session_key = self._get_new_session_key()
s = MongoSession(session_key=self.session_key) s = MongoSession(session_key=self.session_key)
s.session_data = self.encode(self._get_session(no_load=must_create)) if MONGOENGINE_SESSION_DATA_ENCODE:
s.session_data = self.encode(self._get_session(no_load=must_create))
else:
s.session_data = self._get_session(no_load=must_create)
s.expire_date = self.get_expiry_date() s.expire_date = self.get_expiry_date()
try: try:
s.save(force_insert=must_create, safe=True) s.save(force_insert=must_create, safe=True)