server/sources/storages.py
branchstable
changeset 8743 27a83746aebd
parent 8695 358d8bed9626
child 8900 010a59e12d89
equal deleted inserted replaced
8742:bd374bd906f3 8743:27a83746aebd
     1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
     3 #
     3 #
     4 # This file is part of CubicWeb.
     4 # This file is part of CubicWeb.
     5 #
     5 #
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
   142         accessed
   142         accessed
   143         """
   143         """
   144         fpath = source.binary_to_str(value)
   144         fpath = source.binary_to_str(value)
   145         try:
   145         try:
   146             return Binary.from_file(fpath)
   146             return Binary.from_file(fpath)
   147         except EnvironmentError, ex:
   147         except EnvironmentError as ex:
   148             source.critical("can't open %s: %s", value, ex)
   148             source.critical("can't open %s: %s", value, ex)
   149             return None
   149             return None
   150 
   150 
   151     def entity_added(self, entity, attr):
   151     def entity_added(self, entity, attr):
   152         """an entity using this storage for attr has been added"""
   152         """an entity using this storage for attr has been added"""
   153         if entity._cw.transaction_data.get('fs_importing'):
   153         if entity._cw.transaction_data.get('fs_importing'):
   154             binary = Binary.from_file(entity.cw_edited[attr].getvalue())
   154             binary = Binary.from_file(entity.cw_edited[attr].getvalue())
       
   155             entity._cw_dont_cache_attribute(attr, repo_side=True)
   155         else:
   156         else:
   156             binary = entity.cw_edited.pop(attr)
   157             binary = entity.cw_edited.pop(attr)
   157             fpath = self.new_fs_path(entity, attr)
   158             fpath = self.new_fs_path(entity, attr)
   158             # bytes storage used to store file's path
   159             # bytes storage used to store file's path
   159             entity.cw_edited.edited_attribute(attr, Binary(fpath))
   160             entity.cw_edited.edited_attribute(attr, Binary(fpath))
   168         if entity._cw.transaction_data.get('fs_importing'):
   169         if entity._cw.transaction_data.get('fs_importing'):
   169             # If we are importing from the filesystem, the file already exists.
   170             # If we are importing from the filesystem, the file already exists.
   170             # We do not need to create it but we need to fetch the content of
   171             # We do not need to create it but we need to fetch the content of
   171             # the file as the actual content of the attribute
   172             # the file as the actual content of the attribute
   172             fpath = entity.cw_edited[attr].getvalue()
   173             fpath = entity.cw_edited[attr].getvalue()
       
   174             entity._cw_dont_cache_attribute(attr, repo_side=True)
   173             assert fpath is not None
   175             assert fpath is not None
   174             binary = Binary.from_file(fpath)
   176             binary = Binary.from_file(fpath)
   175         else:
   177         else:
   176             # We must store the content of the attributes
   178             # We must store the content of the attributes
   177             # into a file to stay consistent with the behaviour of entity_add.
   179             # into a file to stay consistent with the behaviour of entity_add.
   260 class AddFileOp(hook.DataOperationMixIn, hook.Operation):
   262 class AddFileOp(hook.DataOperationMixIn, hook.Operation):
   261     def rollback_event(self):
   263     def rollback_event(self):
   262         for filepath in self.get_data():
   264         for filepath in self.get_data():
   263             try:
   265             try:
   264                 unlink(filepath)
   266                 unlink(filepath)
   265             except Exception, ex:
   267             except Exception as ex:
   266                 self.error('cant remove %s: %s' % (filepath, ex))
   268                 self.error('cant remove %s: %s' % (filepath, ex))
   267 
   269 
   268 class DeleteFileOp(hook.DataOperationMixIn, hook.Operation):
   270 class DeleteFileOp(hook.DataOperationMixIn, hook.Operation):
   269     def postcommit_event(self):
   271     def postcommit_event(self):
   270         for filepath in self.get_data():
   272         for filepath in self.get_data():
   271             try:
   273             try:
   272                 unlink(filepath)
   274                 unlink(filepath)
   273             except Exception, ex:
   275             except Exception as ex:
   274                 self.error('cant remove %s: %s' % (filepath, ex))
   276                 self.error('cant remove %s: %s' % (filepath, ex))