Compare commits

..

118 Commits
0.7 ... v0.7.10

Author SHA1 Message Date
Ross Lawley
6affbbe865 Update changelog location 2013-04-19 13:08:46 +00:00
Ross Lawley
e3600ef4de Updated version 2013-04-19 12:53:46 +00:00
Ross Lawley
6dcd7006d0 Fix test 2013-04-19 12:47:19 +00:00
Ross Lawley
3f49923298 Update AUTHORS and changelog (#278) 2013-04-18 13:21:36 +00:00
Ross Lawley
c277be8b6b Merge remote-tracking branch 'origin/pr/278' 2013-04-18 13:17:08 +00:00
Ross Lawley
03bfd01862 Updated field iteration for py2.5 2013-04-17 15:54:32 +00:00
Ross Lawley
dcf3c86dce Using "id" in data not "_id" as its a mapping of fieldnames (#255) 2013-04-17 15:07:57 +00:00
daniil
c2d77f51bb test for #278 issue 2013-04-17 12:14:07 +04:00
Ross Lawley
b4d87d9128 Updated changelog 2013-04-16 20:50:34 +00:00
Ross Lawley
4401a309ee Merge remote-tracking branch 'origin/pr/268' 2013-04-16 20:49:33 +00:00
Ross Lawley
b562e209d1 Updated EmailField length to support long domains (#243) 2013-04-16 20:46:02 +00:00
Ross Lawley
3a85422e8f Added 64-bit integer support (#251) 2013-04-16 20:35:29 +00:00
Ross Lawley
e45397c975 Merge remote-tracking branch 'origin/pr/251' 2013-04-16 20:34:32 +00:00
Ross Lawley
1f9ec0c888 Added Django sessions TTL support (#224) 2013-04-16 20:30:40 +00:00
Ross Lawley
f8ee470e70 Merge remote-tracking branch 'origin/pr/224' 2013-04-16 20:28:24 +00:00
Ross Lawley
d02de0798f Documentation fix explaining adding a dummy backend for django (#172) 2013-04-16 20:26:23 +00:00
Ross Lawley
6fe074fb13 Fixed issue with numerical keys in MapField(EmbeddedDocumentField()) (#240) 2013-04-16 20:21:11 +00:00
Ross Lawley
4db339c5f4 Merge remote-tracking branch 'origin/pr/240' 2013-04-16 20:19:23 +00:00
Ross Lawley
a525764359 Fixed clearing _changed_fields for complex nested embedded documents (#237, #239, #242) 2013-04-16 20:12:01 +00:00
Ross Lawley
f970d5878a Merge remote-tracking branch 'origin/pr/242'
Conflicts:
	tests/test_document.py
2013-04-16 20:07:03 +00:00
Daniil Sharou
cc0a2cbc6f fix UnicodeEncodeError for dbref
Fix "UnicodeEncodeError: 'ascii' codec can't encode character ..." error in case dbref contains non-ascii characters
2013-04-16 22:34:33 +04:00
Daniil Sharou
add0b463f5 fix UnicodeEncodeError for dbref
Fix "UnicodeEncodeError: 'ascii' codec can't encode character ..." error in case dbref contains non-ascii characters
2013-04-16 21:12:57 +04:00
Ross Lawley
d80b1a7749 Test clean up (#255) 2013-04-15 08:03:51 +00:00
Ross Lawley
6186691259 Updated changelog and AUTHORS (#255) 2013-04-15 08:01:24 +00:00
Ross Lawley
b451cc567d Return '_id' as the key for document.id in _data dictionary
* Re #146
Conflicts:

	mongoengine/base.py
2013-04-15 08:00:14 +00:00
Ross Lawley
757ff31661 Updated changelog 2013-04-15 07:53:57 +00:00
Ross Lawley
97a98f0045 Only mark a field as changed if the value has changed (#258) 2013-04-15 07:52:04 +00:00
Ross Lawley
8f05896bc9 Merge remote-tracking branch 'origin/pr/258'
Conflicts:
	AUTHORS
2013-04-15 07:51:26 +00:00
Ross Lawley
da7a8939df Also check if a TopLevelMetaclass instance (#261) 2013-04-15 07:41:04 +00:00
Ross Lawley
b6977a88ea Explicitly check for Document instances when dereferencing (#261) 2013-04-15 07:32:04 +00:00
Ross Lawley
eafbc7f20d Merge remote-tracking branch 'origin/pr/261' 2013-04-15 07:31:02 +00:00
Ross Lawley
c9a5710554 Fixed order_by chaining issue (#265) 2013-04-12 15:56:40 +00:00
Ross Lawley
f10e946896 Merge remote-tracking branch 'origin/pr/265' into 265 2013-04-12 14:27:37 +00:00
Ross Lawley
2f19b22bb2 Added dereference support for tuples (#250) 2013-04-12 14:25:43 +00:00
Ross Lawley
d134e11c6d Merge remote-tracking branch 'origin/pr/250' 2013-04-12 14:24:18 +00:00
Ross Lawley
63edd16a92 Resolve field name to db field name when using distinct(#260, #264, #269) 2013-04-12 14:20:44 +00:00
Ross Lawley
37740dc010 Added kwargs to doc.save to help interop with django (#223, #270) 2013-04-12 14:05:08 +00:00
Ross Lawley
836dc96f67 Updated changelog 2013-04-12 12:56:15 +00:00
Ross Lawley
49a7542b14 Fixing cloning in python 3 2013-04-12 12:55:03 +00:00
Ross Lawley
a84ffce5a0 Merge remote-tracking branch 'origin/pr/272' 2013-04-12 10:55:09 +00:00
Ross Lawley
210b3e5192 Merge remote-tracking branch 'origin/pr/254' 2013-04-12 10:41:07 +00:00
Ross Lawley
5f1d5ea056 Try and fix wobbly test 2013-04-12 10:35:09 +00:00
Ross Lawley
19a7372ff9 Fix test_require for Django 2013-04-12 10:32:50 +00:00
Ross Lawley
cc5b60b004 Updated pymongo versions and pillow wont work 2013-04-12 10:30:52 +00:00
Ross Lawley
b06f9dbf8d Travis travis travis 2013-04-12 10:02:55 +00:00
Ross Lawley
d9b8ee7895 next test 2013-04-11 15:47:53 +00:00
Ross Lawley
e9ff655b0e Trying to fix travis 2013-04-11 15:00:49 +00:00
Ross Lawley
669d21a114 Merge pull request #241 from benoitlouy/fixing_travis_build
Fixing travis build
2013-04-11 04:51:51 -07:00
Kristinn Örn Sigurðsson
7e980a16d0 Don't run unset on IntField if the value is 0 (zero).
The IntField in unset if the IntField value doesn't validate to "truthify" (therefore, is
set as 0) and the default value of the IntField in question is 0.
This is not a logical functionality in my opinion. Take this example.
You have an IntField that is a counter which can be incremented and
decremented. This counter has the default value of 0 and is a required
field. Every time the counter reaches 0, the field is unset.
2013-04-05 11:01:46 +02:00
Kristinn Örn Sigurðsson
47df8deb58 Fix the implementation. 2013-04-04 17:30:21 +02:00
Kristinn Örn Sigurðsson
dd006a502e Don't run unset on IntField if the value is 0 (zero). 2013-04-04 17:09:05 +02:00
Alice Bevan-McGregor
07d3e52e6a Tests for construction using positional parameters. 2013-04-03 15:03:33 -04:00
Alice Bevan-McGregor
fc1ce6d39b Allow construction of document instances using positional arguments. 2013-04-03 15:00:51 -04:00
Alice Bevan-McGregor
32d5c0c946 Store ordered list of field names, and return the ordered list when iterating a document instance. 2013-04-03 15:00:34 -04:00
Stefan Wojcik
dfabfce01b show that order_by followed by limit works, but not the other way around 2013-04-01 17:17:01 -07:00
Stefan Wojcik
74f3f4eb15 more ordering unit tests 2013-04-01 16:17:17 -07:00
Paul Swartz
20cb0285f0 explicitly check for Document instances when dereferencing
In particular, `collections.namedtuple` instances also have a `_fields`
attribute which confuses the dereferencing.
2013-03-27 14:53:47 -04:00
Paul Swartz
faf840f924 only mark a field as changed if the value has changed
Prevents spurious changes from being recorded.
2013-03-25 11:00:20 -04:00
Aleksandr Sorokoumov
165bea5bb9 QuerySet chaining test was supplemented with ReferenceField
chaining test
2013-03-18 12:32:49 +01:00
Aleksandr Sorokoumov
f7515cfca8 add myself to AUTHORS 2013-03-18 12:22:55 +01:00
Jaepil Jeong
a762a10dec Revert "Fixed potential overflow error."
This reverts commit 67182713d9.
2013-03-18 19:30:04 +09:00
Aleksandr Sorokoumov
a192029901 ReferenceField query chaining bug fixed. 2013-03-16 16:54:26 +01:00
Jaepil Jeong
67182713d9 Fixed potential overflow error. 2013-03-15 00:12:48 +09:00
Jaepil Jeong
e9464e32db Added test cases for LongField. 2013-03-14 23:59:50 +09:00
Jaepil Jeong
2d6ae16912 Added LongField to support 64-bit integer type. 2013-03-14 23:25:22 +09:00
Russ Weeks
f9cd8b1841 added unit test for dereference patch 2013-03-12 12:45:38 -07:00
Russ Weeks
41a698b442 Changed dereference.py to keep tuples as tuples 2013-03-12 10:28:29 -07:00
Ross Lawley
9f58bc9207 Merge pull request #245 from zmolodchenko/patch-1
fix error reporting, where choices is list of flat values
2013-03-12 01:13:16 -07:00
zmolodchenko
d36f6e7f24 fix error reporting, where choices is list of flat values 2013-03-09 21:08:10 +02:00
Ross Lawley
eeb672feb9 Removing custom layout 2013-03-07 15:37:01 +00:00
Ross Lawley
063a162ce0 Merge branch 'master' of github.com:MongoEngine/mongoengine 2013-03-07 15:31:48 +00:00
Ross Lawley
3e4a900279 Adding google analytics 2013-03-07 15:25:18 +00:00
benoitlouy
43327ea4e1 Add testcase for issue #237 2013-03-01 07:38:28 -05:00
benoitlouy
0d2e84b16b Fix for issue #237: clearing changed fields recursively in EmbeddedDocuments after saving a Document 2013-02-28 00:37:34 -05:00
Benoit Louy
3c78757778 fix travis build: builds were failing because libz.so location changed. 2013-02-26 09:55:29 -05:00
Loic Raucy
d0245bb5ba Fixed #238: dictfields handle numerical strings indexes. 2013-02-26 11:26:51 +01:00
Loic Raucy
3477b0107a Added regression test for numerical string keys. 2013-02-26 11:26:38 +01:00
Ross Lawley
8df9ff90cb Update LICENSE 2013-02-14 08:26:36 +00:00
hellysmile
d6b4ca7a98 Fix docs quotes 2013-02-01 04:19:55 +02:00
hellysmile
2e18199eb2 Django sessions TTL support 2013-02-01 04:17:16 +02:00
Ross Lawley
e508625935 Update docs/upgrade.rst 2013-01-14 16:37:40 +00:00
Ross Lawley
87c965edd3 Fixing PY3.3 test cases 2013-01-10 11:08:07 +00:00
Ross Lawley
06681a453f python 3.3 test fixes 2013-01-09 13:15:21 +00:00
Ross Lawley
8e038dd563 Updated travis python-3.1 no longer supported 2013-01-09 08:51:45 +00:00
Ross Lawley
e537369d98 Trying to get travis to build the 0.8 branch 2013-01-04 14:02:34 +00:00
Ross Lawley
25cdf16cc0 Updated travis 2012-12-19 12:37:37 +00:00
Ross Lawley
74343841e4 Merge branch 'master' of github.com:MongoEngine/mongoengine 2012-12-10 15:17:05 +00:00
Ross Lawley
3b3738b36b 0.7.9 2012-12-10 15:16:31 +00:00
Ross Lawley
b15c3f6a3f Update AUTHORS
Sorry Jorge!
2012-12-10 10:33:55 +00:00
Ross Lawley
2459f9b0aa Merge pull request #195 from balboah/patch-1
Corrected user guide link in README
2012-12-10 02:24:02 -08:00
Johnny Bergström
6ff1bd9b3c Corrected user guide link in README 2012-12-10 11:01:08 +01:00
Ross Lawley
1bc2d2ec37 Version Bump 2012-12-10 09:54:20 +00:00
Ross Lawley
d7fd6a4628 Merge branch 'ref_field_to_mongo_bug' of https://github.com/yakxxx/mongoengine
Conflicts:
	AUTHORS
2012-12-10 09:20:20 +00:00
Ross Lawley
9236f365fa Fix sequence fields in embedded documents (MongoEngine/mongoengine#166) 2012-12-10 09:11:31 +00:00
Ross Lawley
90d22c2a28 Update AUTHORS & Changelog (MongoEngine/mongoengine#176) 2012-12-10 08:50:21 +00:00
Ross Lawley
c9f6e6b62a Merge branch 'order-by-chaining' of https://github.com/pteichman/mongoengine 2012-12-10 08:49:19 +00:00
Ross Lawley
260d9377f5 Updated Changelog 2012-12-10 08:26:42 +00:00
Ross Lawley
22d1ce6319 Merge branch 'master' of https://github.com/AdrianScott/mongoengine 2012-12-10 08:25:37 +00:00
Ross Lawley
6997e02476 Fixed EmailField so can add extra validation
(MongoEngine/mongoengine#173, MongoEngine/mongoengine#174, MongoEngine/mongoengine#187)
2012-12-10 08:23:41 +00:00
Ross Lawley
155d79ff4d Merge branch 'master' of https://github.com/shaunduncan/mongoengine 2012-12-10 08:22:25 +00:00
Ross Lawley
452cd125fa Updated Changelog 2012-12-10 08:11:35 +00:00
Jorge Bastida
e62c35b040 Add more tests 2012-12-07 16:21:31 +00:00
Jorge Bastida
d5ec3c6a31 Add as_pymongo to __getitem__ and in_bulk 2012-12-07 15:59:09 +00:00
Jorge Bastida
ad983dc279 Implement _get_as_pymongo 2012-12-07 15:42:10 +00:00
Adrian Scott
bb15bf8d13 Update AUTHORS
added me (Adrian Scott, issues 180, 181)
2012-12-07 10:02:12 -05:00
Jorge Bastida
94adc207ad First as_pymongo implementation 2012-12-07 11:20:27 +00:00
Shaun Duncan
376d1c97ab EmailField should honor StringField validation as well 2012-12-04 13:08:49 -05:00
Adrian Scott
4fe87b40da added comments 2012-11-29 21:49:54 -05:00
Adrian Scott
b10d76cf4b split line to meet 79 char max line limit 2012-11-29 21:28:03 -05:00
Adrian Scott
3bdc9a2f09 session collection parameter; encoding optional
Added a parameter for the name of the session collection; Added the
option to not encode session_data, which is useful for expiring sessions
of users when a password is changed, etc.; these upgrades provided by
SocialVilla Inc.
2012-11-29 20:53:09 -05:00
Peter Teichman
9d52e18659 Don't freeze the current query state when calling .order_by()
This changes order_by() to eliminate its reference to
self._cursor. This meant that any parameters built by QuerySet
that followed an order_by() clause were ignored.
2012-11-27 10:55:55 -05:00
Ross Lawley
653c4259ee Fixed handling for old style types 2012-11-27 11:59:34 +00:00
Ross Lawley
9f5ab8149f Adding some debugging 2012-11-27 11:06:55 +00:00
Ross Lawley
66c6d14f7a Trying to fix seesaw test on travis 2012-11-27 10:50:22 +00:00
Ross Lawley
2c0fc142a3 Updated travis.yml 2012-11-26 21:04:06 +00:00
yak
0da2dfd191 addition to AUTHORS 2012-11-13 13:04:05 +01:00
yak
787fc1cd8b bug fix for RefferenceField.to_mongo when dbref=False 2012-11-13 13:02:07 +01:00
Ross Lawley
c31488add9 Version bump 2012-11-05 11:14:02 +00:00
21 changed files with 767 additions and 109 deletions

View File

@@ -2,20 +2,18 @@
language: python
services: mongodb
python:
- 2.5
- 2.6
- 2.7
- 3.1
- 3.2
- "2.5"
- "2.6"
- "2.7"
- "3.2"
- "3.3"
env:
- PYMONGO=dev
- PYMONGO=2.3
- PYMONGO=2.2
- PYMONGO=2.5
- PYMONGO=2.4.2
install:
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then sudo apt-get install zlib1g zlib1g-dev; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib/; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then pip install PIL --use-mirrors ; true; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then pip install PIL --use-mirrors ; true; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then cp /usr/lib/*/libz.so $VIRTUAL_ENV/lib/; fi
- if [[ $TRAVIS_PYTHON_VERSION == '2.'* ]]; then pip install pil --use-mirrors ; true; fi
- if [[ $PYMONGO == 'dev' ]]; then pip install https://github.com/mongodb/mongo-python-driver/tarball/master; true; fi
- if [[ $PYMONGO != 'dev' ]]; then pip install pymongo==$PYMONGO --use-mirrors; true; fi
- python setup.py install
@@ -26,4 +24,4 @@ notifications:
branches:
only:
- master
- 0.7
- "0.8"

17
AUTHORS
View File

@@ -106,7 +106,7 @@ that much better:
* Adam Reeve
* Anthony Nemitz
* deignacio
* shaunduncan
* Shaun Duncan
* Meir Kriheli
* Andrey Fedoseev
* aparajita
@@ -124,3 +124,18 @@ that much better:
* Stefan Wójcik
* dimonb
* Garry Polley
* Adrian Scott
* Peter Teichman
* Jakub Kot
* Jorge Bastida
* Aleksandr Sorokoumov
* Yohan Graterol
* bool-dev
* Russ Weeks
* Paul Swartz
* Sundar Raman
* Benoit Louy
* lraucy
* hellysmile
* Jaepil Jeong
* Daniil Sharou

View File

@@ -1,4 +1,4 @@
Copyright (c) 2009-2012 See AUTHORS
Copyright (c) 2009 See AUTHORS
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation

View File

@@ -14,7 +14,7 @@ About
MongoEngine is a Python Object-Document Mapper for working with MongoDB.
Documentation available at http://mongoengine-odm.rtfd.org - there is currently
a `tutorial <http://readthedocs.org/docs/mongoengine-odm/en/latest/tutorial.html>`_, a `user guide
<http://readthedocs.org/docs/mongoengine-odm/en/latest/userguide.html>`_ and an `API reference
<https://mongoengine-odm.readthedocs.org/en/latest/guide/index.html>`_ and an `API reference
<http://readthedocs.org/docs/mongoengine-odm/en/latest/apireference.html>`_.
Installation

View File

@@ -2,7 +2,45 @@
Changelog
=========
Changes in 0.7.X
Changes in 0.7.10
=================
- Fix UnicodeEncodeError for dbref (#278)
- Allow construction using positional parameters (#268)
- Updated EmailField length to support long domains (#243)
- Added 64-bit integer support (#251)
- Added Django sessions TTL support (#224)
- Fixed issue with numerical keys in MapField(EmbeddedDocumentField()) (#240)
- Fixed clearing _changed_fields for complex nested embedded documents (#237, #239, #242)
- Added "id" back to _data dictionary (#255)
- Only mark a field as changed if the value has changed (#258)
- Explicitly check for Document instances when dereferencing (#261)
- Fixed order_by chaining issue (#265)
- Added dereference support for tuples (#250)
- Resolve field name to db field name when using distinct(#260, #264, #269)
- Added kwargs to doc.save to help interop with django (#223, #270)
- Fixed cloning querysets in PY3
- Int fields no longer unset in save when changed to 0 (#272)
- Fixed ReferenceField query chaining bug fixed (#254)
Changes in 0.7.9
================
- Better fix handling for old style _types
- Embedded SequenceFields follow collection naming convention
Changes in 0.7.8
================
- Fix sequence fields in embedded documents (MongoEngine/mongoengine#166)
- Fix query chaining with .order_by() (MongoEngine/mongoengine#176)
- Added optional encoding and collection config for Django sessions (MongoEngine/mongoengine#180, MongoEngine/mongoengine#181, MongoEngine/mongoengine#183)
- Fixed EmailField so can add extra validation (MongoEngine/mongoengine#173, MongoEngine/mongoengine#174, MongoEngine/mongoengine#187)
- Fixed bulk inserts can now handle custom pk's (MongoEngine/mongoengine#192)
- Added as_pymongo method to return raw or cast results from pymongo (MongoEngine/mongoengine#193)
Changes in 0.7.7
================
- Fix handling for old style _types
Changes in 0.7.6
================
- Unicode fix for repr (MongoEngine/mongoengine#133)
- Allow updates with match operators (MongoEngine/mongoengine#144)

View File

@@ -10,6 +10,16 @@ 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.
.. note ::
If you are not using another Database backend make sure you add a dummy
backend, by adding the following to ``settings.py``::
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}
Authentication
==============
MongoEngine includes a Django authentication backend, which uses MongoDB. The
@@ -45,6 +55,9 @@ into you settings module::
SESSION_ENGINE = 'mongoengine.django.sessions'
Django provides session cookie, which expires after ```SESSION_COOKIE_AGE``` seconds, but doesnt delete cookie at sessions backend, so ``'mongoengine.django.sessions'`` supports `mongodb TTL
<http://docs.mongodb.org/manual/tutorial/expire-data/>`_.
.. versionadded:: 0.2.1
Storage

View File

@@ -183,3 +183,9 @@ Alternatively, you can rename your collections eg ::
else:
print "Upgraded collection names"
mongodb 1.8 > 2.0 +
===================
Its been reported that indexes may need to be recreated to the newer version of indexes.
To do this drop indexes and call ``ensure_indexes`` on each model.

View File

@@ -12,7 +12,7 @@ from signals import *
__all__ = (document.__all__ + fields.__all__ + connection.__all__ +
queryset.__all__ + signals.__all__)
VERSION = (0, 7, 5)
VERSION = (0, 7, 10)
def get_version():

View File

@@ -121,10 +121,11 @@ class ValidationError(AssertionError):
def get_document(name):
doc = _document_registry.get(name, None)
if not doc:
# Possible old style names
end = ".%s" % name
# Possible old style name
single_end = name.split('.')[-1]
compound_end = '.%s' % single_end
possible_match = [k for k in _document_registry.keys()
if k.endswith(end)]
if k.endswith(compound_end) or k == single_end]
if len(possible_match) == 1:
doc = _document_registry.get(possible_match.pop(), None)
if not doc:
@@ -204,8 +205,12 @@ class BaseField(object):
def __set__(self, instance, value):
"""Descriptor for assigning a value to a field in a document.
"""
instance._data[self.name] = value
if instance._initialised:
changed = False
if (self.name not in instance._data or
instance._data[self.name] != value):
changed = True
instance._data[self.name] = value
if changed and instance._initialised:
instance._mark_as_changed(self.name)
def error(self, message="", errors=None, field_name=None):
@@ -251,7 +256,7 @@ class BaseField(object):
elif value_to_check not in self.choices:
msg = ('Value must be %s of %s' %
(err_msg, unicode(self.choices)))
self.error()
self.error(msg)
# check validation argument
if self.validation is not None:
@@ -316,12 +321,6 @@ class ComplexBaseField(BaseField):
return value
def __set__(self, instance, value):
"""Descriptor for assigning a value to a field in a document.
"""
instance._data[self.name] = value
instance._mark_as_changed(self.name)
def to_python(self, value):
"""Convert a MongoDB-compatible type to a Python type.
"""
@@ -559,8 +558,11 @@ class DocumentMetaclass(type):
# Set _fields and db_field maps
attrs['_fields'] = doc_fields
attrs['_db_field_map'] = dict([(k, getattr(v, 'db_field', k))
for k, v in doc_fields.iteritems()])
attrs['_fields_ordered'] = tuple(i[1]
for i in sorted((v.creation_counter, v.name)
for v in doc_fields.itervalues()))
attrs['_db_field_map'] = dict((k, getattr(v, 'db_field', k))
for k, v in doc_fields.iteritems())
attrs['_reverse_db_field_map'] = dict(
(v, k) for k, v in attrs['_db_field_map'].iteritems())
@@ -823,6 +825,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
if not new_class._meta.get('id_field'):
new_class._meta['id_field'] = 'id'
new_class._fields['id'] = ObjectIdField(db_field='_id')
new_class._fields['id'].name = 'id'
new_class.id = new_class._fields['id']
# Merge in exceptions with parent hierarchy
@@ -903,7 +906,17 @@ class BaseDocument(object):
_dynamic_lock = True
_initialised = False
def __init__(self, **values):
def __init__(self, *args, **values):
if args:
# Combine positional arguments with named arguments.
# We only want named arguments.
field = iter(self._fields_ordered)
for value in args:
name = field.next()
if name in values:
raise TypeError("Multiple values for keyword argument '" + name + "'")
values[name] = value
signals.pre_init.send(self.__class__, document=self, values=values)
self._data = {}
@@ -1122,6 +1135,22 @@ class BaseDocument(object):
key not in self._changed_fields):
self._changed_fields.append(key)
def _clear_changed_fields(self):
self._changed_fields = []
EmbeddedDocumentField = _import_class("EmbeddedDocumentField")
for field_name, field in self._fields.iteritems():
if (isinstance(field, ComplexBaseField) and
isinstance(field.field, EmbeddedDocumentField)):
field_value = getattr(self, field_name, None)
if field_value:
for idx in (field_value if isinstance(field_value, dict)
else xrange(len(field_value))):
field_value[idx]._clear_changed_fields()
elif isinstance(field, EmbeddedDocumentField):
field_value = getattr(self, field_name, None)
if field_value:
field_value._clear_changed_fields()
def _get_changed_fields(self, key='', inspected=None):
"""Returns a list of all fields that have explicitly been changed.
"""
@@ -1192,7 +1221,7 @@ class BaseDocument(object):
for p in parts:
if isinstance(d, DBRef):
break
elif p.isdigit():
elif isinstance(d, list) and p.isdigit():
d = d[int(p)]
elif hasattr(d, 'get'):
d = d.get(p)
@@ -1206,7 +1235,7 @@ class BaseDocument(object):
# Determine if any changed items were actually unset.
for path, value in set_data.items():
if value or isinstance(value, bool):
if value or isinstance(value, (bool, int)):
continue
# If we've set a value that ain't the default value dont unset it.
@@ -1223,7 +1252,7 @@ class BaseDocument(object):
parts = path.split('.')
db_field_name = parts.pop()
for p in parts:
if p.isdigit():
if isinstance(d, list) and p.isdigit():
d = d[int(p)]
elif (hasattr(d, '__getattribute__') and
not isinstance(d, dict)):
@@ -1301,7 +1330,10 @@ class BaseDocument(object):
return value
def __iter__(self):
return iter(self._fields)
if 'id' in self._fields and 'id' not in self._fields_ordered:
return iter(('id', ) + self._fields_ordered)
return iter(self._fields_ordered)
def __getitem__(self, name):
"""Dictionary-style field access, return a field's value if present.

View File

@@ -33,7 +33,7 @@ class DeReference(object):
self.max_depth = max_depth
doc_type = None
if instance and instance._fields:
if instance and isinstance(instance, (Document, TopLevelDocumentMetaclass)):
doc_type = instance._fields.get(name)
if hasattr(doc_type, 'field'):
doc_type = doc_type.field
@@ -84,7 +84,7 @@ class DeReference(object):
# Recursively find dbreferences
depth += 1
for k, item in iterator:
if hasattr(item, '_fields'):
if isinstance(item, Document):
for field_name, field in item._fields.iteritems():
v = item._data.get(field_name, None)
if isinstance(v, (DBRef)):
@@ -115,7 +115,7 @@ class DeReference(object):
object_map = {}
for col, dbrefs in self.reference_map.iteritems():
keys = object_map.keys()
refs = list(set([dbref for dbref in dbrefs if str(dbref) not in keys]))
refs = list(set([dbref for dbref in dbrefs if unicode(dbref).encode('utf-8') not in keys]))
if hasattr(col, 'objects'): # We have a document class for the refs
references = col.objects.in_bulk(refs)
for key, doc in references.iteritems():
@@ -171,6 +171,7 @@ class DeReference(object):
if not hasattr(items, 'items'):
is_list = True
as_tuple = isinstance(items, tuple)
iterator = enumerate(items)
data = []
else:
@@ -187,7 +188,7 @@ class DeReference(object):
if k in self.object_map and not is_list:
data[k] = self.object_map[k]
elif hasattr(v, '_fields'):
elif isinstance(v, Document):
for field_name, field in v._fields.iteritems():
v = data[k]._data.get(field_name, None)
if isinstance(v, (DBRef)):
@@ -205,7 +206,7 @@ class DeReference(object):
if instance and name:
if is_list:
return BaseList(data, instance, name)
return tuple(data) if as_tuple else BaseList(data, instance, name)
return BaseDict(data, instance, name)
depth += 1
return data

View File

@@ -15,15 +15,33 @@ MONGOENGINE_SESSION_DB_ALIAS = getattr(
settings, 'MONGOENGINE_SESSION_DB_ALIAS',
DEFAULT_CONNECTION_NAME)
# a setting for the name of the collection used to store sessions
MONGOENGINE_SESSION_COLLECTION = getattr(
settings, 'MONGOENGINE_SESSION_COLLECTION',
'django_session')
# a setting for whether session data is stored encoded or not
MONGOENGINE_SESSION_DATA_ENCODE = getattr(
settings, 'MONGOENGINE_SESSION_DATA_ENCODE',
True)
class MongoSession(Document):
session_key = fields.StringField(primary_key=True, max_length=40)
session_data = fields.StringField()
session_data = fields.StringField() if MONGOENGINE_SESSION_DATA_ENCODE \
else fields.DictField()
expire_date = fields.DateTimeField()
meta = {'collection': 'django_session',
'db_alias': MONGOENGINE_SESSION_DB_ALIAS,
'allow_inheritance': False}
meta = {
'collection': MONGOENGINE_SESSION_COLLECTION,
'db_alias': MONGOENGINE_SESSION_DB_ALIAS,
'allow_inheritance': False,
'indexes': [
{
'fields': ['expire_date'],
'expireAfterSeconds': settings.SESSION_COOKIE_AGE
}
]
}
class SessionStore(SessionBase):
@@ -34,7 +52,10 @@ class SessionStore(SessionBase):
try:
s = MongoSession.objects(session_key=self.session_key,
expire_date__gt=datetime.now())[0]
return self.decode(force_unicode(s.session_data))
if MONGOENGINE_SESSION_DATA_ENCODE:
return self.decode(force_unicode(s.session_data))
else:
return s.session_data
except (IndexError, SuspiciousOperation):
self.create()
return {}
@@ -57,7 +78,10 @@ class SessionStore(SessionBase):
if self.session_key is None:
self._session_key = self._get_new_session_key()
s = MongoSession(session_key=self.session_key)
s.session_data = self.encode(self._get_session(no_load=must_create))
if MONGOENGINE_SESSION_DATA_ENCODE:
s.session_data = self.encode(self._get_session(no_load=must_create))
else:
s.session_data = self._get_session(no_load=must_create)
s.expire_date = self.get_expiry_date()
try:
s.save(force_insert=must_create, safe=True)

View File

@@ -164,7 +164,7 @@ class Document(BaseDocument):
def save(self, safe=True, force_insert=False, validate=True,
write_options=None, cascade=None, cascade_kwargs=None,
_refs=None):
_refs=None, **kwargs):
"""Save the :class:`~mongoengine.Document` to the database. If the
document already exists, it will be updated, otherwise it will be
created.
@@ -269,7 +269,7 @@ class Document(BaseDocument):
if id_field not in self._meta.get('shard_key', []):
self[id_field] = self._fields[id_field].to_python(object_id)
self._changed_fields = []
self._clear_changed_fields()
self._created = False
signals.post_save.send(self.__class__, document=self, created=created)
return self

View File

@@ -27,7 +27,7 @@ except ImportError:
Image = None
ImageOps = None
__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
__all__ = ['StringField', 'IntField', 'LongField', 'FloatField', 'BooleanField',
'DateTimeField', 'EmbeddedDocumentField', 'ListField', 'DictField',
'ObjectIdField', 'ReferenceField', 'ValidationError', 'MapField',
'DecimalField', 'ComplexDateTimeField', 'URLField', 'DynamicField',
@@ -143,16 +143,17 @@ class EmailField(StringField):
EMAIL_REGEX = re.compile(
r"(^[-!#$%&'*+/=?^_`{}|~0-9A-Z]+(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*" # dot-atom
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\001-011\013\014\016-\177])*"' # quoted-string
r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE # domain
r')@(?:[A-Z0-9](?:[A-Z0-9-]{0,253}[A-Z0-9])?\.)+[A-Z]{2,6}\.?$', re.IGNORECASE # domain
)
def validate(self, value):
if not EmailField.EMAIL_REGEX.match(value):
self.error('Invalid Mail-address: %s' % value)
super(EmailField, self).validate(value)
class IntField(BaseField):
"""An integer field.
"""An 32-bit integer field.
"""
def __init__(self, min_value=None, max_value=None, **kwargs):
@@ -185,6 +186,40 @@ class IntField(BaseField):
return int(value)
class LongField(BaseField):
"""An 64-bit integer field.
"""
def __init__(self, min_value=None, max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
super(LongField, self).__init__(**kwargs)
def to_python(self, value):
try:
value = long(value)
except ValueError:
pass
return value
def validate(self, value):
try:
value = long(value)
except:
self.error('%s could not be converted to long' % value)
if self.min_value is not None and value < self.min_value:
self.error('Long value is too small')
if self.max_value is not None and value > self.max_value:
self.error('Long value is too large')
def prepare_query_value(self, op, value):
if value is None:
return value
return long(value)
class FloatField(BaseField):
"""An floating point number field.
"""
@@ -777,7 +812,7 @@ class ReferenceField(BaseField):
def to_mongo(self, document):
if isinstance(document, DBRef):
if not self.dbref:
return DBRef.id
return document.id
return document
elif not self.dbref and isinstance(document, basestring):
return document
@@ -1337,7 +1372,7 @@ class SequenceField(IntField):
.. versionadded:: 0.5
"""
def __init__(self, collection_name=None, db_alias = None, sequence_name = None, *args, **kwargs):
def __init__(self, collection_name=None, db_alias=None, sequence_name=None, *args, **kwargs):
self.collection_name = collection_name or 'mongoengine.counters'
self.db_alias = db_alias or DEFAULT_CONNECTION_NAME
self.sequence_name = sequence_name
@@ -1347,7 +1382,7 @@ class SequenceField(IntField):
"""
Generate and Increment the counter
"""
sequence_name = self.sequence_name or self.owner_document._get_collection_name()
sequence_name = self.get_sequence_name()
sequence_id = "%s.%s" % (sequence_name, self.name)
collection = get_db(alias=self.db_alias)[self.collection_name]
counter = collection.find_and_modify(query={"_id": sequence_id},
@@ -1356,6 +1391,16 @@ class SequenceField(IntField):
upsert=True)
return counter['next']
def get_sequence_name(self):
if self.sequence_name:
return self.sequence_name
owner = self.owner_document
if issubclass(owner, Document):
return owner._get_collection_name()
else:
return ''.join('_%s' % c if c.isupper() else c
for c in owner._class_name).strip('_').lower()
def __get__(self, instance, owner):
if instance is None:

View File

@@ -7,10 +7,11 @@ import operator
from collections import defaultdict
from functools import partial
from mongoengine.python_support import product, reduce
from mongoengine.python_support import product, reduce, PY3
import pymongo
from bson.code import Code
from bson.son import SON
from mongoengine import signals
@@ -353,6 +354,8 @@ class QuerySet(object):
self._slave_okay = False
self._iter = False
self._scalar = []
self._as_pymongo = False
self._as_pymongo_coerce = False
# If inheritance is allowed, only return instances and instances of
# subclasses of the class being used
@@ -364,6 +367,10 @@ class QuerySet(object):
self._skip = None
self._hint = -1 # Using -1 as None is a valid value for hint
def __deepcopy__(self, memo):
"""Essential for chained queries with ReferenceFields involved"""
return self.clone()
def clone(self):
"""Creates a copy of the current :class:`~mongoengine.queryset.QuerySet`
@@ -372,8 +379,8 @@ class QuerySet(object):
c = self.__class__(self._document, self._collection_obj)
copy_props = ('_initial_query', '_query_obj', '_where_clause',
'_loaded_fields', '_ordering', '_snapshot',
'_timeout', '_limit', '_skip', '_slave_okay', '_hint')
'_loaded_fields', '_ordering', '_snapshot', '_timeout',
'_limit', '_skip', '_slave_okay', '_hint')
for prop in copy_props:
val = getattr(self, prop)
@@ -390,7 +397,7 @@ class QuerySet(object):
return self._mongo_query
def ensure_index(self, key_or_list, drop_dups=False, background=False,
**kwargs):
**kwargs):
"""Ensure that the given indexes are in place.
:param key_or_list: a single index key or a list of index keys (to
@@ -608,11 +615,13 @@ class QuerySet(object):
if self._where_clause:
self._cursor_obj.where(self._where_clause)
# apply default ordering
if self._ordering:
# Apply query ordering
self._cursor_obj.sort(self._ordering)
elif self._document._meta['ordering']:
# Otherwise, apply the ordering from the document model
self.order_by(*self._document._meta['ordering'])
self._cursor_obj.sort(self._ordering)
if self._limit is not None:
self._cursor_obj.limit(self._limit - (self._skip or 0))
@@ -700,7 +709,7 @@ class QuerySet(object):
mongo_query = {}
merge_query = defaultdict(list)
for key, value in query.items():
for key, value in sorted(query.items()):
if key == "__raw__":
mongo_query.update(value)
continue
@@ -804,7 +813,6 @@ class QuerySet(object):
mongo_query['$and'].append(value)
else:
mongo_query['$and'] = value
return mongo_query
def get(self, *q_objs, **query):
@@ -927,7 +935,7 @@ class QuerySet(object):
if not isinstance(doc, self._document):
msg = "Some documents inserted aren't instances of %s" % str(self._document)
raise OperationError(msg)
if doc.pk:
if doc.pk and not doc._created:
msg = "Some documents have ObjectIds use doc.update() instead"
raise OperationError(msg)
raw.append(doc.to_mongo())
@@ -986,6 +994,9 @@ class QuerySet(object):
for doc in docs:
doc_map[doc['_id']] = self._get_scalar(
self._document._from_son(doc))
elif self._as_pymongo:
for doc in docs:
doc_map[doc['_id']] = self._get_as_pymongo(doc)
else:
for doc in docs:
doc_map[doc['_id']] = self._document._from_son(doc)
@@ -1002,6 +1013,9 @@ class QuerySet(object):
if self._scalar:
return self._get_scalar(self._document._from_son(
self._cursor.next()))
if self._as_pymongo:
return self._get_as_pymongo(self._cursor.next())
return self._document._from_son(self._cursor.next())
except StopIteration, e:
self.rewind()
@@ -1184,6 +1198,8 @@ class QuerySet(object):
if self._scalar:
return self._get_scalar(self._document._from_son(
self._cursor[key]))
if self._as_pymongo:
return self._get_as_pymongo(self._cursor.next())
return self._document._from_son(self._cursor[key])
raise AttributeError
@@ -1196,8 +1212,11 @@ class QuerySet(object):
.. versionchanged:: 0.5 - Fixed handling references
.. versionchanged:: 0.6 - Improved db_field refrence handling
"""
return self._dereference(self._cursor.distinct(field), 1,
name=field, instance=self._document)
try:
field = self._fields_to_dbfields([field]).pop()
finally:
return self._dereference(self._cursor.distinct(field), 1,
name=field, instance=self._document)
def only(self, *fields):
"""Load only a subset of this document's fields. ::
@@ -1302,7 +1321,8 @@ class QuerySet(object):
key_list.append((key, direction))
self._ordering = key_list
self._cursor.sort(key_list)
if self._cursor_obj:
self._cursor_obj.sort(key_list)
return self
def explain(self, format=False):
@@ -1580,6 +1600,48 @@ class QuerySet(object):
return tuple(data)
def _get_as_pymongo(self, row):
# Extract which fields paths we should follow if .fields(...) was
# used. If not, handle all fields.
if not getattr(self, '__as_pymongo_fields', None):
self.__as_pymongo_fields = []
for field in self._loaded_fields.fields - set(['_cls', '_id', '_types']):
self.__as_pymongo_fields.append(field)
while '.' in field:
field, _ = field.rsplit('.', 1)
self.__as_pymongo_fields.append(field)
all_fields = not self.__as_pymongo_fields
def clean(data, path=None):
path = path or ''
if isinstance(data, dict):
new_data = {}
for key, value in data.iteritems():
new_path = '%s.%s' % (path, key) if path else key
if all_fields or new_path in self.__as_pymongo_fields:
new_data[key] = clean(value, path=new_path)
data = new_data
elif isinstance(data, list):
data = [clean(d, path=path) for d in data]
else:
if self._as_pymongo_coerce:
# If we need to coerce types, we need to determine the
# type of this field and use the corresponding .to_python(...)
from mongoengine.fields import EmbeddedDocumentField
obj = self._document
for chunk in path.split('.'):
obj = getattr(obj, chunk, None)
if obj is None:
break
elif isinstance(obj, EmbeddedDocumentField):
obj = obj.document_type
if obj and data is not None:
data = obj.to_python(data)
return data
return clean(row)
def scalar(self, *fields):
"""Instead of returning Document instances, return either a specific
value or a tuple of values in order.
@@ -1602,6 +1664,16 @@ class QuerySet(object):
"""An alias for scalar"""
return self.scalar(*fields)
def as_pymongo(self, coerce_types=False):
"""Instead of returning Document instances, return raw values from
pymongo.
:param coerce_type: Field types (if applicable) would be use to coerce types.
"""
self._as_pymongo = True
self._as_pymongo_coerce = coerce_types
return self
def _sub_js_fields(self, code):
"""When fields are specified with [~fieldname] syntax, where
*fieldname* is the Python name of a field, *fieldname* will be

View File

@@ -5,7 +5,7 @@
%define srcname mongoengine
Name: python-%{srcname}
Version: 0.7.5
Version: 0.7.10
Release: 1%{?dist}
Summary: A Python Document-Object Mapper for working with MongoDB
@@ -51,4 +51,4 @@ rm -rf $RPM_BUILD_ROOT
# %{python_sitearch}/*
%changelog
* See: http://readthedocs.org/docs/mongoengine-odm/en/latest/changelog.html
* See: http://docs.mongoengine.org/en/latest/changelog.html

View File

@@ -58,7 +58,7 @@ if sys.version_info[0] == 3:
extra_opts['packages'].append("tests")
extra_opts['package_data'] = {"tests": ["mongoengine.png"]}
else:
extra_opts['tests_require'] = ['nose', 'coverage', 'blinker', 'django>=1.3', 'PIL']
extra_opts['tests_require'] = ['nose', 'coverage', 'blinker', 'django==1.4.2', 'PIL']
extra_opts['packages'] = find_packages(exclude=('tests',))
setup(name='mongoengine',

View File

@@ -53,7 +53,7 @@ class TestWarnings(unittest.TestCase):
p2.parent = p1
p2.save(cascade=False)
self.assertEqual(len(self.warning_list), 1)
self.assertTrue(len(self.warning_list) > 0)
warning = self.warning_list[0]
self.assertEqual(FutureWarning, warning["category"])
self.assertTrue("ReferenceFields will default to using ObjectId"
@@ -76,7 +76,9 @@ class TestWarnings(unittest.TestCase):
p2.parent.name = "Poppa Wilson"
p2.save()
self.assertEqual(len(self.warning_list), 1)
self.assertTrue(len(self.warning_list) > 0)
if len(self.warning_list) > 1:
print self.warning_list
warning = self.warning_list[0]
self.assertEqual(FutureWarning, warning["category"])
self.assertTrue("Cascading saves will default to off in 0.8"

View File

@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
import unittest
@@ -42,6 +43,12 @@ class FieldTest(unittest.TestCase):
group_obj = Group.objects.first()
self.assertEqual(q, 1)
len(group_obj._data['members'])
self.assertEqual(q, 1)
len(group_obj.members)
self.assertEqual(q, 2)
[m for m in group_obj.members]
self.assertEqual(q, 2)
@@ -179,8 +186,9 @@ class FieldTest(unittest.TestCase):
# Migrate the data
for g in Group.objects():
g.author = g.author
g.members = g.members
# Explicitly mark as changed so resets
g._mark_as_changed('author')
g._mark_as_changed('members')
g.save()
group = Group.objects.first()
@@ -991,3 +999,57 @@ class FieldTest(unittest.TestCase):
msg = Message.objects.get(id=1)
self.assertEqual(0, msg.comments[0].id)
self.assertEqual(1, msg.comments[1].id)
def test_tuples_as_tuples(self):
"""
Ensure that tuples remain tuples when they are
inside a ComplexBaseField
"""
from mongoengine.base import BaseField
class EnumField(BaseField):
def __init__(self, **kwargs):
super(EnumField,self).__init__(**kwargs)
def to_mongo(self, value):
return value
def to_python(self, value):
return tuple(value)
class TestDoc(Document):
items = ListField(EnumField())
TestDoc.drop_collection()
tuples = [(100, 'Testing')]
doc = TestDoc()
doc.items = tuples
doc.save()
x = TestDoc.objects().get()
self.assertTrue(x is not None)
self.assertTrue(len(x.items) == 1)
self.assertTrue(tuple(x.items[0]) in tuples)
self.assertTrue(x.items[0] in tuples)
def test_non_ascii_pk(self):
"""
Ensure that dbref conversion to string does not fail when
non-ascii characters are used in primary key
"""
class Brand(Document):
title = StringField(max_length=255, primary_key=True)
class BrandGroup(Document):
title = StringField(max_length=255, primary_key=True)
brands = ListField(ReferenceField("Brand", dbref=True))
Brand.drop_collection()
BrandGroup.drop_collection()
brand1 = Brand(title="Moschino").save()
brand2 = Brand(title=u"Денис Симачёв").save()
BrandGroup(title="top_brands", brands=[brand1, brand2]).save()
brand_groups = BrandGroup.objects().all()
self.assertEqual(2, len([brand for bg in brand_groups for brand in bg.brands]))

View File

@@ -8,6 +8,7 @@ import sys
import unittest
import uuid
import warnings
import operator
from nose.plugins.skip import SkipTest
from datetime import datetime
@@ -15,7 +16,7 @@ from datetime import datetime
from tests.fixtures import Base, Mixin, PickleEmbedded, PickleTest
from mongoengine import *
from mongoengine.base import NotRegistered, InvalidDocumentError
from mongoengine.base import NotRegistered, InvalidDocumentError, get_document
from mongoengine.queryset import InvalidQueryError
from mongoengine.connection import get_db, get_connection
@@ -452,7 +453,8 @@ class DocumentTest(unittest.TestCase):
info = collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertEqual([[(u'_id', 1)], [(u'_types', 1), (u'name', 1)]], info)
self.assertEqual([[('_id', 1)], [('_types', 1), ('name', 1)]],
sorted(info, key=operator.itemgetter(0)))
# Turn off inheritance
class Animal(Document):
@@ -473,7 +475,8 @@ class DocumentTest(unittest.TestCase):
info = collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertEqual([[(u'_id', 1)], [(u'_types', 1), (u'name', 1)]], info)
self.assertEqual([[(u'_id', 1)], [(u'_types', 1), (u'name', 1)]],
sorted(info, key=operator.itemgetter(0)))
info = collection.index_information()
indexes_to_drop = [key for key, value in info.iteritems() if '_types' in dict(value['key'])]
@@ -482,14 +485,16 @@ class DocumentTest(unittest.TestCase):
info = collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertEqual([[(u'_id', 1)]], info)
self.assertEqual([[(u'_id', 1)]],
sorted(info, key=operator.itemgetter(0)))
# Recreate indexes
dog = Animal.objects.first()
dog.save()
info = collection.index_information()
info = [value['key'] for key, value in info.iteritems()]
self.assertEqual([[(u'_id', 1)], [(u'name', 1),]], info)
self.assertEqual([[(u'_id', 1)], [(u'name', 1),]],
sorted(info, key=operator.itemgetter(0)))
Animal.drop_collection()
@@ -924,7 +929,7 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(1, Person.objects.count())
info = Person.objects._collection.index_information()
self.assertEqual(info.keys(), ['_types_1_user_guid_1', '_id_', '_types_1_name_1'])
self.assertEqual(sorted(info.keys()), ['_id_', '_types_1_name_1', '_types_1_user_guid_1'])
Person.drop_collection()
def test_disable_index_creation(self):
@@ -968,7 +973,7 @@ class DocumentTest(unittest.TestCase):
BlogPost.drop_collection()
info = BlogPost.objects._collection.index_information()
self.assertEqual(info.keys(), ['_types_1_date.yr_-1', '_id_'])
self.assertEqual(sorted(info.keys()), [ '_id_', '_types_1_date.yr_-1'])
BlogPost.drop_collection()
def test_list_embedded_document_index(self):
@@ -991,7 +996,8 @@ class DocumentTest(unittest.TestCase):
info = BlogPost.objects._collection.index_information()
# we don't use _types in with list fields by default
self.assertEqual(info.keys(), ['_id_', '_types_1', 'tags.tag_1'])
self.assertEqual(sorted(info.keys()),
['_id_', '_types_1', 'tags.tag_1'])
post1 = BlogPost(title="Embedded Indexes tests in place",
tags=[Tag(name="about"), Tag(name="time")]
@@ -1008,7 +1014,7 @@ class DocumentTest(unittest.TestCase):
recursive_obj = EmbeddedDocumentField(RecursiveObject)
info = RecursiveDocument.objects._collection.index_information()
self.assertEqual(info.keys(), ['_id_', '_types_1'])
self.assertEqual(sorted(info.keys()), ['_id_', '_types_1'])
def test_geo_indexes_recursion(self):
@@ -1336,7 +1342,6 @@ class DocumentTest(unittest.TestCase):
User.drop_collection()
def test_document_not_registered(self):
class Place(Document):
@@ -1361,6 +1366,19 @@ class DocumentTest(unittest.TestCase):
print Place.objects.all()
self.assertRaises(NotRegistered, query_without_importing_nice_place)
def test_document_registry_regressions(self):
class Location(Document):
name = StringField()
meta = {'allow_inheritance': True}
class Area(Location):
location = ReferenceField('Location', dbref=True)
Location.drop_collection()
self.assertEquals(Area, get_document("Area"))
self.assertEquals(Area, get_document("Location.Area"))
def test_creation(self):
"""Ensure that document may be created using keyword arguments.
@@ -1369,6 +1387,28 @@ class DocumentTest(unittest.TestCase):
self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 30)
def test_positional_creation(self):
"""Ensure that document may be created using positional arguments.
"""
person = self.Person("Test User", 42)
self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 42)
def test_mixed_creation(self):
"""Ensure that document may be created using mixed arguments.
"""
person = self.Person("Test User", age=42)
self.assertEqual(person.name, "Test User")
self.assertEqual(person.age, 42)
def test_bad_mixed_creation(self):
"""Ensure that document gives correct error when duplicating arguments
"""
def construct_bad_instance():
return self.Person("Test User", 42, name="Bad User")
self.assertRaises(TypeError, construct_bad_instance)
def test_to_dbref(self):
"""Ensure that you can get a dbref of a document"""
person = self.Person(name="Test User", age=30)
@@ -1511,8 +1551,10 @@ class DocumentTest(unittest.TestCase):
doc.validate()
keys = doc._data.keys()
self.assertEqual(2, len(keys))
self.assertTrue(None in keys)
self.assertTrue('e' in keys)
# Ensure that the _id field has the right id
self.assertTrue('id' in keys)
self.assertEqual(doc._data.get('id'), doc.id)
def test_save(self):
"""Ensure that a document may be saved in the database.
@@ -2707,7 +2749,7 @@ class DocumentTest(unittest.TestCase):
Person.drop_collection()
self.assertEqual(Person._fields.keys(), ['name', 'id'])
self.assertEqual(sorted(Person._fields.keys()), ['id', 'name'])
Person(name="Rozza").save()
@@ -3350,6 +3392,60 @@ class DocumentTest(unittest.TestCase):
}
) ]), "1,2")
def test_data_contains_id_field(self):
"""Ensure that asking for _data returns 'id'
"""
class Person(Document):
name = StringField()
Person.drop_collection()
Person(name="Harry Potter").save()
person = Person.objects.first()
self.assertTrue('id' in person._data.keys())
self.assertEqual(person._data.get('id'), person.id)
def test_complex_nesting_document_and_embedded_document(self):
class Macro(EmbeddedDocument):
value = DynamicField(default="UNDEFINED")
class Parameter(EmbeddedDocument):
macros = MapField(EmbeddedDocumentField(Macro))
def expand(self):
self.macros["test"] = Macro()
class Node(Document):
parameters = MapField(EmbeddedDocumentField(Parameter))
def expand(self):
self.flattened_parameter = {}
for parameter_name, parameter in self.parameters.iteritems():
parameter.expand()
class System(Document):
name = StringField(required=True)
nodes = MapField(ReferenceField(Node, dbref=False))
def save(self, *args, **kwargs):
for node_name, node in self.nodes.iteritems():
node.expand()
node.save(*args, **kwargs)
super(System, self).save(*args, **kwargs)
System.drop_collection()
Node.drop_collection()
system = System(name="system")
system.nodes["node"] = Node()
system.save()
system.nodes["node"].parameters["param"] = Parameter()
system.save()
system = System.objects.first()
self.assertEqual("UNDEFINED", system.nodes["node"].parameters["param"].macros["test"].value)
class ValidatorErrorTest(unittest.TestCase):
@@ -3399,8 +3495,8 @@ class ValidatorErrorTest(unittest.TestCase):
try:
User().validate()
except ValidationError, e:
expected_error_message = """ValidationError(Field is required: ['username', 'name'])"""
self.assertEqual(e.message, expected_error_message)
expected_error_message = """ValidationError(Field is required"""
self.assertTrue(expected_error_message in e.message)
self.assertEqual(e.to_dict(), {
'username': 'Field is required',
'name': 'Field is required'})
@@ -3503,6 +3599,5 @@ class ValidatorErrorTest(unittest.TestCase):
self.assertRaises(OperationError, change_shard_key)
if __name__ == '__main__':
unittest.main()

View File

@@ -144,6 +144,17 @@ class FieldTest(unittest.TestCase):
self.assertEqual(1, TestDocument.objects(int_fld__ne=None).count())
self.assertEqual(1, TestDocument.objects(float_fld__ne=None).count())
def test_long_ne_operator(self):
class TestDocument(Document):
long_fld = LongField()
TestDocument.drop_collection()
TestDocument(long_fld=None).save()
TestDocument(long_fld=1).save()
self.assertEqual(1, TestDocument.objects(long_fld__ne=None).count())
def test_object_id_validation(self):
"""Ensure that invalid values cannot be assigned to string fields.
"""
@@ -217,6 +228,23 @@ class FieldTest(unittest.TestCase):
person.age = 'ten'
self.assertRaises(ValidationError, person.validate)
def test_long_validation(self):
"""Ensure that invalid values cannot be assigned to long fields.
"""
class TestDocument(Document):
value = LongField(min_value=0, max_value=110)
doc = TestDocument()
doc.value = 50
doc.validate()
doc.value = -1
self.assertRaises(ValidationError, doc.validate)
doc.age = 120
self.assertRaises(ValidationError, doc.validate)
doc.age = 'ten'
self.assertRaises(ValidationError, doc.validate)
def test_float_validation(self):
"""Ensure that invalid values cannot be assigned to float fields.
"""
@@ -965,6 +993,24 @@ class FieldTest(unittest.TestCase):
doc = self.db.test.find_one()
self.assertEqual(doc['x']['DICTIONARY_KEY']['i'], 2)
def test_mapfield_numerical_index(self):
"""Ensure that MapField accept numeric strings as indexes."""
class Embedded(EmbeddedDocument):
name = StringField()
class Test(Document):
my_map = MapField(EmbeddedDocumentField(Embedded))
Test.drop_collection()
test = Test()
test.my_map['1'] = Embedded(name='test')
test.save()
test.my_map['1'].name = 'test updated'
test.save()
Test.drop_collection()
def test_map_field_lookup(self):
"""Ensure MapField lookups succeed on Fields without a lookup method"""
@@ -1105,6 +1151,16 @@ class FieldTest(unittest.TestCase):
p = Person.objects.get(name="Ross")
self.assertEqual(p.parent, p1)
def test_dbref_to_mongo(self):
class Person(Document):
name = StringField()
parent = ReferenceField('self', dbref=False)
p1 = Person._from_son({'name': "Yakxxx",
'parent': "50a234ea469ac1eda42d347d"})
mongoed = p1.to_mongo()
self.assertTrue(isinstance(mongoed['parent'], ObjectId))
def test_objectid_reference_fields(self):
class Person(Document):
@@ -2175,6 +2231,28 @@ class FieldTest(unittest.TestCase):
c = self.db['mongoengine.counters'].find_one({'_id': 'animal.id'})
self.assertEqual(c['next'], 10)
def test_embedded_sequence_field(self):
class Comment(EmbeddedDocument):
id = SequenceField()
content = StringField(required=True)
class Post(Document):
title = StringField(required=True)
comments = ListField(EmbeddedDocumentField(Comment))
self.db['mongoengine.counters'].drop()
Post.drop_collection()
Post(title="MongoEngine",
comments=[Comment(content="NoSQL Rocks"),
Comment(content="MongoEngine Rocks")]).save()
c = self.db['mongoengine.counters'].find_one({'_id': 'comment.id'})
self.assertEqual(c['next'], 2)
post = Post.objects.first()
self.assertEqual(1, post.comments[0].id)
self.assertEqual(2, post.comments[1].id)
def test_generic_embedded_document(self):
class Car(EmbeddedDocument):
name = StringField()
@@ -2293,12 +2371,38 @@ class FieldTest(unittest.TestCase):
self.assertTrue(1 in error_dict['comments'])
self.assertTrue('content' in error_dict['comments'][1])
self.assertEqual(error_dict['comments'][1]['content'],
u'Field is required')
u'Field is required')
post.comments[1].content = 'here we go'
post.validate()
def test_email_field(self):
class User(Document):
email = EmailField()
user = User(email="ross@example.com")
self.assertTrue(user.validate() is None)
user = User(email=("Kofq@rhom0e4klgauOhpbpNdogawnyIKvQS0wk2mjqrgGQ5S"
"ucictfqpdkK9iS1zeFw8sg7s7cwAF7suIfUfeyueLpfosjn3"
"aJIazqqWkm7.net"))
self.assertTrue(user.validate() is None)
user = User(email='me@localhost')
self.assertRaises(ValidationError, user.validate)
def test_email_field_honors_regex(self):
class User(Document):
email = EmailField(regex=r'\w+@example.com')
# Fails regex validation
user = User(email='me@foo.com')
self.assertRaises(ValidationError, user.validate)
# Passes regex validation
user = User(email='me@example.com')
self.assertTrue(user.validate() is None)
if __name__ == '__main__':
unittest.main()

View File

@@ -47,7 +47,7 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(QuerySet._transform_query(age__gt=20, age__lt=50),
{'age': {'$gt': 20, '$lt': 50}})
self.assertEqual(QuerySet._transform_query(age=20, age__gt=50),
{'age': 20})
{'$and': [{'age': {'$gt': 50}}, {'age': 20}]})
self.assertEqual(QuerySet._transform_query(friend__age__gte=30),
{'friend.age': {'$gte': 30}})
self.assertEqual(QuerySet._transform_query(name__exists=True),
@@ -232,28 +232,33 @@ class QuerySetTest(unittest.TestCase):
def test_chaining(self):
class A(Document):
pass
s = StringField()
class B(Document):
a = ReferenceField(A)
ref = ReferenceField(A)
boolfield = BooleanField(default=False)
A.drop_collection()
B.drop_collection()
a1 = A().save()
a2 = A().save()
a1 = A(s="test1").save()
a2 = A(s="test2").save()
B(a=a1).save()
B(ref=a1, boolfield=True).save()
# Works
q1 = B.objects.filter(a__in=[a1, a2], a=a1)._query
q1 = B.objects.filter(ref__in=[a1, a2], ref=a1)._query
# Doesn't work
q2 = B.objects.filter(a__in=[a1, a2])
q2 = q2.filter(a=a1)._query
q2 = B.objects.filter(ref__in=[a1, a2])
q2 = q2.filter(ref=a1)._query
self.assertEqual(q1, q2)
a_objects = A.objects(s='test1')
query = B.objects(ref__in=a_objects)
query = query.filter(boolfield=True)
self.assertEquals(query.count(), 1)
def test_update_write_options(self):
"""Test that passing write_options works"""
@@ -591,6 +596,10 @@ class QuerySetTest(unittest.TestCase):
self.assertRaises(OperationError, throw_operation_error)
# Test can insert new doc
new_post = Blog(title="code", id=ObjectId())
Blog.objects.insert(new_post)
# test handles other classes being inserted
def throw_operation_error_wrong_doc():
class Author(Document):
@@ -948,6 +957,11 @@ class QuerySetTest(unittest.TestCase):
{'attachments.views.extracted': 'no'}]}
self.assertEqual(expected, raw_query)
def assertSequence(self, qs, expected):
self.assertEqual(len(qs), len(expected))
for i in range(len(qs)):
self.assertEqual(qs[i], expected[i])
def test_ordering(self):
"""Ensure default ordering is applied and can be overridden.
"""
@@ -961,10 +975,10 @@ class QuerySetTest(unittest.TestCase):
BlogPost.drop_collection()
blog_post_1 = BlogPost(title="Blog Post #1",
published_date=datetime(2010, 1, 5, 0, 0 ,0))
blog_post_2 = BlogPost(title="Blog Post #2",
published_date=datetime(2010, 1, 6, 0, 0 ,0))
blog_post_1 = BlogPost(title="Blog Post #1",
published_date=datetime(2010, 1, 5, 0, 0 ,0))
blog_post_3 = BlogPost(title="Blog Post #3",
published_date=datetime(2010, 1, 7, 0, 0 ,0))
@@ -974,14 +988,13 @@ class QuerySetTest(unittest.TestCase):
# get the "first" BlogPost using default ordering
# from BlogPost.meta.ordering
latest_post = BlogPost.objects.first()
self.assertEqual(latest_post.title, "Blog Post #3")
expected = [blog_post_3, blog_post_2, blog_post_1]
self.assertSequence(BlogPost.objects.all(), expected)
# override default ordering, order BlogPosts by "published_date"
first_post = BlogPost.objects.order_by("+published_date").first()
self.assertEqual(first_post.title, "Blog Post #1")
BlogPost.drop_collection()
qs = BlogPost.objects.order_by("+published_date")
expected = [blog_post_1, blog_post_2, blog_post_3]
self.assertSequence(qs, expected)
def test_only(self):
"""Ensure that QuerySet.only only returns the requested fields.
@@ -1917,8 +1930,8 @@ class QuerySetTest(unittest.TestCase):
def test_order_by(self):
"""Ensure that QuerySets may be ordered.
"""
self.Person(name="User A", age=20).save()
self.Person(name="User B", age=40).save()
self.Person(name="User A", age=20).save()
self.Person(name="User C", age=30).save()
names = [p.name for p in self.Person.objects.order_by('-age')]
@@ -1933,6 +1946,93 @@ class QuerySetTest(unittest.TestCase):
ages = [p.age for p in self.Person.objects.order_by('-name')]
self.assertEqual(ages, [30, 40, 20])
def test_order_by_optional(self):
class BlogPost(Document):
title = StringField()
published_date = DateTimeField(required=False)
BlogPost.drop_collection()
blog_post_3 = BlogPost(title="Blog Post #3",
published_date=datetime(2010, 1, 6, 0, 0 ,0))
blog_post_2 = BlogPost(title="Blog Post #2",
published_date=datetime(2010, 1, 5, 0, 0 ,0))
blog_post_4 = BlogPost(title="Blog Post #4",
published_date=datetime(2010, 1, 7, 0, 0 ,0))
blog_post_1 = BlogPost(title="Blog Post #1", published_date=None)
blog_post_3.save()
blog_post_1.save()
blog_post_4.save()
blog_post_2.save()
expected = [blog_post_1, blog_post_2, blog_post_3, blog_post_4]
self.assertSequence(BlogPost.objects.order_by('published_date'),
expected)
self.assertSequence(BlogPost.objects.order_by('+published_date'),
expected)
expected.reverse()
self.assertSequence(BlogPost.objects.order_by('-published_date'),
expected)
def test_order_by_list(self):
class BlogPost(Document):
title = StringField()
published_date = DateTimeField(required=False)
BlogPost.drop_collection()
blog_post_1 = BlogPost(title="A",
published_date=datetime(2010, 1, 6, 0, 0 ,0))
blog_post_2 = BlogPost(title="B",
published_date=datetime(2010, 1, 6, 0, 0 ,0))
blog_post_3 = BlogPost(title="C",
published_date=datetime(2010, 1, 7, 0, 0 ,0))
blog_post_2.save()
blog_post_3.save()
blog_post_1.save()
qs = BlogPost.objects.order_by('published_date', 'title')
expected = [blog_post_1, blog_post_2, blog_post_3]
self.assertSequence(qs, expected)
qs = BlogPost.objects.order_by('-published_date', '-title')
expected.reverse()
self.assertSequence(qs, expected)
def test_order_by_chaining(self):
"""Ensure that an order_by query chains properly and allows .only()
"""
self.Person(name="User B", age=40).save()
self.Person(name="User A", age=20).save()
self.Person(name="User C", age=30).save()
only_age = self.Person.objects.order_by('-age').only('age')
names = [p.name for p in only_age]
ages = [p.age for p in only_age]
# The .only('age') clause should mean that all names are None
self.assertEqual(names, [None, None, None])
self.assertEqual(ages, [40, 30, 20])
qs = self.Person.objects.all().order_by('-age')
qs = qs.limit(10)
ages = [p.age for p in qs]
self.assertEqual(ages, [40, 30, 20])
qs = self.Person.objects.all().limit(10)
qs = qs.order_by('-age')
ages = [p.age for p in qs]
self.assertEqual(ages, [40, 30, 20])
qs = self.Person.objects.all().skip(0)
qs = qs.order_by('-age')
ages = [p.age for p in qs]
self.assertEqual(ages, [40, 30, 20])
def test_confirm_order_by_reference_wont_work(self):
"""Ordering by reference is not possible. Use map / reduce.. or
denormalise"""
@@ -2461,6 +2561,25 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(Foo.objects.distinct("bar"), [bar])
def test_distinct_handles_db_field(self):
"""Ensure that distinct resolves field name to db_field as expected.
"""
class Product(Document):
product_id = IntField(db_field='pid')
Product.drop_collection()
Product(product_id=1).save()
Product(product_id=2).save()
Product(product_id=1).save()
self.assertEqual(set(Product.objects.distinct('product_id')),
set([1, 2]))
self.assertEqual(set(Product.objects.distinct('pid')),
set([1, 2]))
Product.drop_collection()
def test_custom_manager(self):
"""Ensure that custom QuerySetManager instances work as expected.
"""
@@ -3691,6 +3810,38 @@ class QueryFieldListTest(unittest.TestCase):
ak = list(Bar.objects(foo__match={'shape': "square", "color": "purple"}))
self.assertEqual([b1], ak)
def test_as_pymongo(self):
from decimal import Decimal
class User(Document):
id = ObjectIdField('_id')
name = StringField()
age = IntField()
price = DecimalField()
User.drop_collection()
User(name="Bob Dole", age=89, price=Decimal('1.11')).save()
User(name="Barack Obama", age=51, price=Decimal('2.22')).save()
users = User.objects.only('name', 'price').as_pymongo()
results = list(users)
self.assertTrue(isinstance(results[0], dict))
self.assertTrue(isinstance(results[1], dict))
self.assertEqual(results[0]['name'], 'Bob Dole')
self.assertEqual(results[0]['price'], '1.11')
self.assertEqual(results[1]['name'], 'Barack Obama')
self.assertEqual(results[1]['price'], '2.22')
# Test coerce_types
users = User.objects.only('name', 'price').as_pymongo(coerce_types=True)
results = list(users)
self.assertTrue(isinstance(results[0], dict))
self.assertTrue(isinstance(results[1], dict))
self.assertEqual(results[0]['name'], 'Bob Dole')
self.assertEqual(results[0]['price'], Decimal('1.11'))
self.assertEqual(results[1]['name'], 'Barack Obama')
self.assertEqual(results[1]['price'], Decimal('2.22'))
if __name__ == '__main__':
unittest.main()