Created ObjectIdField, removed object_id field parameter

This commit is contained in:
Harry Marr
2009-11-18 21:38:41 +00:00
parent ff5e5addf5
commit 94be32b387
5 changed files with 56 additions and 18 deletions

View File

@@ -1,5 +1,7 @@
from collection import CollectionManager
import pymongo
class ValidationError(Exception):
pass
@@ -65,6 +67,25 @@ class BaseField(object):
pass
class ObjectIdField(BaseField):
"""An field wrapper around MongoDB's ObjectIds.
"""
def _to_python(self, value):
return str(value)
def _to_mongo(self, value):
if not isinstance(value, pymongo.objectid.ObjectId):
return pymongo.objectid.ObjectId(value)
return value
def _validate(self, value):
try:
pymongo.objectid.ObjectId(str(value))
except:
raise ValidationError('Invalid Object ID')
class DocumentMetaclass(type):
"""Metaclass for all documents.
"""
@@ -76,7 +97,6 @@ class DocumentMetaclass(type):
return super_new(cls, name, bases, attrs)
doc_fields = {}
# Include all fields present in superclasses
for base in bases:
if hasattr(base, '_fields'):
@@ -85,7 +105,6 @@ class DocumentMetaclass(type):
# Add the document's fields to the _fields attribute
for attr_name, attr_value in attrs.items():
if issubclass(attr_value.__class__, BaseField):
#print attr_value.name
if not attr_value.name:
attr_value.name = attr_name
doc_fields[attr_name] = attr_value
@@ -114,22 +133,15 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
for base in bases:
if hasattr(base, '_meta') and 'collection' in base._meta:
collection = base._meta['collection']
# Get primary key field
object_id_field = None
for attr_name, attr_value in attrs.items():
if issubclass(attr_value.__class__, BaseField):
if hasattr(attr_value, 'object_id') and attr_value.object_id:
object_id_field = attr_name
attr_value.required = True
meta = {
'collection': collection,
'object_id_field': object_id_field,
}
meta.update(attrs.get('meta', {}))
attrs['_meta'] = meta
attrs['_id'] = ObjectIdField()
# Set up collection manager, needs the class to have fields so use
# DocumentMetaclass before instantiating CollectionManager object
new_class = super_new(cls, name, bases, attrs)

View File

@@ -10,7 +10,6 @@ class CollectionManager(object):
self._collection_name = document._meta['collection']
# This will create the collection if it doesn't exist
self._collection = db[self._collection_name]
self._id_field = document._meta['object_id_field']
def _save_document(self, document):
"""Save the provided document to the collection.

View File

@@ -1,20 +1,20 @@
from base import BaseField, ValidationError
from base import BaseField, ObjectIdField, ValidationError
from document import EmbeddedDocument
import re
import pymongo
__all__ = ['StringField', 'IntField', 'EmbeddedDocumentField',
'ValidationError']
'ObjectIdField', 'ValidationError']
class StringField(BaseField):
"""A unicode string field.
"""
def __init__(self, regex=None, max_length=None, object_id=False, **kwargs):
def __init__(self, regex=None, max_length=None, **kwargs):
self.regex = re.compile(regex) if regex else None
self.object_id = object_id
self.max_length = max_length
super(StringField, self).__init__(**kwargs)