reset to master, keep working on the dirty-fields-patch in another branch
This commit is contained in:
parent
963a223e7e
commit
73aff806f3
@ -15,14 +15,13 @@ class BaseField(object):
|
||||
_index_with_types = True
|
||||
|
||||
def __init__(self, name=None, required=False, default=None, unique=False,
|
||||
unique_with=None, primary_key=False, modified=False):
|
||||
unique_with=None, primary_key=False):
|
||||
self.name = name if not primary_key else '_id'
|
||||
self.required = required or primary_key
|
||||
self.default = default
|
||||
self.unique = bool(unique or unique_with)
|
||||
self.unique_with = unique_with
|
||||
self.primary_key = primary_key
|
||||
self.modified = modified
|
||||
|
||||
def __get__(self, instance, owner):
|
||||
"""Descriptor for retrieving a value from a field in a document. Do
|
||||
@ -45,7 +44,6 @@ class BaseField(object):
|
||||
"""Descriptor for assigning a value to a field in a document.
|
||||
"""
|
||||
instance._data[self.name] = value
|
||||
self.modified = True
|
||||
|
||||
def to_python(self, value):
|
||||
"""Convert a MongoDB-compatible type to a Python type.
|
||||
@ -254,11 +252,8 @@ class BaseDocument(object):
|
||||
|
||||
def __init__(self, **values):
|
||||
self._data = {}
|
||||
|
||||
modified = 'id' in values.keys()
|
||||
# Assign initial values to instance
|
||||
for attr_name, attr_value in self._fields.items():
|
||||
attr_value.modified = modified
|
||||
if attr_name in values:
|
||||
setattr(self, attr_name, values.pop(attr_name))
|
||||
else:
|
||||
|
@ -1,9 +1,8 @@
|
||||
from base import (DocumentMetaclass, TopLevelDocumentMetaclass, BaseDocument,
|
||||
ValidationError)
|
||||
from queryset import OperationError, QuerySet
|
||||
from queryset import OperationError
|
||||
from connection import _get_db
|
||||
|
||||
|
||||
import pymongo
|
||||
|
||||
|
||||
@ -76,34 +75,12 @@ class Document(BaseDocument):
|
||||
if force_insert:
|
||||
object_id = collection.insert(doc, safe=safe)
|
||||
else:
|
||||
if getattr(self, 'id', None) == None:
|
||||
# new document
|
||||
object_id = collection.save(doc, safe=safe)
|
||||
else:
|
||||
# update document
|
||||
modified_fields = map(lambda obj: obj[0], filter(lambda obj: obj[1].modified, self._fields.items()))
|
||||
modified_doc = dict(filter(lambda k: k[0] in modified_fields, doc.items()))
|
||||
try:
|
||||
#
|
||||
# WORK IN PROGRESS
|
||||
# - EmbeddedDocuments still aren't tracked
|
||||
#
|
||||
id_field = self._meta['id_field']
|
||||
idObj = self._fields[id_field].to_mongo(self['id'])
|
||||
collection.update({'_id': idObj}, {'$set': modified_doc}, safe=safe)
|
||||
except pymongo.errors.OperationFailure, err:
|
||||
if str(err) == 'multi not coded yet':
|
||||
raise OperationError('update() method requires MongoDB 1.1.3+')
|
||||
raise OperationError('Update failed (%s)' % str(err))
|
||||
object_id = self['id']
|
||||
|
||||
for field in self._fields.values(): field.modified = False
|
||||
except pymongo.errors.OperationFailure, err:
|
||||
message = 'Could not save document (%s)'
|
||||
if 'duplicate key' in str(err):
|
||||
message = 'Tried to save duplicate unique keys (%s)'
|
||||
raise OperationError(message % str(err))
|
||||
|
||||
id_field = self._meta['id_field']
|
||||
self[id_field] = self._fields[id_field].to_python(object_id)
|
||||
|
||||
@ -129,7 +106,6 @@ class Document(BaseDocument):
|
||||
obj = self.__class__.objects(**{id_field: self[id_field]}).first()
|
||||
for field in self._fields:
|
||||
setattr(self, field, obj[field])
|
||||
obj.modified = False
|
||||
|
||||
@classmethod
|
||||
def drop_collection(cls):
|
||||
|
@ -71,7 +71,6 @@ class FloatField(BaseField):
|
||||
return float(value)
|
||||
|
||||
def validate(self, value):
|
||||
if isinstance(value, int): value = float(value)
|
||||
assert isinstance(value, float)
|
||||
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
|
Loading…
x
Reference in New Issue
Block a user