Commit f16266d1 authored by Sylvain Thénault's avatar Sylvain Thénault
Browse files

[bfss] use set_operation for AddFileOp/DeleteFileOp

parent aed0c441923a
......@@ -4,7 +4,7 @@ from os import unlink, path as osp
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 @@ class BytesFileSystemStorage(Storage):
# 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 @@ class BytesFileSystemStorage(Storage):
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 @@ class BytesFileSystemStorage(Storage):
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 @@ class BytesFileSystemStorage(Storage):
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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment