Skip to content
Open
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
44 changes: 34 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
language: python
services: mongodb
python:
- "3.5"
- "3.4"
- "3.3"
- "3.2"
- "2.7"
- "2.6"
- "2.5"
- "pypy"
env:
- DJANGO="Django>=1.4,<1.5"
- DJANGO="Django>=1.5"
- DJANGO="Django>=1.6,<1.7"
- DJANGO="Django>=1.7,<1.8"
- DJANGO="Django>=1.8,<1.9"
- DJANGO="Django>=1.9,<1.10"
- DJANGO="Django>=1.10,<1.11"
matrix:
exclude:
- python: "3.3"
env: DJANGO="Django>=1.4,<1.5"
- python: "3.2"
env: DJANGO="Django>=1.4,<1.5"
- python: "2.6"
env: DJANGO="Django>=1.5"
- python: "2.5"
env: DJANGO="Django>=1.5"
env: DJANGO="Django>=1.7,<1.8"
- python: "2.6"
env: DJANGO="Django>=1.8,<1.9"
- python: "2.6"
env: DJANGO="Django>=1.9,<1.10"
- python: "2.6"
env: DJANGO="Django>=1.10,<1.11"

- python: "3.2"
env: DJANGO="Django>=1.9,<1.10"
- python: "3.2"
env: DJANGO="Django>=1.10,<1.11"

- python: "3.3"
env: DJANGO="Django>=1.7,<1.8"
- python: "3.3"
env: DJANGO="Django>=1.9,<1.10"
- python: "3.3"
env: DJANGO="Django>=1.10,<1.11"

- python: "3.4"
env: DJANGO="Django>=1.6,<1.7"

- python: "3.5"
env: DJANGO="Django>=1.6,<1.7"
- python: "3.5"
env: DJANGO="Django>=1.7,<1.8"
install:
- pip install $DJANGO --use-mirrors
script: python setup.py nosetests
6 changes: 6 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
django-mongo-session is written and maintained by ``hellysmile`` and following contributors:

Patches and Suggestions
```````````````````````

- Andrew Lytvyn (``litwisha``)
24 changes: 22 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,37 @@ set ``mongo_sessions.session`` as session engine::
settings
--------

there is two ways to setup mongodb connection at ``settings.py``
there are two ways to setup mongodb connection at ``settings.py``


first, if already have mongo connection, like::
first, if already have mongo connection as ``pymongo.database.Database`` instance::

import pymongo
from pymongo import MongoClient
connection = MongoClient()
MONGO_CLIENT = connection.your_database
MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option

or as ``pymongo.MongoClient`` instance::

import pymongo
from pymongo import MongoClient
connection = MongoClient()
MONGO_CLIENT = connection
MONGO_DB_NAME = 'test' # default option
MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option



Also, ``MONGO_CLIENT`` setting can be a dotted path string to ``pymongo.MongoClient`` or ``pymongo.database.Database`` instances::

MONGO_CLIENT = 'project.main.client' # MongoClient instance
MONGO_DB_NAME = 'test' # default option

or::

MONGO_CLIENT = 'project.main.db' # pymongo.database.Database instance

second, if you need to connect to mongodb, like::

MONGO_PORT = 27017
Expand Down
46 changes: 35 additions & 11 deletions mongo_sessions/settings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import six

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try ImportError -> import six

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked out all django versions starting from 1.6:
they all have django.utils.six module

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, then set to setup.py >=1.6,<2, there will be no six at django 2

from pymongo import MongoClient
from pymongo.database import Database

try:
from django.utils.module_loading import import_string
except ImportError:
# Django 1.5 <= version <= 1.6
from django.utils.module_loading import import_by_path as import_string

MONGO_CLIENT = getattr(settings, 'MONGO_CLIENT', False)

MONGO_DB_NAME = getattr(settings, 'MONGO_DB_NAME', 'test')

MONGO_SESSIONS_COLLECTION = getattr(
settings, 'MONGO_SESSIONS_COLLECTION', 'mongo_sessions'
)
Expand All @@ -14,30 +24,44 @@
settings, 'MONGO_SESSIONS_TTL', settings.SESSION_COOKIE_AGE
)

if not MONGO_CLIENT:
if MONGO_CLIENT:
if isinstance(MONGO_CLIENT, six.string_types):
MONGO_CLIENT = import_string(MONGO_CLIENT)

if isinstance(MONGO_CLIENT, MongoClient):
MONGO_DB = MONGO_CLIENT[MONGO_DB_NAME]

elif isinstance(MONGO_CLIENT, Database):
MONGO_DB = MONGO_CLIENT

else:
raise ImproperlyConfigured(
'''
Incorrect MONGO_CLIENT settings.
Must be MongoClient or Database instance
'''
)
else:
MONGO_PORT = int(getattr(settings, 'MONGO_PORT', 27017))
MONGO_HOST = getattr(settings, 'MONGO_HOST', 'localhost')
MONGO_DB_NAME = getattr(settings, 'MONGO_DB_NAME', 'test')
MONGO_DB_USER = getattr(settings, 'MONGO_DB_USER', False)
MONGO_DB_PASSWORD = getattr(settings, 'MONGO_DB_PASSWORD', False)

from pymongo import MongoClient

MONGO_CLIENT = MongoClient(
host=MONGO_HOST,
port=MONGO_PORT,
)

MONGO_CLIENT = MONGO_CLIENT[MONGO_DB_NAME]
MONGO_DB = MONGO_CLIENT[MONGO_DB_NAME]

if MONGO_DB_USER and MONGO_DB_PASSWORD:
MONGO_CLIENT.authenticate(MONGO_DB_USER, MONGO_DB_PASSWORD)
MONGO_DB.authenticate(MONGO_DB_USER, MONGO_DB_PASSWORD)

try:
MONGO_DB_VERSION = MONGO_CLIENT.connection.server_info()['version']
MONGO_DB_VERSION = MONGO_DB.connection.server_info()['version']
except TypeError:
# for pymongo >= 3
MONGO_DB_VERSION = MONGO_CLIENT.client.server_info()['version']
MONGO_DB_VERSION = MONGO_DB.client.server_info()['version']

if not float('.'.join(MONGO_DB_VERSION.split('.')[:-1])) >= 2.2:
raise ImproperlyConfigured(
Expand All @@ -47,15 +71,15 @@
'''
)

DB_COLLECTION = MONGO_CLIENT[MONGO_SESSIONS_COLLECTION]
DB_COLLECTION = MONGO_DB[MONGO_SESSIONS_COLLECTION]

MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information()

# check existing indexes
if len(MONGO_SESSIONS_INDEXES) <= 1:
DB_COLLECTION.ensure_index(
'session_key',
unique=True
unique=True,
)

DB_COLLECTION.ensure_index(
Expand All @@ -72,7 +96,7 @@

DB_COLLECTION.ensure_index(
'creation_date',
expireAfterSeconds=MONGO_SESSIONS_TTL
expireAfterSeconds=MONGO_SESSIONS_TTL,
)

MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information()
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def long_description():
url='https://github.com/hellysmile/django-mongo-sessions',
zip_safe=False,
install_requires=[
'django >= 1.4',
'pymongo >= 2.4.2'
'django >= 1.6',
'pymongo >= 2.4.2',
],
license='http://www.apache.org/licenses/LICENSE-2.0',
classifiers=filter(None, classifiers.split('\n')),
Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

settings.configure(
SESSION_ENGINE='mongo_sessions.session',
SESSION_COOKIE_AGE=10
SESSION_COOKIE_AGE=10,
)
41 changes: 37 additions & 4 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
# tests stolen from https://github.com/martinrusev/django-redis-sessions
import time
from imp import reload

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.test.utils import override_settings

from mongo_sessions import settings as mongo_session_settings
from nose.tools import assert_raises, eq_

try:
# For Django versions < 1.9
from django.utils.importlib import import_module
except ImportError:
# For Django versions >= 1.9
from django.utils.module_loading import import_module

from django.conf import settings
import time
from nose.tools import eq_
session_engine = import_module(settings.SESSION_ENGINE).SessionStore()

incorrect_db = object()

session_engine = import_module(settings.SESSION_ENGINE).SessionStore()

def test_incorrect_mongo_client_setting():
def _test_connection(settings, exc):
with override_settings(**settings):
# because older versions has no support as contextmanager
assert_raises(exc, reload, mongo_session_settings)

# test non-existent mongoDB instance
settings = {
'MONGO_CLIENT': 'wrong.conn.string',
}
# different exceptions in different django versions
_test_connection(settings, (ImportError, ImproperlyConfigured))

# test invalid mongoDB instance
settings = {
'MONGO_CLIENT': object(),
}
_test_connection(settings, ImproperlyConfigured)

# test invalid mongoDB instance as str
settings = {
'MONGO_CLIENT': 'tests.tests.incorrect_db',
}
_test_connection(settings, ImproperlyConfigured)


def test_modify_and_keys():
Expand Down
Loading