Esempio n. 1
0
    def update(self, obj, set_fields = None, unset_fields = None, update_obj = True):
        """
        We return the result of the save method (updates are not yet implemented here).
        """
        if set_fields:
            if isinstance(set_fields,(list,tuple)):
                set_attributes = {}
                for key in set_fields:
                    try:
                        set_attributes[key] = get_value(obj,key)
                    except KeyError:
                        pass
            else:
                set_attributes = set_fields
        else:
            set_attributes = {}
        if unset_fields:
            unset_attributes = unset_fields
        else:
            unset_attributes = []

        self.call_hook('before_update',obj,set_attributes,unset_attributes)

        if update_obj:
            for key,value in set_attributes.items():
                set_value(obj,key,value)
            for key in unset_attributes:
                delete_value(obj,key)

        return self.save(obj,call_hook = False)
Esempio n. 2
0
    def update(self, obj, set_fields=None, unset_fields=None, update_obj=True):

        collection = self.get_collection_for_cls(obj.__class__)

        if obj.pk == None:
            raise obj.DoesNotExist("update() called on document without primary key!")

        def serialize_fields(fields):


            if isinstance(fields, (list,tuple)):
                update_dict = {}
                for key in fields:
                    try:
                        update_dict[key] = get_value(obj,key)
                    except KeyError:
                        pass
            elif isinstance(fields,dict):
                update_dict = fields.copy()
            else:
                raise TypeError("fields must be a list/tuple!")

            return update_dict

        if set_fields:
            set_attributes = serialize_fields(set_fields)
        else:
            set_attributes = {}

        if unset_fields:
            unset_attributes = list(unset_fields)
        else:
            unset_attributes = []

        self.call_hook('before_update',obj,set_attributes,unset_attributes)


        set_attributes = {key : self.serialize(value)
                                for key,value in set_attributes.items()}

        if update_obj:
            for key,value in set_attributes.items():
                set_value(obj,key,value)
            for key in unset_attributes:
                delete_value(obj,key)

        update_dict = {}

        if set_attributes:
            update_dict['$set'] = set_attributes
        if unset_attributes:
            update_dict['$unset'] = {key : '' for key in unset_attributes}

        if not update_dict:
            return #nothing to do...

        if self.autocommit:
            self.db[collection].update({'_id': obj.pk}, update_dict)
        else:
            if obj.pk in self._delete_cache[collection]:
                raise obj.DoesNotExist("update() on document that is marked for deletion!")
            if obj.pk in self._update_cache[collection]:
                update_cache = self._update_cache[collection][obj.pk]
                if set_attributes:
                    if '$set' not in update_cache:
                        update_cache['$set'] = {}
                    for key, value in set_attributes.items():
                        if '$unset' in update_cache and key in update_cache['$unset']:
                            del update_cache['$unset'][key]
                        update_cache['$set'][key] = value
                if unset_attributes:
                    if '$unset' not in update_cache:
                        update_cache['$unset'] = {}
                    for key in unset_attributes:
                        if '$set' in update_cache and key in update_cache['$set']:
                            del update_cache['$set'][key]
                        update_cache['$unset'][key] = ''
            else:
                self._update_cache[collection][obj.pk] = update_dict
Esempio n. 3
0
    def update(self, obj, set_fields=None, unset_fields=None, update_obj=True):

        collection = self.get_collection_for_cls(obj.__class__)

        if obj.pk == None:
            raise obj.DoesNotExist("update() called on document without primary key!")

        def serialize_fields(fields):


            if isinstance(fields, (list,tuple)):
                update_dict = {}
                for key in fields:
                    try:
                        update_dict[key] = get_value(obj,key)
                    except KeyError:
                        pass
            elif isinstance(fields,dict):
                update_dict = fields.copy()
            else:
                raise TypeError("fields must be a list/tuple!")

            return update_dict

        if set_fields:
            set_attributes = serialize_fields(set_fields)
        else:
            set_attributes = {}

        if unset_fields:
            unset_attributes = list(unset_fields)
        else:
            unset_attributes = []

        self.call_hook('before_update',obj,set_attributes,unset_attributes)


        set_attributes = {key : self.serialize(value)
                                for key,value in set_attributes.items()}

        if update_obj:
            for key,value in set_attributes.items():
                set_value(obj,key,value)
            for key in unset_attributes:
                delete_value(obj,key)

        update_dict = {}

        if set_attributes:
            update_dict['$set'] = set_attributes
        if unset_attributes:
            update_dict['$unset'] = {key : '' for key in unset_attributes}

        if not update_dict:
            return #nothing to do...

        if self.autocommit:
            self.db[collection].update({'_id': obj.pk}, update_dict)
        else:
            if obj.pk in self._delete_cache[collection]:
                raise obj.DoesNotExist("update() on document that is marked for deletion!")
            if obj.pk in self._update_cache[collection]:
                update_cache = self._update_cache[collection][obj.pk]
                if set_attributes:
                    if '$set' not in update_cache:
                        update_cache['$set'] = {}
                    for key, value in set_attributes.items():
                        if '$unset' in update_cache and key in update_cache['$unset']:
                            del update_cache['$unset'][key]
                        update_cache['$set'][key] = value
                if unset_attributes:
                    if '$unset' not in update_cache:
                        update_cache['$unset'] = {}
                    for key in unset_attributes:
                        if '$set' in update_cache and key in update_cache['$set']:
                            del update_cache['$set'][key]
                        update_cache['$unset'][key] = ''
            else:
                self._update_cache[collection][obj.pk] = update_dict