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 ...@@ -4,7 +4,7 @@ from os import unlink, path as osp
from yams.schema import role_name from yams.schema import role_name
from cubicweb import Binary from cubicweb import Binary
from cubicweb.server.hook import Operation from cubicweb.server import hook
def set_attribute_storage(repo, etype, attr, storage): def set_attribute_storage(repo, etype, attr, storage):
repo.system_source.set_storage(etype, attr, storage) repo.system_source.set_storage(etype, attr, storage)
...@@ -99,7 +99,7 @@ class BytesFileSystemStorage(Storage): ...@@ -99,7 +99,7 @@ class BytesFileSystemStorage(Storage):
# bytes storage used to store file's path # bytes storage used to store file's path
entity[attr] = Binary(fpath) entity[attr] = Binary(fpath)
file(fpath, 'w').write(binary.getvalue()) file(fpath, 'w').write(binary.getvalue())
AddFileOp(entity._cw, filepath=fpath) hook.set_operation(entity._cw, 'bfss_added', fpath, AddFileOp)
return binary return binary
def entity_updated(self, entity, attr): def entity_updated(self, entity, attr):
...@@ -108,7 +108,8 @@ class BytesFileSystemStorage(Storage): ...@@ -108,7 +108,8 @@ class BytesFileSystemStorage(Storage):
oldpath = self.current_fs_path(entity, attr) oldpath = self.current_fs_path(entity, attr)
fpath = entity[attr].getvalue() fpath = entity[attr].getvalue()
if oldpath != fpath: if oldpath != fpath:
DeleteFileOp(entity._cw, filepath=oldpath) hook.set_operation(entity._cw, 'bfss_deleted', oldpath,
DeleteFileOp)
binary = Binary(file(fpath).read()) binary = Binary(file(fpath).read())
else: else:
binary = entity.pop(attr) binary = entity.pop(attr)
...@@ -118,7 +119,8 @@ class BytesFileSystemStorage(Storage): ...@@ -118,7 +119,8 @@ class BytesFileSystemStorage(Storage):
def entity_deleted(self, entity, attr): def entity_deleted(self, entity, attr):
"""an entity using this storage for attr has been deleted""" """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): def new_fs_path(self, entity, attr):
# We try to get some hint about how to name the file using attribute's # We try to get some hint about how to name the file using attribute's
...@@ -149,21 +151,24 @@ class BytesFileSystemStorage(Storage): ...@@ -149,21 +151,24 @@ class BytesFileSystemStorage(Storage):
binarywrap=str) binarywrap=str)
class AddFileOp(Operation):
class AddFileOp(hook.Operation):
def rollback_event(self): def rollback_event(self):
try: for filepath in self.session.transaction_data.pop('bfss_added'):
unlink(self.filepath) try:
except: unlink(filepath)
pass except Exception, ex:
self.error('cant remove %s: %s' % (filepath, ex))
class DeleteFileOp(Operation): class DeleteFileOp(hook.Operation):
def commit_event(self): def commit_event(self):
try: for filepath in self.session.transaction_data.pop('bfss_deleted'):
unlink(self.filepath) try:
except: unlink(filepath)
pass except Exception, ex:
self.error('cant remove %s: %s' % (filepath, ex))
class UpdateFileOp(Operation): class UpdateFileOp(hook.Operation):
def precommit_event(self): def precommit_event(self):
try: try:
file(self.filepath, 'w').write(self.filedata) 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