Code Monkey home page Code Monkey logo

django-mongo-sessions's Introduction

django-mongo-sessions

info:mongodb as Django sessions backend
https://api.travis-ci.org/hellysmile/django-mongo-sessions.png

features

  • fast NoSQL Django sessions backend
  • invalidation via TTL

installation

run pip install django-mongo-sessions

set mongo_sessions.session as session engine:

SESSION_ENGINE = 'mongo_sessions.session'

settings

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

first, if already have mongo connection, like:

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

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

MONGO_PORT = 27017
MONGO_HOST = 'localhost'
MONGO_DB_NAME = 'test'
MONGO_DB_USER = False
MONGO_DB_PASSWORD = False
MONGO_SESSIONS_COLLECTION = 'mongo_sessions'

# all this settings are defaults, you can skip any

expireAfterSeconds index value by default is SESSION_COOKIE_AGE you can change:

MONGO_SESSIONS_TTL = 60 * 60 # one hour

be sure, that you know what are you doing with it, SESSION_COOKIE_AGE will get different expiration time

every time you change one of this values, expireAfterSeconds index will be dropped and then will be indexed with ensureIndex again, be careful here

it is good way to change expireAfterSeconds only by 1 running instance

tests

pip install tox
tox

django-mongo-sessions's People

Contributors

hellysmile avatar litwisha avatar

Stargazers

Oluwatosin Olubiyi avatar 박지혜 avatar Ali Ünsal avatar  avatar  avatar Michal Kolarczyk avatar Ben Irving avatar Cristhofer Andana Alcaino avatar Eric Peterson avatar Luiz Boaretto avatar  avatar

Watchers

James Cloos avatar Dj Gilcrease avatar  avatar Fedir Alifirenko avatar  avatar  avatar

django-mongo-sessions's Issues

can't compare offset-naive and offset-aware datetimes

Received the next TypeError when USE_TZ = True setted in settings.py:

Internal Server Error: /
Traceback (most recent call last):
  File "env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "env/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "env/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File ".../views.py", line 9, in index
    print(f'exists {session.exists(session.session_key)}')
  File "env/lib/python3.6/site-packages/mongo_sessions/session.py", line 51, in exists
    if session['creation_date'] <= self.get_expiration_date():
TypeError: can't compare offset-naive and offset-aware datetimes

It seems that setting USE_TZ = False in settings.py fixes the issue.

ObjectId is not JSON serializable

I got this error when I am using django-mongo-session.
In my web app I tried to store an ObjectId in the session.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 201, in get_response
response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/middleware.py", line 38, in process_response
request.session.save()
File "/usr/local/lib/python2.7/dist-packages/mongo_sessions/session.py", line 75, in save
self._get_session(no_load=must_create)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 87, in encode
serialized = self.serializer().dumps(session_dict)
File "/usr/local/lib/python2.7/dist-packages/django/core/signing.py", line 88, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/lib/python2.7/json/init.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 178, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ObjectId('5338453b514f2735a57e9183') is not JSON serializable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.