Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tz_aware MongoClient #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion flask_session/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def _get_interface(self, app):
config.setdefault('SESSION_MONGODB', None)
config.setdefault('SESSION_MONGODB_DB', 'flask_session')
config.setdefault('SESSION_MONGODB_COLLECT', 'sessions')
config.setdefault('SESSION_MONGODB_TZ_AWARE', False)
config.setdefault('SESSION_SQLALCHEMY', None)
config.setdefault('SESSION_SQLALCHEMY_TABLE', 'sessions')

Expand All @@ -96,7 +97,7 @@ def _get_interface(self, app):
config['SESSION_MONGODB'], config['SESSION_MONGODB_DB'],
config['SESSION_MONGODB_COLLECT'],
config['SESSION_KEY_PREFIX'], config['SESSION_USE_SIGNER'],
config['SESSION_PERMANENT'])
config['SESSION_PERMANENT'], config['SESSION_MONGODB_TZ_AWARE'])
elif config['SESSION_TYPE'] == 'sqlalchemy':
session_interface = SqlAlchemySessionInterface(
app, config['SESSION_SQLALCHEMY'],
Expand Down
19 changes: 16 additions & 3 deletions flask_session/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:license: BSD, see LICENSE for more details.
"""
import sys
import pytz
import time
from datetime import datetime
from uuid import uuid4
Expand Down Expand Up @@ -374,21 +375,26 @@ class MongoDBSessionInterface(SessionInterface):
:param key_prefix: A prefix that is added to all MongoDB store keys.
:param use_signer: Whether to sign the session id cookie or not.
:param permanent: Whether to use permanent session or not.
:param tz_aware: Whether to use tz_aware MongoClient or not.
"""

serializer = pickle
session_class = MongoDBSession

def __init__(self, client, db, collection, key_prefix, use_signer=False,
permanent=True):
permanent=True, tz_aware=False):
if client is None:
from pymongo import MongoClient
client = MongoClient()
if tz_aware:
client = MongoClient(tz_aware=tz_aware)
else:
client = MongoClient()
self.client = client
self.store = client[db][collection]
self.key_prefix = key_prefix
self.use_signer = use_signer
self.permanent = permanent
self.tz_aware = tz_aware

def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
Expand All @@ -408,7 +414,14 @@ def open_session(self, app, request):

store_id = self.key_prefix + sid
document = self.store.find_one({'id': store_id})
if document and document.get('expiration') <= datetime.utcnow():

# Workaround for tz_aware MongoClient
if self.tz_aware:
utc_now = datetime.utcnow().replace(tzinfo=pytz.UTC)
else:
utc_now = datetime.utcnow()

if document and document.get('expiration') <= utc_now:
# Delete expired session
self.store.remove({'id': store_id})
document = None
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
include_package_data=True,
platforms='any',
install_requires=[
'Flask>=0.8'
'Flask>=0.8',
'pytz'
],
test_suite='test_session',
classifiers=[
Expand Down