Added InvalidDocumentError

Ensures defined documents are valid and users don't override core methods
by accident.

fixes #275
This commit is contained in:
Ross Lawley 2011-09-09 17:36:40 +01:00
parent 60f0491f62
commit 050542c29b
3 changed files with 20 additions and 2 deletions

View File

@ -5,7 +5,8 @@ Changelog
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
- 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

View File

@ -16,6 +16,9 @@ class NotRegistered(Exception):
pass
class InvalidDocumentError(Exception):
pass
class ValidationError(Exception):
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['_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)
for field in new_class._fields.values():
field.owner_document = new_class
@ -396,6 +401,9 @@ class DocumentMetaclass(type):
field.document_type.register_delete_rule(new_class, field.name,
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__')
base_excs = tuple(base.DoesNotExist for base in bases

View File

@ -12,7 +12,7 @@ import weakref
from fixtures import Base, Mixin, PickleEmbedded, PickleTest
from mongoengine import *
from mongoengine.base import _document_registry, NotRegistered
from mongoengine.base import _document_registry, NotRegistered, InvalidDocumentError
from mongoengine.connection import _get_db
@ -2336,6 +2336,15 @@ class DocumentTest(unittest.TestCase):
pickle_doc.reload()
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__':
unittest.main()