diff --git a/docs/changelog.rst b/docs/changelog.rst index 8674e9ee..5a52160f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,8 +5,9 @@ Changelog Changes in 0.6.X ================ -- updated replicasetconnection - pop port if exists +- refactored connection / fixed replicasetconnection - bug fix for unknown connection alias error message +- Sessions support Django 1.3 and Django 1.4 Changes in 0.6.3 ================ diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 9cf8264a..96b8100d 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -39,22 +39,7 @@ def register_connection(alias, name, host='localhost', port=27017, """ global _connection_settings - # Handle uri style connections - if "://" in host: - uri_dict = uri_parser.parse_uri(host) - if uri_dict.get('database') is None: - raise ConnectionError("If using URI style connection include "\ - "database name in string") - _connection_settings[alias] = { - 'host': host, - 'name': uri_dict.get('database'), - 'username': uri_dict.get('username'), - 'password': uri_dict.get('password') - } - _connection_settings[alias].update(kwargs) - return - - _connection_settings[alias] = { + conn_settings = { 'name': name, 'host': host, 'port': port, @@ -64,7 +49,22 @@ def register_connection(alias, name, host='localhost', port=27017, 'password': password, 'read_preference': read_preference } - _connection_settings[alias].update(kwargs) + + # Handle uri style connections + if "://" in host: + uri_dict = uri_parser.parse_uri(host) + if uri_dict.get('database') is None: + raise ConnectionError("If using URI style connection include "\ + "database name in string") + conn_settings.update({ + 'host': host, + 'name': uri_dict.get('database'), + 'username': uri_dict.get('username'), + 'password': uri_dict.get('password'), + 'read_preference': read_preference, + }) + + _connection_settings[alias] = conn_settings def disconnect(alias=DEFAULT_CONNECTION_NAME): diff --git a/mongoengine/django/auth.py b/mongoengine/django/auth.py index 156daf74..000e3528 100644 --- a/mongoengine/django/auth.py +++ b/mongoengine/django/auth.py @@ -1,11 +1,35 @@ +import datetime + from mongoengine import * from django.utils.encoding import smart_str from django.contrib.auth.models import AnonymousUser -from django.contrib.auth.hashers import check_password, make_password from django.utils.translation import ugettext_lazy as _ -import datetime +try: + from django.contrib.auth.hashers import check_password, make_password +except ImportError: + """Handle older versions of Django""" + + def get_hexdigest(algorithm, salt, raw_password): + raw_password, salt = smart_str(raw_password), smart_str(salt) + if algorithm == 'md5': + return md5_constructor(salt + raw_password).hexdigest() + elif algorithm == 'sha1': + return sha_constructor(salt + raw_password).hexdigest() + raise ValueError('Got unknown password algorithm type in password') + + def check_password(raw_password, password): + algo, salt, hash = password.split('$') + return hash == get_hexdigest(algo, salt, raw_password) + + def make_password(raw_password): + from random import random + algo = 'sha1' + salt = get_hexdigest(algo, str(random()), str(random()))[:5] + hash = get_hexdigest(algo, salt, raw_password) + return '%s$%s$%s' % (algo, salt, hash) + REDIRECT_FIELD_NAME = 'next' diff --git a/mongoengine/django/sessions.py b/mongoengine/django/sessions.py index ca35962a..667cf245 100644 --- a/mongoengine/django/sessions.py +++ b/mongoengine/django/sessions.py @@ -1,3 +1,6 @@ +from datetime import datetime + +from django.conf import settings from django.contrib.sessions.backends.base import SessionBase, CreateError from django.core.exceptions import SuspiciousOperation from django.utils.encoding import force_unicode @@ -6,18 +9,18 @@ from mongoengine.document import Document from mongoengine import fields from mongoengine.queryset import OperationError from mongoengine.connection import DEFAULT_CONNECTION_NAME -from django.conf import settings -from datetime import datetime + MONGOENGINE_SESSION_DB_ALIAS = getattr( settings, 'MONGOENGINE_SESSION_DB_ALIAS', DEFAULT_CONNECTION_NAME) + class MongoSession(Document): session_key = fields.StringField(primary_key=True, max_length=40) session_data = fields.StringField() expire_date = fields.DateTimeField() - + meta = {'collection': 'django_session', 'db_alias': MONGOENGINE_SESSION_DB_ALIAS, 'allow_inheritance': False} @@ -41,7 +44,7 @@ class SessionStore(SessionBase): def create(self): while True: - self._session_key = self._get_new_session_key() + self.session_key = self._get_new_session_key() try: self.save(must_create=True) except CreateError: @@ -51,9 +54,9 @@ class SessionStore(SessionBase): return def save(self, must_create=False): - if self._session_key is None: + if self.session_key is None: self.create() - s = MongoSession(session_key=self._session_key) + s = MongoSession(session_key=self.session_key) s.session_data = self.encode(self._get_session(no_load=must_create)) s.expire_date = self.get_expiry_date() try: