def force_save_fields(self, field_names): """ Save all fields that are specified in `field_names`, even if they are not dirty. """ fields = [self.fields[field_name] for field_name in field_names] fields_to_save_json = {} for field in fields: fields_to_save_json[field.name] = field.to_json( self._field_data_cache[field.name]) try: # Throws KeyValueMultiSaveError if things go wrong self._field_data.set_many(self, fields_to_save_json) except KeyValueMultiSaveError as save_error: saved_fields = [ field for field in fields if field.name in save_error.saved_field_names ] # pylint: disable=exception-escape for field in saved_fields: # should only find one corresponding field fields.remove(field) # if the field was dirty, delete from dirty fields self._reset_dirty_field(field) msg = 'Error saving fields {}'.format(save_error.saved_field_names) raise XBlockSaveError(saved_fields, fields, msg) # Remove all dirty fields, since the save was successful for field in fields: self._reset_dirty_field(field)
def save(self): """Save all dirty fields attached to this XBlock.""" if not self._dirty_fields: # nop if _dirty_fields attribute is empty return try: fields_to_save = self._get_fields_to_save() # Throws KeyValueMultiSaveError if things go wrong self._field_data.set_many(self, fields_to_save) except KeyValueMultiSaveError as save_error: saved_fields = [field for field in self._dirty_fields if field.name in save_error.saved_field_names] for field in saved_fields: # should only find one corresponding field del self._dirty_fields[field] raise XBlockSaveError(saved_fields, self._dirty_fields.keys()) # Remove all dirty fields, since the save was successful self._clear_dirty_fields()