From 3bdc9a2f0940cf03d6f72212e4db3148f0a0cfb7 Mon Sep 17 00:00:00 2001 From: Adrian Scott Date: Thu, 29 Nov 2012 20:53:09 -0500 Subject: [PATCH] session collection parameter; encoding optional Added a parameter for the name of the session collection; Added the option to not encode session_data, which is useful for expiring sessions of users when a password is changed, etc.; these upgrades provided by SocialVilla Inc. --- mongoengine/django/sessions.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/mongoengine/django/sessions.py b/mongoengine/django/sessions.py index f1783429..0330ee1b 100644 --- a/mongoengine/django/sessions.py +++ b/mongoengine/django/sessions.py @@ -15,13 +15,20 @@ MONGOENGINE_SESSION_DB_ALIAS = getattr( settings, 'MONGOENGINE_SESSION_DB_ALIAS', DEFAULT_CONNECTION_NAME) +MONGOENGINE_SESSION_COLLECTION = getattr( + settings, 'MONGOENGINE_SESSION_COLLECTION', + 'django_session') + +MONGOENGINE_SESSION_DATA_ENCODE = getattr( + settings, 'MONGOENGINE_SESSION_DATA_ENCODE', + True) class MongoSession(Document): 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() - meta = {'collection': 'django_session', + meta = {'collection': MONGOENGINE_SESSION_COLLECTION, 'db_alias': MONGOENGINE_SESSION_DB_ALIAS, 'allow_inheritance': False} @@ -34,7 +41,10 @@ class SessionStore(SessionBase): try: s = MongoSession.objects(session_key=self.session_key, 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): self.create() return {} @@ -57,7 +67,10 @@ class SessionStore(SessionBase): if self.session_key is None: self._session_key = self._get_new_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() try: s.save(force_insert=must_create, safe=True)