Added InvalidDocumentError
Ensures defined documents are valid and users don't override core methods by accident. fixes #275
This commit is contained in:
parent
60f0491f62
commit
050542c29b
@ -5,7 +5,8 @@ Changelog
|
|||||||
Changes in dev
|
Changes in dev
|
||||||
==============
|
==============
|
||||||
|
|
||||||
- Added GenericEmbeddedDocument - So you can embed any type of embeddable document
|
- Added InvalidDocumentError - so Document core methods can't be overwritten
|
||||||
|
- Added GenericEmbeddedDocument - so you can embed any type of embeddable document
|
||||||
- Added within_polygon support - for those with mongodb 1.9
|
- Added within_polygon support - for those with mongodb 1.9
|
||||||
- Updated sum / average to use map_reduce as db.eval doesn't work in sharded environments
|
- Updated sum / average to use map_reduce as db.eval doesn't work in sharded environments
|
||||||
- Added where() - filter to allowing users to specify query expressions as Javascript
|
- Added where() - filter to allowing users to specify query expressions as Javascript
|
||||||
|
@ -16,6 +16,9 @@ class NotRegistered(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidDocumentError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class ValidationError(Exception):
|
class ValidationError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -388,6 +391,8 @@ class DocumentMetaclass(type):
|
|||||||
attrs['_db_field_map'] = dict([(k, v.db_field) for k, v in doc_fields.items() if k!=v.db_field])
|
attrs['_db_field_map'] = dict([(k, v.db_field) for k, v in doc_fields.items() if k!=v.db_field])
|
||||||
attrs['_reverse_db_field_map'] = dict([(v, k) for k, v in attrs['_db_field_map'].items()])
|
attrs['_reverse_db_field_map'] = dict([(v, k) for k, v in attrs['_db_field_map'].items()])
|
||||||
|
|
||||||
|
from mongoengine import Document
|
||||||
|
|
||||||
new_class = super_new(cls, name, bases, attrs)
|
new_class = super_new(cls, name, bases, attrs)
|
||||||
for field in new_class._fields.values():
|
for field in new_class._fields.values():
|
||||||
field.owner_document = new_class
|
field.owner_document = new_class
|
||||||
@ -396,6 +401,9 @@ class DocumentMetaclass(type):
|
|||||||
field.document_type.register_delete_rule(new_class, field.name,
|
field.document_type.register_delete_rule(new_class, field.name,
|
||||||
delete_rule)
|
delete_rule)
|
||||||
|
|
||||||
|
if field.name and hasattr(Document, field.name):
|
||||||
|
raise InvalidDocumentError("%s is a document method and not a valid field name" % field.name)
|
||||||
|
|
||||||
module = attrs.get('__module__')
|
module = attrs.get('__module__')
|
||||||
|
|
||||||
base_excs = tuple(base.DoesNotExist for base in bases
|
base_excs = tuple(base.DoesNotExist for base in bases
|
||||||
|
@ -12,7 +12,7 @@ import weakref
|
|||||||
from fixtures import Base, Mixin, PickleEmbedded, PickleTest
|
from fixtures import Base, Mixin, PickleEmbedded, PickleTest
|
||||||
|
|
||||||
from mongoengine import *
|
from mongoengine import *
|
||||||
from mongoengine.base import _document_registry, NotRegistered
|
from mongoengine.base import _document_registry, NotRegistered, InvalidDocumentError
|
||||||
from mongoengine.connection import _get_db
|
from mongoengine.connection import _get_db
|
||||||
|
|
||||||
|
|
||||||
@ -2336,6 +2336,15 @@ class DocumentTest(unittest.TestCase):
|
|||||||
pickle_doc.reload()
|
pickle_doc.reload()
|
||||||
self.assertEquals(resurrected, pickle_doc)
|
self.assertEquals(resurrected, pickle_doc)
|
||||||
|
|
||||||
|
def throw_invalid_document_error(self):
|
||||||
|
|
||||||
|
# test handles people trying to upsert
|
||||||
|
def throw_invalid_document_error():
|
||||||
|
class Blog(Document):
|
||||||
|
validate = DictField()
|
||||||
|
|
||||||
|
self.assertRaises(InvalidDocumentError, throw_invalid_document_error)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user