[bfss] use set_operation for AddFileOp/DeleteFileOp
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 23 Apr 2010 17:54:34 +0200
changeset 5396 78d92a47a4e5
parent 5387 aed0c441923a
child 5397 cdbf823450aa
[bfss] use set_operation for AddFileOp/DeleteFileOp
server/sources/storages.py
--- a/server/sources/storages.py	Fri Apr 23 13:25:31 2010 +0200
+++ b/server/sources/storages.py	Fri Apr 23 17:54:34 2010 +0200
@@ -4,7 +4,7 @@
 from yams.schema import role_name
 
 from cubicweb import Binary
-from cubicweb.server.hook import Operation
+from cubicweb.server import hook
 
 def set_attribute_storage(repo, etype, attr, storage):
     repo.system_source.set_storage(etype, attr, storage)
@@ -99,7 +99,7 @@
             # bytes storage used to store file's path
             entity[attr] = Binary(fpath)
             file(fpath, 'w').write(binary.getvalue())
-            AddFileOp(entity._cw, filepath=fpath)
+            hook.set_operation(entity._cw, 'bfss_added', fpath, AddFileOp)
         return binary
 
     def entity_updated(self, entity, attr):
@@ -108,7 +108,8 @@
             oldpath = self.current_fs_path(entity, attr)
             fpath = entity[attr].getvalue()
             if oldpath != fpath:
-                DeleteFileOp(entity._cw, filepath=oldpath)
+                hook.set_operation(entity._cw, 'bfss_deleted', oldpath,
+                                   DeleteFileOp)
             binary = Binary(file(fpath).read())
         else:
             binary = entity.pop(attr)
@@ -118,7 +119,8 @@
 
     def entity_deleted(self, entity, attr):
         """an entity using this storage for attr has been deleted"""
-        DeleteFileOp(entity._cw, filepath=self.current_fs_path(entity, attr))
+        fpath = self.current_fs_path(entity, attr)
+        hook.set_operation(entity._cw, 'bfss_deleted', fpath, DeleteFileOp)
 
     def new_fs_path(self, entity, attr):
         # We try to get some hint about how to name the file using attribute's
@@ -149,21 +151,24 @@
                                        binarywrap=str)
 
 
-class AddFileOp(Operation):
+
+class AddFileOp(hook.Operation):
     def rollback_event(self):
-        try:
-            unlink(self.filepath)
-        except:
-            pass
+        for filepath in self.session.transaction_data.pop('bfss_added'):
+            try:
+                unlink(filepath)
+            except Exception, ex:
+                self.error('cant remove %s: %s' % (filepath, ex))
 
-class DeleteFileOp(Operation):
+class DeleteFileOp(hook.Operation):
     def commit_event(self):
-        try:
-            unlink(self.filepath)
-        except:
-            pass
+        for filepath in self.session.transaction_data.pop('bfss_deleted'):
+            try:
+                unlink(filepath)
+            except Exception, ex:
+                self.error('cant remove %s: %s' % (filepath, ex))
 
-class UpdateFileOp(Operation):
+class UpdateFileOp(hook.Operation):
     def precommit_event(self):
         try:
             file(self.filepath, 'w').write(self.filedata)