From af1d7ef664b3acf2b511509cc4f05a39efb20183 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Tue, 5 Jan 2010 18:17:44 +0000 Subject: [PATCH 01/14] Added BooleanField --- docs/userguide.rst | 4 ++-- mongoengine/fields.py | 27 +++++++++++++++++++-------- setup.py | 17 +++++++++++++++-- tests/fields.py | 15 +++++++++++++++ 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/docs/userguide.rst b/docs/userguide.rst index 152e3402..c1dd13ae 100644 --- a/docs/userguide.rst +++ b/docs/userguide.rst @@ -2,8 +2,6 @@ User Guide ========== -.. _guide-connecting: - Installing ========== MongoEngine is available on PyPI, so to use it you can use @@ -20,6 +18,8 @@ Alternatively, if you don't have setuptools installed, `download it from PyPi # python setup.py install +.. _guide-connecting: + Connecting to MongoDB ===================== To connect to a running instance of :program:`mongod`, use the diff --git a/mongoengine/fields.py b/mongoengine/fields.py index badc7363..6612d444 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -7,9 +7,9 @@ import pymongo import datetime -__all__ = ['StringField', 'IntField', 'FloatField', 'DateTimeField', - 'EmbeddedDocumentField', 'ListField', 'ObjectIdField', - 'ReferenceField', 'ValidationError'] +__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField', + 'DateTimeField', 'EmbeddedDocumentField', 'ListField', + 'ObjectIdField', 'ReferenceField', 'ValidationError'] class StringField(BaseField): @@ -25,7 +25,7 @@ class StringField(BaseField): return unicode(value) def validate(self, value): - assert(isinstance(value, (str, unicode))) + assert isinstance(value, (str, unicode)) if self.max_length is not None and len(value) > self.max_length: raise ValidationError('String value is too long') @@ -50,7 +50,7 @@ class IntField(BaseField): return int(value) def validate(self, value): - assert(isinstance(value, (int, long))) + assert isinstance(value, (int, long)) if self.min_value is not None and value < self.min_value: raise ValidationError('Integer value is too small') @@ -71,7 +71,7 @@ class FloatField(BaseField): return float(value) def validate(self, value): - assert(isinstance(value, float)) + assert isinstance(value, float) if self.min_value is not None and value < self.min_value: raise ValidationError('Float value is too small') @@ -80,12 +80,23 @@ class FloatField(BaseField): raise ValidationError('Float value is too large') +class BooleanField(BaseField): + """A boolean field type. + """ + + def to_python(self, value): + return bool(value) + + def validate(self, value): + assert isinstance(value, bool) + + class DateTimeField(BaseField): """A datetime field. """ def validate(self, value): - assert(isinstance(value, datetime.datetime)) + assert isinstance(value, datetime.datetime) class EmbeddedDocumentField(BaseField): @@ -202,7 +213,7 @@ class ReferenceField(BaseField): return pymongo.dbref.DBRef(collection, id_) def validate(self, value): - assert(isinstance(value, (self.document_type, pymongo.dbref.DBRef))) + assert isinstance(value, (self.document_type, pymongo.dbref.DBRef)) def lookup_member(self, member_name): return self.document_type._fields.get(member_name) diff --git a/setup.py b/setup.py index b8f43e1e..e0585b7c 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ from setuptools import setup, find_packages - -VERSION = '0.1.1' +import os DESCRIPTION = "A Python Document-Object Mapper for working with MongoDB" @@ -10,6 +9,20 @@ try: except: pass +def get_version(version_tuple): + version = '%s.%s' % (version_tuple[0], version_tuple[1]) + if version_tuple[2]: + version = '%s.%s' % (version, version_tuple[2]) + return version + +# Dirty hack to get version number from monogengine/__init__.py - we can't +# import it as it depends on PyMongo and PyMongo isn't installed until this +# file is read +init = os.path.join(os.path.dirname(__file__), 'mongoengine', '__init__.py') +version_line = filter(lambda l: l.startswith('VERSION'), open(init))[0] +VERSION = get_version(eval(version_line.split('=')[-1])) +print VERSION + CLASSIFIERS = [ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', diff --git a/tests/fields.py b/tests/fields.py index b580dc20..f61d45a8 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -113,6 +113,21 @@ class FieldTest(unittest.TestCase): person.height = 4.0 self.assertRaises(ValidationError, person.validate) + def test_boolean_validation(self): + """Ensure that invalid values cannot be assigned to boolean fields. + """ + class Person(Document): + admin = BooleanField() + + person = Person() + person.admin = True + person.validate() + + person.admin = 2 + self.assertRaises(ValidationError, person.validate) + person.admin = 'Yes' + self.assertRaises(ValidationError, person.validate) + def test_datetime_validation(self): """Ensure that invalid values cannot be assigned to datetime fields. """ From 4ae21a671d33e36aae7c1293bc8fe320ca1460ac Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Tue, 5 Jan 2010 19:37:30 +0000 Subject: [PATCH 02/14] Document dict access now only looks for fields --- mongoengine/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index cbea67df..7c166dff 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -202,9 +202,11 @@ class BaseDocument(object): """Dictionary-style field access, return a field's value if present. """ try: - return getattr(self, name) + if name in self._fields: + return getattr(self, name) except AttributeError: - raise KeyError(name) + pass + raise KeyError(name) def __setitem__(self, name, value): """Dictionary-style field access, set a field's value. From 196f4471bed28e5354d168d108ae2ebc783f077d Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Wed, 6 Jan 2010 00:40:35 +0000 Subject: [PATCH 03/14] Made connection lazy --- docs/apireference.rst | 2 ++ mongoengine/connection.py | 38 +++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/apireference.rst b/docs/apireference.rst index 86818805..0b4bb480 100644 --- a/docs/apireference.rst +++ b/docs/apireference.rst @@ -38,6 +38,8 @@ Fields .. autoclass:: mongoengine.FloatField +.. autoclass:: mongoengine.BooleanField + .. autoclass:: mongoengine.DateTimeField .. autoclass:: mongoengine.EmbeddedDocumentField diff --git a/mongoengine/connection.py b/mongoengine/connection.py index 15e07fd5..ee8d735b 100644 --- a/mongoengine/connection.py +++ b/mongoengine/connection.py @@ -10,6 +10,10 @@ _connection_settings = { 'pool_size': 1, } _connection = None + +_db_name = None +_db_username = None +_db_password = None _db = None @@ -19,14 +23,30 @@ class ConnectionError(Exception): def _get_connection(): global _connection + # Connect to the database if not already connected if _connection is None: - _connection = Connection(**_connection_settings) + try: + _connection = Connection(**_connection_settings) + except: + raise ConnectionError('Cannot connect to the database') return _connection def _get_db(): - global _db + global _db, _connection + # Connect if not already connected + if _connection is None: + _connection = _get_connection() + if _db is None: - raise ConnectionError('Not connected to database') + # _db_name will be None if the user hasn't called connect() + if _db_name is None: + raise ConnectionError('Not connected to the database') + + # Get DB from current connection and authenticate if necessary + _db = _connection[_db_name] + if _db_username and _db_password: + _db.authenticate(_db_username, _db_password) + return _db def connect(db, username=None, password=None, **kwargs): @@ -35,12 +55,8 @@ def connect(db, username=None, password=None, **kwargs): the default port on localhost. If authentication is needed, provide username and password arguments as well. """ - global _db - + global _connection_settings, _db_name, _db_username, _db_password _connection_settings.update(kwargs) - connection = _get_connection() - # Get DB from connection and auth if necessary - _db = connection[db] - if username is not None and password is not None: - _db.authenticate(username, password) - + _db_name = db + _db_username = username + _db_password = password From 557fb19d131736f858e54c991dceabbe8d68ccbf Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Wed, 6 Jan 2010 03:14:21 +0000 Subject: [PATCH 04/14] Query values may be processed before being used --- mongoengine/base.py | 8 ++++++++ mongoengine/document.py | 5 +++-- mongoengine/fields.py | 6 ++++++ mongoengine/queryset.py | 36 +++++++++++++++++++++++++++--------- tests/document.py | 2 ++ tests/queryset.py | 26 ++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 11 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index 7c166dff..d4412e2a 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -49,6 +49,11 @@ class BaseField(object): """ return self.to_python(value) + def prepare_query_value(self, value): + """Prepare a value that is being used in a query for PyMongo. + """ + return value + def validate(self, value): """Perform validation on a value. """ @@ -67,6 +72,9 @@ class ObjectIdField(BaseField): return pymongo.objectid.ObjectId(value) return value + def prepare_query_value(self, value): + return self.to_mongo(value) + def validate(self, value): try: pymongo.objectid.ObjectId(str(value)) diff --git a/mongoengine/document.py b/mongoengine/document.py index 822a3ea5..e26c5ed0 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -67,8 +67,9 @@ class Document(BaseDocument): def reload(self): """Reloads all attributes from the database. """ - object_id = self._fields['id'].to_mongo(self.id) - obj = self.__class__.objects(id=object_id).first() + #object_id = self._fields['id'].to_mongo(self.id) + #obj = self.__class__.objects(id=object_id).first() + obj = self.__class__.objects(id=self.id).first() for field in self._fields: setattr(self, field, getattr(obj, field)) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 6612d444..61fb385a 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -199,18 +199,24 @@ class ReferenceField(BaseField): def to_mongo(self, document): if isinstance(document, (str, unicode, pymongo.objectid.ObjectId)): + # document may already be an object id id_ = document else: + # We need the id from the saved object to create the DBRef id_ = document.id if id_ is None: raise ValidationError('You can only reference documents once ' 'they have been saved to the database') + # id may be a string rather than an ObjectID object if not isinstance(id_, pymongo.objectid.ObjectId): id_ = pymongo.objectid.ObjectId(id_) collection = self.document_type._meta['collection'] return pymongo.dbref.DBRef(collection, id_) + + def prepare_query_value(self, value): + return self.to_mongo(value) def validate(self, value): assert isinstance(value, (self.document_type, pymongo.dbref.DBRef)) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index ff2d8a3e..7b3f7c6b 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -53,12 +53,13 @@ class QuerySet(object): return self._cursor_obj @classmethod - def _translate_field_name(cls, document, parts): - """Translate a field attribute name to a database field name. + def _lookup_field(cls, document, parts): + """Lookup a field based on its attribute and return a list containing + the field's parents and the field. """ if not isinstance(parts, (list, tuple)): parts = [parts] - field_names = [] + fields = [] field = None for field_name in parts: if field is None: @@ -70,9 +71,15 @@ class QuerySet(object): if field is None: raise InvalidQueryError('Cannot resolve field "%s"' % field_name) - field_names.append(field.name) - return field_names - + fields.append(field) + return fields + + @classmethod + def _translate_field_name(cls, doc_cls, parts): + """Translate a field attribute name to a database field name. + """ + return [field.name for field in QuerySet._lookup_field(doc_cls, parts)] + @classmethod def _transform_query(cls, _doc_cls=None, **query): """Transform a query from Django-style format to Mongo format. @@ -87,11 +94,22 @@ class QuerySet(object): op = None if parts[-1] in operators: op = parts.pop() - value = {'$' + op: value} - # Switch field names to proper names [set in Field(name='foo')] if _doc_cls: - parts = QuerySet._translate_field_name(_doc_cls, parts) + # Switch field names to proper names [set in Field(name='foo')] + fields = QuerySet._lookup_field(_doc_cls, parts) + parts = [field.name for field in fields] + + # Convert value to proper value + field = fields[-1] + if op in (None, 'neq', 'gt', 'gte', 'lt', 'lte'): + value = field.prepare_query_value(value) + elif op in ('in', 'nin', 'all'): + # 'in', 'nin' and 'all' require a list of values + value = [field.prepare_query_value(v) for v in value] + + if op: + value = {'$' + op: value} key = '.'.join(parts) if op is None or key not in mongo_query: diff --git a/tests/document.py b/tests/document.py index 31ae0999..47d9c5c4 100644 --- a/tests/document.py +++ b/tests/document.py @@ -321,6 +321,8 @@ class DocumentTest(unittest.TestCase): comments = ListField(EmbeddedDocumentField(Comment)) tags = ListField(StringField()) + BlogPost.drop_collection() + post = BlogPost(content='Went for a walk today...') post.tags = tags = ['fun', 'leisure'] comments = [Comment(content='Good for you'), Comment(content='Yay.')] diff --git a/tests/queryset.py b/tests/queryset.py index 13e38e4d..e7e79ccc 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -300,6 +300,32 @@ class QuerySetTest(unittest.TestCase): BlogPost.drop_collection() + def test_query_value_conversion(self): + """Ensure that query values are properly converted when necessary. + """ + class BlogPost(Document): + author = ReferenceField(self.Person) + + BlogPost.drop_collection() + + person = self.Person(name='test', age=30) + person.save() + + post = BlogPost(author=person) + post.save() + + # Test that query may be performed by providing a document as a value + # while using a ReferenceField's name - the document should be + # converted to an DBRef, which is legal, unlike a Document object + post_obj = BlogPost.objects(author=person).first() + self.assertEqual(post.id, post_obj.id) + + # Test that lists of values work when using the 'in', 'nin' and 'all' + post_obj = BlogPost.objects(author__in=[person]).first() + self.assertEqual(post.id, post_obj.id) + + BlogPost.drop_collection() + def tearDown(self): self.Person.drop_collection() From f86496b545579e222df2bb85fe9e30dee58808c3 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Wed, 6 Jan 2010 03:23:02 +0000 Subject: [PATCH 05/14] Bump to v0.1.2 --- docs/changelog.rst | 9 +++++++++ docs/conf.py | 2 +- mongoengine/__init__.py | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 9a89ec17..96f7cbf9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,6 +2,15 @@ Changelog ========= +Changes in v0.1.2 +================= +- Query values may be processed before before being used in queries +- Made connections lazy +- Fixed bug in Document dictionary-style access +- Added BooleanField +- Added Document.reload method + + Changes in v0.1.1 ================= - Documents may now use capped collections diff --git a/docs/conf.py b/docs/conf.py index bc6725d2..a40a25ff 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ sys.path.append(os.path.abspath('..')) extensions = ['sphinx.ext.autodoc'] # Add any paths that contain templates here, relative to this directory. -templates_path = ['.templates'] +templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py index cf20d9ca..7a1134f7 100644 --- a/mongoengine/__init__.py +++ b/mongoengine/__init__.py @@ -12,7 +12,7 @@ __all__ = (document.__all__ + fields.__all__ + connection.__all__ + __author__ = 'Harry Marr' -VERSION = (0, 1, 1) +VERSION = (0, 1, 2) def get_version(): version = '%s.%s' % (VERSION[0], VERSION[1]) From 2e74c9387885164b21676e2029621e5e9b8e55a3 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 15:24:52 +0000 Subject: [PATCH 06/14] Minor bugfixes --- mongoengine/base.py | 4 +--- mongoengine/queryset.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index d4412e2a..ec70e5ad 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -202,9 +202,7 @@ class BaseDocument(object): return all_subclasses def __iter__(self): - # Use _data rather than _fields as iterator only looks at names so - # values don't need to be converted to Python types - return iter(self._data) + return iter(self._fields) def __getitem__(self, name): """Dictionary-style field access, return a field's value if present. diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 7b3f7c6b..95f02891 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -132,7 +132,7 @@ class QuerySet(object): """Retrieve the object matching the id provided. """ if not isinstance(object_id, pymongo.objectid.ObjectId): - object_id = pymongo.objectid.ObjectId(object_id) + object_id = pymongo.objectid.ObjectId(str(object_id)) result = self._collection.find_one(object_id) if result is not None: From a6d64b20106827f1a6a5fbc4d367182511e4c2be Mon Sep 17 00:00:00 2001 From: blackbrrr Date: Wed, 6 Jan 2010 04:28:24 +0800 Subject: [PATCH 07/14] added meta support for indexes ensured at call-time --- mongoengine/base.py | 4 ++++ mongoengine/queryset.py | 20 +++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mongoengine/base.py b/mongoengine/base.py index ec70e5ad..4b7c6b33 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -154,8 +154,12 @@ class TopLevelDocumentMetaclass(DocumentMetaclass): 'allow_inheritance': True, 'max_documents': None, 'max_size': None, + 'indexes': [] # indexes to be ensured at runtime } + + # Apply document-defined meta options meta.update(attrs.get('meta', {})) + # Only simple classes - direct subclasses of Document - may set # allow_inheritance to False if not simple_class and not meta['allow_inheritance']: diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 95f02891..75a92751 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -30,18 +30,32 @@ class QuerySet(object): """ if isinstance(key_or_list, basestring): # single-field indexes needn't specify a direction - if key_or_list.startswith("-"): + if key_or_list.startswith("-") or key_or_list.startswith("+"): key_or_list = key_or_list[1:] self._collection.ensure_index(key_or_list) elif isinstance(key_or_list, (list, tuple)): - print key_or_list - self._collection.ensure_index(key_or_list) + index_list = [] + for key in key_or_list: + if key.startswith("-"): + index_list.append((key[1:], pymongo.DESCENDING)) + else: + if key.startswith("+"): + key = key[1:] + index_list.append((key, pymongo.ASCENDING)) + self._collection.ensure_index(index_list) return self def __call__(self, **query): """Filter the selected documents by calling the :class:`~mongoengine.QuerySet` with a query. """ + + # ensure document-defined indexes are created + if self._document._meta['indexes']: + for key_or_list in self._document._meta['indexes']: + # print "key", key_or_list + self.ensure_index(key_or_list) + query = QuerySet._transform_query(_doc_cls=self._document, **query) self._query.update(query) return self From 4c93e2945c0e24ab680b8f780c3c96bf20d8d4d1 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 15:46:52 +0000 Subject: [PATCH 08/14] Added test for meta[indexes] --- mongoengine/document.py | 2 -- mongoengine/queryset.py | 2 +- tests/document.py | 26 ++++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index e26c5ed0..d7154404 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -67,8 +67,6 @@ class Document(BaseDocument): def reload(self): """Reloads all attributes from the database. """ - #object_id = self._fields['id'].to_mongo(self.id) - #obj = self.__class__.objects(id=object_id).first() obj = self.__class__.objects(id=self.id).first() for field in self._fields: setattr(self, field, getattr(obj, field)) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 75a92751..96c23594 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -25,7 +25,7 @@ class QuerySet(object): self._query = {'_types': self._document._class_name} self._cursor_obj = None - def ensure_index(self, key_or_list, direction=None): + def ensure_index(self, key_or_list): """Ensure that the given indexes are in place. """ if isinstance(key_or_list, basestring): diff --git a/tests/document.py b/tests/document.py index 47d9c5c4..a6abca89 100644 --- a/tests/document.py +++ b/tests/document.py @@ -221,6 +221,32 @@ class DocumentTest(unittest.TestCase): Log.drop_collection() + def test_indexes(self): + """Ensure that indexes are used when meta[indexes] is specified. + """ + class BlogPost(Document): + date = DateTimeField(default=datetime.datetime.now) + category = StringField() + meta = { + 'indexes': [ + '-date', + ('category', '-date') + ], + } + + BlogPost.drop_collection() + + info = BlogPost.objects._collection.index_information() + self.assertEqual(len(info), 0) + + BlogPost.objects() + info = BlogPost.objects._collection.index_information() + self.assertTrue([('category', 1), ('date', -1)] in info.values()) + # Even though descending order was specified, single-key indexes use 1 + self.assertTrue([('date', 1)] in info.values()) + + BlogPost.drop_collection() + def test_creation(self): """Ensure that document may be created using keyword arguments. """ From e0a546000da41241062e8cb6349c85796e8c7bd6 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 18:56:28 +0000 Subject: [PATCH 09/14] Added Django authentication backend --- mongoengine/django/__init__.py | 0 mongoengine/django/auth.py | 78 ++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 mongoengine/django/__init__.py create mode 100644 mongoengine/django/auth.py diff --git a/mongoengine/django/__init__.py b/mongoengine/django/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/mongoengine/django/auth.py b/mongoengine/django/auth.py new file mode 100644 index 00000000..c9180596 --- /dev/null +++ b/mongoengine/django/auth.py @@ -0,0 +1,78 @@ +from mongoengine import * + +from django.utils.hashcompat import md5_constructor, sha_constructor +from django.utils.encoding import smart_str +from django.contrib.auth.models import AnonymousUser + +import datetime + +REDIRECT_FIELD_NAME = 'next' + +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') + + +class User(Document): + """A User document that aims to mirror most of the API specified by Django + at http://docs.djangoproject.com/en/dev/topics/auth/#users + """ + username = StringField(max_length=30, required=True) + first_name = StringField(max_length=30) + last_name = StringField(max_length=30) + email = StringField() + password = StringField(max_length=128) + is_staff = BooleanField(default=False) + is_active = BooleanField(default=True) + is_superuser = BooleanField(default=False) + last_login = DateTimeField(default=datetime.datetime.now) + + def get_full_name(self): + full_name = u'%s %s' % (self.first_name or '', self.last_name or '') + return full_name.strip() + + def is_anonymous(self): + return False + + def is_authenticated(self): + return True + + def set_password(self, raw_password): + from random import random + algo = 'sha1' + salt = get_hexdigest(algo, str(random()), str(random()))[:5] + hash = get_hexdigest(algo, salt, raw_password) + self.password = '%s$%s$%s' % (algo, salt, hash) + + def check_password(self, raw_password): + algo, salt, hash = self.password.split('$') + return hash == get_hexdigest(algo, salt, raw_password) + + +class MongoEngineBackend(object): + """Authenticate using MongoEngine and mongoengine.django.auth.User. + """ + + def authenticate(self, username=None, password=None): + user = User.objects(username=username).first() + if user: + if password and user.check_password(password): + return user + return None + + def get_user(self, user_id): + return User.objects.with_id(user_id) + + +def get_user(userid): + """Returns a User object from an id (User.id). Django's equivalent takes + request, but taking an id instead leaves it up to the developer to store + the id in any way they want (session, signed cookie, etc.) + """ + if not userid: + return AnonymousUser() + return MongoEngineBackend().get_user(userid) or AnonymousUser() From d48296eacc4dad0c95cb563889c8bb24c0bb3977 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 22:25:26 +0000 Subject: [PATCH 10/14] Added create_user method to Django User model --- mongoengine/django/auth.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mongoengine/django/auth.py b/mongoengine/django/auth.py index c9180596..cc88ed0a 100644 --- a/mongoengine/django/auth.py +++ b/mongoengine/django/auth.py @@ -52,6 +52,13 @@ class User(Document): algo, salt, hash = self.password.split('$') return hash == get_hexdigest(algo, salt, raw_password) + @classmethod + def create_user(cls, username, password, email=None): + user = User(username=username, email=email) + user.set_password(password) + user.save() + return user + class MongoEngineBackend(object): """Authenticate using MongoEngine and mongoengine.django.auth.User. From b7e8108edd1e271dd4e552ccf3aedb1a9a24fb72 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 22:48:39 +0000 Subject: [PATCH 11/14] Added docs about using MongoEngine with Django --- docs/changelog.rst | 8 ++++++++ docs/index.rst | 1 + mongoengine/django/auth.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index 96f7cbf9..e6f11571 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,6 +2,14 @@ Changelog ========= +Changes is v0.1.3 +================= +- Added Django authentication backend +- Added Document.meta support for indexes, which are ensured just before + querying takes place +- A few minor bugfixes + + Changes in v0.1.2 ================= - Query values may be processed before before being used in queries diff --git a/docs/index.rst b/docs/index.rst index 5a04de7a..40672f49 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -16,6 +16,7 @@ The source is available on `GitHub `_. tutorial userguide apireference + django changelog Indices and tables diff --git a/mongoengine/django/auth.py b/mongoengine/django/auth.py index cc88ed0a..5789d208 100644 --- a/mongoengine/django/auth.py +++ b/mongoengine/django/auth.py @@ -32,6 +32,8 @@ class User(Document): last_login = DateTimeField(default=datetime.datetime.now) def get_full_name(self): + """Returns the users first and last names, separated by a space. + """ full_name = u'%s %s' % (self.first_name or '', self.last_name or '') return full_name.strip() @@ -42,6 +44,10 @@ class User(Document): return True def set_password(self, raw_password): + """Sets the user's password - always use this rather than directly + assigning to :attr:`~mongoengine.django.auth.User.password` as the + password is hashed before storage. + """ from random import random algo = 'sha1' salt = get_hexdigest(algo, str(random()), str(random()))[:5] @@ -49,11 +55,19 @@ class User(Document): self.password = '%s$%s$%s' % (algo, salt, hash) def check_password(self, raw_password): + """Checks the user's password against a provided password - always use + this rather than directly comparing to + :attr:`~mongoengine.django.auth.User.password` as the password is + hashed before storage. + """ algo, salt, hash = self.password.split('$') return hash == get_hexdigest(algo, salt, raw_password) @classmethod def create_user(cls, username, password, email=None): + """Create (and save) a new user with the given username, password and + email address. + """ user = User(username=username, email=email) user.set_password(password) user.save() From ef5815e4a5c7e55c9d2d03995c5a73a3c4cb0cfe Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 22:49:24 +0000 Subject: [PATCH 12/14] Bump to v0.1.3 --- mongoengine/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py index 7a1134f7..8298a79c 100644 --- a/mongoengine/__init__.py +++ b/mongoengine/__init__.py @@ -12,7 +12,7 @@ __all__ = (document.__all__ + fields.__all__ + connection.__all__ + __author__ = 'Harry Marr' -VERSION = (0, 1, 2) +VERSION = (0, 1, 3) def get_version(): version = '%s.%s' % (VERSION[0], VERSION[1]) From 960aea2fd4aba71678b1027739b321aa72baf535 Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Thu, 7 Jan 2010 23:54:57 +0000 Subject: [PATCH 13/14] Added indexes and Django use to docs --- docs/django.rst | 29 +++++++++++++++++++++++++++++ docs/userguide.rst | 16 ++++++++++++++++ mongoengine/document.py | 5 +++++ 3 files changed, 50 insertions(+) create mode 100644 docs/django.rst diff --git a/docs/django.rst b/docs/django.rst new file mode 100644 index 00000000..57684365 --- /dev/null +++ b/docs/django.rst @@ -0,0 +1,29 @@ +============================= +Using MongoEngine with Django +============================= + +Connecting +========== +In your **settings.py** file, ignore the standard database settings (unless you +also plan to use the ORM in your project), and instead call +:func:`~mongoengine.connect` somewhere in the settings module. + +Authentication +============== +MongoEngine includes a Django authentication backend, which uses MongoDB. The +:class:`~mongoengine.django.auth.User` model is a MongoEngine +:class:`~mongoengine.Document`, but implements most of the methods and +attributes that the standard Django :class:`User` model does - so the two are +moderately compatible. Using this backend will allow you to store users in +MongoDB but still use many of the Django authentication infrastucture (such as +the :func:`login_required` decorator and the :func:`authenticate` function). To +enable the MongoEngine auth backend, add the following to you **settings.py** +file:: + + AUTHENTICATION_BACKENDS = ( + 'mongoengine.django.auth.MongoEngineBackend', + ) + +The :mod:`~mongoengine.django.auth` module also contains a +:func:`~mongoengine.django.auth.get_user` helper function, that takes a user's +:attr:`id` and returns a :class:`~mongoengine.django.auth.User` object. diff --git a/docs/userguide.rst b/docs/userguide.rst index c1dd13ae..602d51b6 100644 --- a/docs/userguide.rst +++ b/docs/userguide.rst @@ -168,6 +168,22 @@ The following example shows a :class:`Log` document that will be limited to ip_address = StringField() meta = {'max_documents': 1000, 'max_size': 2000000} +Indexes +------- +You can specify indexes on collections to make querying faster. This is done +by creating a list of index specifications called :attr:`indexes` in the +:attr:`~Document.meta` dictionary, where an index specification may either be +a single field name, or a tuple containing multiple field names. A direction +may be specified on fields by prefixing the field name with a **+** or a **-** +sign. Note that direction only matters on multi-field indexes. :: + + class Page(Document): + title = StringField() + rating = StringField() + meta = { + 'indexes': ['title', ('title', '-rating')] + } + Document inheritance -------------------- To create a specialised type of a :class:`~mongoengine.Document` you have diff --git a/mongoengine/document.py b/mongoengine/document.py index d7154404..1afa1e41 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -44,6 +44,11 @@ class Document(BaseDocument): maximum size of the collection in bytes. If :attr:`max_size` is not specified and :attr:`max_documents` is, :attr:`max_size` defaults to 10000000 bytes (10MB). + + Indexes may be created by specifying :attr:`indexes` in the :attr:`meta` + dictionary. The value should be a list of field names or tuples of field + names. Index direction may be specified by prefixing the field names with + a **+** or **-** sign. """ __metaclass__ = TopLevelDocumentMetaclass From eb3e6963fafbbe6d67edba87b20a3de8d8c2f42f Mon Sep 17 00:00:00 2001 From: Harry Marr Date: Fri, 8 Jan 2010 00:15:20 +0000 Subject: [PATCH 14/14] Index specs now use proper field names --- mongoengine/queryset.py | 23 +++++++++++++++-------- tests/document.py | 6 +++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 96c23594..18a26f9d 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -30,18 +30,23 @@ class QuerySet(object): """ if isinstance(key_or_list, basestring): # single-field indexes needn't specify a direction - if key_or_list.startswith("-") or key_or_list.startswith("+"): + if key_or_list.startswith(("-", "+")): key_or_list = key_or_list[1:] - self._collection.ensure_index(key_or_list) + # Use real field name + key = QuerySet._translate_field_name(self._document, key_or_list) + self._collection.ensure_index(key) elif isinstance(key_or_list, (list, tuple)): index_list = [] for key in key_or_list: + # Get direction from + or - + direction = pymongo.ASCENDING if key.startswith("-"): - index_list.append((key[1:], pymongo.DESCENDING)) - else: - if key.startswith("+"): + direction = pymongo.DESCENDING + if key.startswith(("+", "-")): key = key[1:] - index_list.append((key, pymongo.ASCENDING)) + # Use real field name + key = QuerySet._translate_field_name(self._document, key) + index_list.append((key, direction)) self._collection.ensure_index(index_list) return self @@ -89,10 +94,12 @@ class QuerySet(object): return fields @classmethod - def _translate_field_name(cls, doc_cls, parts): + def _translate_field_name(cls, doc_cls, field, sep='.'): """Translate a field attribute name to a database field name. """ - return [field.name for field in QuerySet._lookup_field(doc_cls, parts)] + parts = field.split(sep) + parts = [f.name for f in QuerySet._lookup_field(doc_cls, parts)] + return '.'.join(parts) @classmethod def _transform_query(cls, _doc_cls=None, **query): diff --git a/tests/document.py b/tests/document.py index a6abca89..5783ae05 100644 --- a/tests/document.py +++ b/tests/document.py @@ -225,7 +225,7 @@ class DocumentTest(unittest.TestCase): """Ensure that indexes are used when meta[indexes] is specified. """ class BlogPost(Document): - date = DateTimeField(default=datetime.datetime.now) + date = DateTimeField(name='addDate', default=datetime.datetime.now) category = StringField() meta = { 'indexes': [ @@ -241,9 +241,9 @@ class DocumentTest(unittest.TestCase): BlogPost.objects() info = BlogPost.objects._collection.index_information() - self.assertTrue([('category', 1), ('date', -1)] in info.values()) + self.assertTrue([('category', 1), ('addDate', -1)] in info.values()) # Even though descending order was specified, single-key indexes use 1 - self.assertTrue([('date', 1)] in info.values()) + self.assertTrue([('addDate', 1)] in info.values()) BlogPost.drop_collection()