server/sources/storages.py
branchstable
changeset 7695 2f6e37661cf6
parent 7481 23ae090fc6a4
parent 7694 bd56a29acaa8
child 8131 a6654712ad50
equal deleted inserted replaced
7692:b37f4fdc44d4 7695:2f6e37661cf6
   146         if entity._cw.transaction_data.get('fs_importing'):
   146         if entity._cw.transaction_data.get('fs_importing'):
   147             # If we are importing from the filesystem, the file already exists.
   147             # If we are importing from the filesystem, the file already exists.
   148             # We do not need to create it but we need to fetch the content of
   148             # We do not need to create it but we need to fetch the content of
   149             # the file as the actual content of the attribute
   149             # the file as the actual content of the attribute
   150             fpath = entity.cw_edited[attr].getvalue()
   150             fpath = entity.cw_edited[attr].getvalue()
       
   151             assert fpath is not None
   151             binary = Binary(file(fpath, 'rb').read())
   152             binary = Binary(file(fpath, 'rb').read())
   152         else:
   153         else:
   153             # We must store the content of the attributes
   154             # We must store the content of the attributes
   154             # into a file to stay consistent with the behaviour of entity_add.
   155             # into a file to stay consistent with the behaviour of entity_add.
   155             # Moreover, the BytesFileSystemStorage expects to be able to
   156             # Moreover, the BytesFileSystemStorage expects to be able to
   158             # and keep the old one that will be removed on commit if everything
   159             # and keep the old one that will be removed on commit if everything
   159             # went ok.
   160             # went ok.
   160             #
   161             #
   161             # fetch the current attribute value in memory
   162             # fetch the current attribute value in memory
   162             binary = entity.cw_edited.pop(attr)
   163             binary = entity.cw_edited.pop(attr)
   163             # Get filename for it
   164             if binary is None:
   164             fpath = self.new_fs_path(entity, attr)
   165                 fpath = None
   165             assert not osp.exists(fpath)
   166             else:
   166             # write attribute value on disk
   167                 # Get filename for it
   167             file(fpath, 'wb').write(binary.getvalue())
   168                 fpath = self.new_fs_path(entity, attr)
   168             # Mark the new file as added during the transaction.
   169                 assert not osp.exists(fpath)
   169             # The file will be removed on rollback
   170                 # write attribute value on disk
   170             AddFileOp.get_instance(entity._cw).add_data(fpath)
   171                 file(fpath, 'wb').write(binary.getvalue())
       
   172                 # Mark the new file as added during the transaction.
       
   173                 # The file will be removed on rollback
       
   174                 AddFileOp.get_instance(entity._cw).add_data(fpath)
   171         if oldpath != fpath:
   175         if oldpath != fpath:
   172             # register the new location for the file.
   176             # register the new location for the file.
   173             entity.cw_edited.edited_attribute(attr, Binary(fpath))
   177             if fpath is None:
       
   178                 entity.cw_edited.edited_attribute(attr, None)
       
   179             else:
       
   180                 entity.cw_edited.edited_attribute(attr, Binary(fpath))
   174             # Mark the old file as useless so the file will be removed at
   181             # Mark the old file as useless so the file will be removed at
   175             # commit.
   182             # commit.
   176             if oldpath is not None:
   183             if oldpath is not None:
   177                 DeleteFileOp.get_instance(entity._cw).add_data(oldpath)
   184                 DeleteFileOp.get_instance(entity._cw).add_data(oldpath)
   178         return binary
   185         return binary