From 0376910f3392ee58fa57558458e9f0aa363e2cbc Mon Sep 17 00:00:00 2001 From: Dan Crosta Date: Tue, 17 Apr 2012 19:47:54 -0400 Subject: [PATCH 1/2] refactor get_connection In the previous version, the requested ReadPreference was ignored in the case that the user specified a MongoDB URI. This rearranges the code to ensure that only those values which we explicitly parse out of the URI override values set as keyword arguments. This leaves open the possibility of conflicts between the URI and the kwargs -- we should consider whether to raise an exception if, e.g., username is specified as a kwarg *and* in the URI. --- mongoengine/connection.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) 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): From 2d71eb8a18f50dca717da04f7cf72c2de9ba2816 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Wed, 18 Apr 2012 10:22:26 +0100 Subject: [PATCH 2/2] Added support back for Django 1.3 as well as 1.4 --- docs/changelog.rst | 3 ++- mongoengine/django/auth.py | 28 ++++++++++++++++++++++++++-- mongoengine/django/sessions.py | 15 +++++++++------ 3 files changed, 37 insertions(+), 9 deletions(-) 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/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: