From 8d037424d3ca307b72550b55fbe1d075fbd4d568 Mon Sep 17 00:00:00 2001 From: litwisha Date: Thu, 22 Dec 2016 17:15:09 +0200 Subject: [PATCH 01/13] MONGO_CLIENT setting as string --- mongo_sessions/settings.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index 2235790..6ccf06b 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -1,6 +1,6 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured - +from django.utils.module_loading import import_string MONGO_CLIENT = getattr(settings, 'MONGO_CLIENT', False) @@ -33,6 +33,16 @@ if MONGO_DB_USER and MONGO_DB_PASSWORD: MONGO_CLIENT.authenticate(MONGO_DB_USER, MONGO_DB_PASSWORD) +elif isinstance(MONGO_CLIENT, str): + connection, db = MONGO_CLIENT.rsplit('.', 1) + connection = import_string(connection) + MONGO_CLIENT = getattr(connection, db, False) + + if not MONGO_CLIENT: + raise ImproperlyConfigured( + 'Incorrect MONGO_CLIENT string' + ) + try: MONGO_DB_VERSION = MONGO_CLIENT.connection.server_info()['version'] except TypeError: From 2f049c42057ca0d56dc6c2d48159eebd58490c7d Mon Sep 17 00:00:00 2001 From: litwisha Date: Thu, 22 Dec 2016 19:04:34 +0200 Subject: [PATCH 02/13] Add `import_string` compatibility for django versions [1.5; 1.6]. Modify README.rst. Change requirements in setup.py for django version >=1.6 --- README.rst | 9 +++++++++ mongo_sessions/settings.py | 9 +++++++-- setup.py | 2 +- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index b866c5f..ed8f18b 100644 --- a/README.rst +++ b/README.rst @@ -45,6 +45,15 @@ second, if you need to connect to mongodb, like:: MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # all this settings are defaults, you can skip any + +Also, ``MONGO_CLIENT`` can be a string - dotted path to mongo connection with database name:: + + MONGO_CLIENT = 'project.storage.connection.db_name' + +Where +``project.storage`` - module containing a mongo connection instance, +``connection`` - mongo connection instance, +``db_name`` - name of mongo database. ``expireAfterSeconds`` index value by default is ``SESSION_COOKIE_AGE`` you can change:: diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index 6ccf06b..f7a6dd4 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -1,6 +1,11 @@ from django.conf import settings from django.core.exceptions import ImproperlyConfigured -from django.utils.module_loading import import_string + +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) @@ -40,7 +45,7 @@ if not MONGO_CLIENT: raise ImproperlyConfigured( - 'Incorrect MONGO_CLIENT string' + 'Incorrect MONGO_CLIENT string', ) try: diff --git a/setup.py b/setup.py index 373a446..1cd5651 100644 --- a/setup.py +++ b/setup.py @@ -49,7 +49,7 @@ def long_description(): url='https://github.com/hellysmile/django-mongo-sessions', zip_safe=False, install_requires=[ - 'django >= 1.4', + 'django >= 1.6', 'pymongo >= 2.4.2' ], license='http://www.apache.org/licenses/LICENSE-2.0', From d58daf18f79bad444b9246626d018a103cbea321 Mon Sep 17 00:00:00 2001 From: litwisha Date: Fri, 23 Dec 2016 13:48:48 +0200 Subject: [PATCH 03/13] Move from MONGO_CLIENT to MONGO_DB setting; Change MONGO_DB string isinstance checker: from `str` to `six.string_types` for python 2 --- mongo_sessions/settings.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index f7a6dd4..3fb8a95 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -1,3 +1,4 @@ +import six from django.conf import settings from django.core.exceptions import ImproperlyConfigured @@ -7,7 +8,7 @@ # 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 = getattr(settings, 'MONGO_DB', False) MONGO_SESSIONS_COLLECTION = getattr( settings, 'MONGO_SESSIONS_COLLECTION', 'mongo_sessions' @@ -19,7 +20,7 @@ settings, 'MONGO_SESSIONS_TTL', settings.SESSION_COOKIE_AGE ) -if not MONGO_CLIENT: +if not MONGO_DB: MONGO_PORT = int(getattr(settings, 'MONGO_PORT', 27017)) MONGO_HOST = getattr(settings, 'MONGO_HOST', 'localhost') MONGO_DB_NAME = getattr(settings, 'MONGO_DB_NAME', 'test') @@ -28,31 +29,24 @@ from pymongo import MongoClient - MONGO_CLIENT = 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) -elif isinstance(MONGO_CLIENT, str): - connection, db = MONGO_CLIENT.rsplit('.', 1) - connection = import_string(connection) - MONGO_CLIENT = getattr(connection, db, False) - - if not MONGO_CLIENT: - raise ImproperlyConfigured( - 'Incorrect MONGO_CLIENT string', - ) +elif isinstance(MONGO_DB, six.string_types): + MONGO_DB = import_string(MONGO_DB) 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( @@ -62,7 +56,7 @@ ''' ) -DB_COLLECTION = MONGO_CLIENT[MONGO_SESSIONS_COLLECTION] +DB_COLLECTION = MONGO_DB[MONGO_SESSIONS_COLLECTION] MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information() From 09d9ecce312017fbd4bf102d3acb109c08ce980c Mon Sep 17 00:00:00 2001 From: litwisha Date: Fri, 23 Dec 2016 16:18:19 +0200 Subject: [PATCH 04/13] MONGO_DB string setting test coverage --- README.rst | 11 +++++------ tests/__init__.py | 2 +- tests/tests.py | 25 +++++++++++++++++++++---- tox.ini | 1 + 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index ed8f18b..d4a1f00 100644 --- a/README.rst +++ b/README.rst @@ -32,7 +32,7 @@ first, if already have mongo connection, like:: import pymongo from pymongo import MongoClient connection = MongoClient() - MONGO_CLIENT = connection.your_database + MONGO_DB = connection.your_database MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option second, if you need to connect to mongodb, like:: @@ -46,14 +46,13 @@ second, if you need to connect to mongodb, like:: # all this settings are defaults, you can skip any -Also, ``MONGO_CLIENT`` can be a string - dotted path to mongo connection with database name:: +Also, ``MONGO_DB`` can be a string - dotted path to mongo connection with database name:: - MONGO_CLIENT = 'project.storage.connection.db_name' + MONGO_DB = 'project.storage.db' Where -``project.storage`` - module containing a mongo connection instance, -``connection`` - mongo connection instance, -``db_name`` - name of mongo database. +``project.storage`` - module containing a mongo db instance, +``db`` - mongo db instance. ``expireAfterSeconds`` index value by default is ``SESSION_COOKIE_AGE`` you can change:: diff --git a/tests/__init__.py b/tests/__init__.py index eea09e6..7f8c64d 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,5 +3,5 @@ settings.configure( SESSION_ENGINE='mongo_sessions.session', - SESSION_COOKIE_AGE=10 + SESSION_COOKIE_AGE=10, ) diff --git a/tests/tests.py b/tests/tests.py index 50a7325..a87f82e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,4 +1,13 @@ # tests stolen from https://github.com/martinrusev/django-redis-sessions +import time +from imp import reload + +from django.conf import settings +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 @@ -6,12 +15,20 @@ # 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_db_string(): + def _test_connection(conn, exc): + with override_settings(MONGO_DB=conn): + with assert_raises(exc): + reload(mongo_session_settings) + + _test_connection('wrong.conn.string', ImportError) + + _test_connection('tests.tests.incorrect_db', AttributeError) def test_modify_and_keys(): diff --git a/tox.ini b/tox.ini index 7caf3ac..0e178ed 100644 --- a/tox.ini +++ b/tox.ini @@ -12,6 +12,7 @@ envlist = [base] deps = nose + six [testenv] commands = From e86a2fe2cdc3df341592ca0f77f4e00c2842316a Mon Sep 17 00:00:00 2001 From: litwisha Date: Fri, 23 Dec 2016 16:22:28 +0200 Subject: [PATCH 05/13] Add six to dependencies --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1cd5651..8282912 100644 --- a/setup.py +++ b/setup.py @@ -50,7 +50,8 @@ def long_description(): zip_safe=False, install_requires=[ 'django >= 1.6', - 'pymongo >= 2.4.2' + 'pymongo >= 2.4.2', + 'six >= 1.0.0', ], license='http://www.apache.org/licenses/LICENSE-2.0', classifiers=filter(None, classifiers.split('\n')), From d3242f70e0246f2e493e20b9a41b181c0110a101 Mon Sep 17 00:00:00 2001 From: litwisha Date: Fri, 23 Dec 2016 19:39:35 +0200 Subject: [PATCH 06/13] Add additional config var MONGO_DB that can be a db instance or a dotted path string to db instance --- README.rst | 16 ++++++---------- mongo_sessions/settings.py | 29 +++++++++++++++++------------ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/README.rst b/README.rst index d4a1f00..6024fac 100644 --- a/README.rst +++ b/README.rst @@ -32,10 +32,14 @@ first, if already have mongo connection, like:: import pymongo from pymongo import MongoClient connection = MongoClient() - MONGO_DB = connection.your_database + MONGO_CLIENT = connection.your_database MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option -second, if you need to connect to mongodb, like:: +second, if already have mongo db instance, like:: + + MONGO_DB = 'project.storage.db' # dotted path mongo database instance + +third, if you need to connect to mongodb, like:: MONGO_PORT = 27017 MONGO_HOST = 'localhost' @@ -45,14 +49,6 @@ second, if you need to connect to mongodb, like:: MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # all this settings are defaults, you can skip any - -Also, ``MONGO_DB`` can be a string - dotted path to mongo connection with database name:: - - MONGO_DB = 'project.storage.db' - -Where -``project.storage`` - module containing a mongo db instance, -``db`` - mongo db instance. ``expireAfterSeconds`` index value by default is ``SESSION_COOKIE_AGE`` you can change:: diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index 3fb8a95..7975230 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -8,6 +8,8 @@ # 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 = getattr(settings, 'MONGO_DB', False) MONGO_SESSIONS_COLLECTION = getattr( @@ -20,7 +22,13 @@ settings, 'MONGO_SESSIONS_TTL', settings.SESSION_COOKIE_AGE ) -if not MONGO_DB: +if MONGO_DB: + if isinstance(MONGO_DB, six.string_types): + MONGO_CLIENT = import_string(MONGO_DB) + else: + MONGO_CLIENT = MONGO_DB + +if not MONGO_CLIENT: MONGO_PORT = int(getattr(settings, 'MONGO_PORT', 27017)) MONGO_HOST = getattr(settings, 'MONGO_HOST', 'localhost') MONGO_DB_NAME = getattr(settings, 'MONGO_DB_NAME', 'test') @@ -29,24 +37,21 @@ from pymongo import MongoClient - _mongo_client = MongoClient( + MONGO_CLIENT = MongoClient( host=MONGO_HOST, port=MONGO_PORT, ) - MONGO_DB = _mongo_client[MONGO_DB_NAME] + MONGO_CLIENT = MONGO_CLIENT[MONGO_DB_NAME] if MONGO_DB_USER and MONGO_DB_PASSWORD: - MONGO_DB.authenticate(MONGO_DB_USER, MONGO_DB_PASSWORD) - -elif isinstance(MONGO_DB, six.string_types): - MONGO_DB = import_string(MONGO_DB) + MONGO_CLIENT.authenticate(MONGO_DB_USER, MONGO_DB_PASSWORD) try: - MONGO_DB_VERSION = MONGO_DB.connection.server_info()['version'] + MONGO_DB_VERSION = MONGO_CLIENT.connection.server_info()['version'] except TypeError: # for pymongo >= 3 - MONGO_DB_VERSION = MONGO_DB.client.server_info()['version'] + MONGO_DB_VERSION = MONGO_CLIENT.client.server_info()['version'] if not float('.'.join(MONGO_DB_VERSION.split('.')[:-1])) >= 2.2: raise ImproperlyConfigured( @@ -56,7 +61,7 @@ ''' ) -DB_COLLECTION = MONGO_DB[MONGO_SESSIONS_COLLECTION] +DB_COLLECTION = MONGO_CLIENT[MONGO_SESSIONS_COLLECTION] MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information() @@ -64,7 +69,7 @@ if len(MONGO_SESSIONS_INDEXES) <= 1: DB_COLLECTION.ensure_index( 'session_key', - unique=True + unique=True, ) DB_COLLECTION.ensure_index( @@ -81,7 +86,7 @@ DB_COLLECTION.ensure_index( 'creation_date', - expireAfterSeconds=MONGO_SESSIONS_TTL + expireAfterSeconds=MONGO_SESSIONS_TTL, ) MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information() From 865e70c682a4dce8efe2add56668476b9ae14fd4 Mon Sep 17 00:00:00 2001 From: litwisha Date: Mon, 26 Dec 2016 13:59:38 +0200 Subject: [PATCH 07/13] Handle ambigious mongo settings: make user to provide only one of the settings: `MONGO_CLIENT` or `MONGO_DB` --- README.rst | 3 +++ mongo_sessions/settings.py | 8 ++++++++ tests/tests.py | 26 ++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 6024fac..b8d2ec3 100644 --- a/README.rst +++ b/README.rst @@ -39,6 +39,9 @@ second, if already have mongo db instance, like:: MONGO_DB = 'project.storage.db' # dotted path mongo database instance + +Note: Provide only one of: ``MONGO_DB`` or ``MONGO_CLIENT`` setting. + third, if you need to connect to mongodb, like:: MONGO_PORT = 27017 diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index 7975230..179a68c 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -22,6 +22,14 @@ settings, 'MONGO_SESSIONS_TTL', settings.SESSION_COOKIE_AGE ) +if MONGO_DB and MONGO_CLIENT: + raise ImproperlyConfigured( + ''' + Ambigious mongo configuration + Provide only one of the following settings: MONGO_DB or MONGO_CLIENT + ''' + ) + if MONGO_DB: if isinstance(MONGO_DB, six.string_types): MONGO_CLIENT = import_string(MONGO_DB) diff --git a/tests/tests.py b/tests/tests.py index a87f82e..5776eb6 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -3,6 +3,7 @@ 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 @@ -21,14 +22,31 @@ def test_incorrect_mongo_db_string(): - def _test_connection(conn, exc): - with override_settings(MONGO_DB=conn): + def _test_connection(settings, exc): + with override_settings(**settings): with assert_raises(exc): reload(mongo_session_settings) - _test_connection('wrong.conn.string', ImportError) + # test non-existent mongoDB instance + settings = { + 'MONGO_DB': 'wrong.conn.string', + } + _test_connection(settings, ImportError) + + # test invalid mongoDB instance + settings = { + 'MONGO_DB': 'tests.tests.incorrect_db', + } + _test_connection(settings, AttributeError) + + # test ambigious mongo settings + settings = { + 'MONGO_DB': 'tests.tests.incorrect_db', + 'MONGO_CLIENT': 'tests.tests.incorrect_db', + } + + _test_connection(settings, ImproperlyConfigured) - _test_connection('tests.tests.incorrect_db', AttributeError) def test_modify_and_keys(): From 6f327619a1d9add919d1bb5490576dbeb7b55ac8 Mon Sep 17 00:00:00 2001 From: litwisha Date: Tue, 27 Dec 2016 14:55:30 +0200 Subject: [PATCH 08/13] Remove MONGO_DB settings and handle MONGO_CLIENT in different ways: as Database or MongoClient instance, and as dotted path string to the instaces --- mongo_sessions/settings.py | 48 ++++++++++++++++++++------------------ setup.py | 1 - 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/mongo_sessions/settings.py b/mongo_sessions/settings.py index 179a68c..f3e44ae 100644 --- a/mongo_sessions/settings.py +++ b/mongo_sessions/settings.py @@ -1,6 +1,8 @@ -import six from django.conf import settings from django.core.exceptions import ImproperlyConfigured +from django.utils import six +from pymongo import MongoClient +from pymongo.database import Database try: from django.utils.module_loading import import_string @@ -10,7 +12,7 @@ MONGO_CLIENT = getattr(settings, 'MONGO_CLIENT', False) -MONGO_DB = getattr(settings, 'MONGO_DB', False) +MONGO_DB_NAME = getattr(settings, 'MONGO_DB_NAME', 'test') MONGO_SESSIONS_COLLECTION = getattr( settings, 'MONGO_SESSIONS_COLLECTION', 'mongo_sessions' @@ -22,44 +24,44 @@ settings, 'MONGO_SESSIONS_TTL', settings.SESSION_COOKIE_AGE ) -if MONGO_DB and MONGO_CLIENT: - raise ImproperlyConfigured( - ''' - Ambigious mongo configuration - Provide only one of the following settings: MONGO_DB or MONGO_CLIENT - ''' - ) +if MONGO_CLIENT: + if isinstance(MONGO_CLIENT, six.string_types): + MONGO_CLIENT = import_string(MONGO_CLIENT) -if MONGO_DB: - if isinstance(MONGO_DB, six.string_types): - MONGO_CLIENT = import_string(MONGO_DB) - else: - MONGO_CLIENT = MONGO_DB + if isinstance(MONGO_CLIENT, MongoClient): + MONGO_DB = MONGO_CLIENT[MONGO_DB_NAME] + + elif isinstance(MONGO_CLIENT, Database): + MONGO_DB = MONGO_CLIENT -if not 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( @@ -69,7 +71,7 @@ ''' ) -DB_COLLECTION = MONGO_CLIENT[MONGO_SESSIONS_COLLECTION] +DB_COLLECTION = MONGO_DB[MONGO_SESSIONS_COLLECTION] MONGO_SESSIONS_INDEXES = DB_COLLECTION.index_information() diff --git a/setup.py b/setup.py index 8282912..1d45deb 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,6 @@ def long_description(): install_requires=[ 'django >= 1.6', 'pymongo >= 2.4.2', - 'six >= 1.0.0', ], license='http://www.apache.org/licenses/LICENSE-2.0', classifiers=filter(None, classifiers.split('\n')), From 96a5a4575b142e1d7e6c731e1b8da6642b7edfb1 Mon Sep 17 00:00:00 2001 From: litwisha Date: Tue, 27 Dec 2016 16:25:36 +0200 Subject: [PATCH 09/13] Edit tests for MONGO_CLIENT setting and use django`s six --- AUTHORS | 6 ++++++ README.rst | 27 ++++++++++++++++++++------- tests/tests.py | 13 +++++-------- tox.ini | 1 - 4 files changed, 31 insertions(+), 16 deletions(-) create mode 100644 AUTHORS diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..c3dbb79 --- /dev/null +++ b/AUTHORS @@ -0,0 +1,6 @@ +django-mongo-session is written and maintained by ``hellysmile`` and following contributors: + +Patches and Suggestions +``````````````````````` + +- Andrew Lytvyn (``litwisha``) \ No newline at end of file diff --git a/README.rst b/README.rst index b8d2ec3..6151d1c 100644 --- a/README.rst +++ b/README.rst @@ -4,7 +4,7 @@ django-mongo-sessions :info: mongodb as Django sessions backend .. image:: https://api.travis-ci.org/hellysmile/django-mongo-sessions.png - :target: https://travis-ci.org/hellysmile/django-mongo-sessions +:target: https://travis-ci.org/hellysmile/django-mongo-sessions features ******** @@ -24,10 +24,10 @@ 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 @@ -35,14 +35,27 @@ first, if already have mongo connection, like:: MONGO_CLIENT = connection.your_database MONGO_SESSIONS_COLLECTION = 'mongo_sessions' # default option -second, if already have mongo db instance, like:: +or as ``pymongo.MongoClient`` instance:: - MONGO_DB = 'project.storage.db' # dotted path mongo database 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:: -Note: Provide only one of: ``MONGO_DB`` or ``MONGO_CLIENT`` setting. + MONGO_CLIENT = 'project.main.db' # pymongo.database.Database instance -third, if you need to connect to mongodb, like:: +second, if you need to connect to mongodb, like:: MONGO_PORT = 27017 MONGO_HOST = 'localhost' diff --git a/tests/tests.py b/tests/tests.py index 5776eb6..57771f5 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -21,7 +21,7 @@ incorrect_db = object() -def test_incorrect_mongo_db_string(): +def test_incorrect_mongo_client_setting(): def _test_connection(settings, exc): with override_settings(**settings): with assert_raises(exc): @@ -29,26 +29,23 @@ def _test_connection(settings, exc): # test non-existent mongoDB instance settings = { - 'MONGO_DB': 'wrong.conn.string', + 'MONGO_CLIENT': 'wrong.conn.string', } _test_connection(settings, ImportError) # test invalid mongoDB instance settings = { - 'MONGO_DB': 'tests.tests.incorrect_db', + 'MONGO_CLIENT': object(), } - _test_connection(settings, AttributeError) + _test_connection(settings, ImproperlyConfigured) - # test ambigious mongo settings + # test invalid mongoDB instance as str settings = { - 'MONGO_DB': 'tests.tests.incorrect_db', 'MONGO_CLIENT': 'tests.tests.incorrect_db', } - _test_connection(settings, ImproperlyConfigured) - def test_modify_and_keys(): eq_(session_engine.modified, False) session_engine['test'] = 'test_me' diff --git a/tox.ini b/tox.ini index 0e178ed..7caf3ac 100644 --- a/tox.ini +++ b/tox.ini @@ -12,7 +12,6 @@ envlist = [base] deps = nose - six [testenv] commands = From e03e75f06be2e1e20f5e313a2b1c3395c6940edf Mon Sep 17 00:00:00 2001 From: litwisha Date: Tue, 27 Dec 2016 16:46:28 +0200 Subject: [PATCH 10/13] Update AUTHORS: newline at the end of file --- AUTHORS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index c3dbb79..f250bef 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,4 +3,4 @@ django-mongo-session is written and maintained by ``hellysmile`` and following c Patches and Suggestions ``````````````````````` -- Andrew Lytvyn (``litwisha``) \ No newline at end of file +- Andrew Lytvyn (``litwisha``) From adc0b7423fb8f24a491ee743dcfa02f4ac0b4e7d Mon Sep 17 00:00:00 2001 From: litwisha Date: Tue, 27 Dec 2016 16:51:33 +0200 Subject: [PATCH 11/13] Fix typo --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 6151d1c..a21ecaf 100644 --- a/README.rst +++ b/README.rst @@ -4,7 +4,7 @@ django-mongo-sessions :info: mongodb as Django sessions backend .. image:: https://api.travis-ci.org/hellysmile/django-mongo-sessions.png -:target: https://travis-ci.org/hellysmile/django-mongo-sessions + :target: https://travis-ci.org/hellysmile/django-mongo-sessions features ******** From 7882920fd2f4c5336a6e6fad079d7e774d4f8172 Mon Sep 17 00:00:00 2001 From: litwisha Date: Wed, 28 Dec 2016 18:10:33 +0200 Subject: [PATCH 12/13] Add django 1.6-1.10 and python 2.6-3.5 environments in tox.ini and fix tests --- tests/tests.py | 7 +-- tox.ini | 135 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 111 insertions(+), 31 deletions(-) diff --git a/tests/tests.py b/tests/tests.py index 57771f5..0deb336 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -24,14 +24,15 @@ def test_incorrect_mongo_client_setting(): def _test_connection(settings, exc): with override_settings(**settings): - with assert_raises(exc): - reload(mongo_session_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', } - _test_connection(settings, ImportError) + # different exceptions in different django versions + _test_connection(settings, (ImportError, ImproperlyConfigured)) # test invalid mongoDB instance settings = { diff --git a/tox.ini b/tox.ini index 7caf3ac..1d7ee1d 100644 --- a/tox.ini +++ b/tox.ini @@ -1,13 +1,21 @@ [tox] envlist = - py25-dj14, - py26-dj14, - py27-dj14, - py27-dj15, - py32-dj15, - py33-dj15, - pypy19-dj14, - pypy19-dj15 + py26-dj16, + py27-dj16, + py32-dj16, + py33-dj16, + py27-dj17, + py32-dj17, + py33-dj17, + py34-dj17, + py27-dj18, + py32-dj18, + py33-dj18, + py34-dj18, + py35-dj18, + py27-dj19, + py34-dj19, + py35-dj19, [base] deps = @@ -17,50 +25,121 @@ deps = commands = {envpython} setup.py nosetests -[testenv:py25-dj14] -basepython=python2.5 +# django 1.6 on python versions: 2.6, 2.7, 3.2, 3.3 +[testenv:py26-dj16] +basepython=python2.6 deps = - Django>=1.4,<1.5 + Django>=1.6,<1.7 {[base]deps} -[testenv:py26-dj14] -basepython=python2.6 +[testenv:py27-dj16] +basepython=python2.7 +deps = + Django>=1.6,<1.7 + {[base]deps} + +[testenv:py32-dj16] +basepython=python3.2 +deps = + Django>=1.6,<1.7 + {[base]deps} + +[testenv:py33-dj16] +basepython=python3.3 deps = - Django>=1.4,<1.5 + Django>=1.6,<1.7 {[base]deps} -[testenv:py27-dj14] +# django 1.7 on python versions: 2.7, 3.2, 3.4 +[testenv:py27-dj17] basepython=python2.7 deps = - Django>=1.4,<1.5 + Django>=1.7,<1.8 + {[base]deps} + +[testenv:py32-dj17] +basepython=python3.2 +deps = + Django>=1.7,<1.8 + {[base]deps} + +[testenv:py33-dj17] +basepython=python3.3 +deps = + Django>=1.7,<1.8 + {[base]deps} + +[testenv:py34-dj17] +basepython=python3.4 +deps = + Django>=1.7,<1.8 {[base]deps} -[testenv:py27-dj15] +# django 1.8 on python versions: 2.7, 3.2, 3.3, 3.4, 3.5 +[testenv:py27-dj18] basepython=python2.7 deps = - Django>=1.5 + Django>=1.8,<1.9 {[base]deps} -[testenv:py32-dj15] +[testenv:py32-dj18] basepython=python3.2 deps = - Django>=1.5 + Django>=1.8,<1.9 {[base]deps} -[testenv:py33-dj15] +[testenv:py33-dj18] basepython=python3.3 deps = - Django>=1.5 + Django>=1.8,<1.9 + {[base]deps} + +[testenv:py34-dj18] +basepython=python3.4 +deps = + Django>=1.8,<1.9 + {[base]deps} + +[testenv:py35-dj18] +basepython=python3.5 +deps = + Django>=1.8,<1.9 + {[base]deps} + +# django 1.9 on python versions: 2.7, 3.4, 3.5 +[testenv:py27-dj19] +basepython=python2.7 +deps = + Django>=1.9,<1.10 + {[base]deps} + +[testenv:py34-dj19] +basepython=python3.4 +deps = + Django>=1.9,<1.10 + {[base]deps} + +[testenv:py35-dj19] +basepython=python3.5 +deps = + Django>=1.9,<1.10 + {[base]deps} + +# django 1.10 on python versions: 2.7, 3.4, 3.5 +[testenv:py27-dj110] +basepython=python2.7 +deps = + Django>=1.10,<1.11 {[base]deps} -[testenv:pypy19-dj14] -basepython=pypy +[testenv:py34-dj110] +basepython=python3.4 deps = - Django>=1.4,<1.5 + Django>=1.10,<1.11 {[base]deps} -[testenv:pypy19-dj15] -basepython=pypy +[testenv:py35-dj110] +basepython=python3.5 deps = - Django>=1.5 + Django>=1.10,<1.11 {[base]deps} From 36e673c2e9b8123fc823048fafbc54503a236404 Mon Sep 17 00:00:00 2001 From: litwisha Date: Thu, 29 Dec 2016 18:15:21 +0200 Subject: [PATCH 13/13] Add python2.6-3.5 to django1.6-1.10 travis matrix --- .travis.yml | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7c7868b..791db49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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