From 939119ab1bd5e8570699407dd99a72ba91e0709c Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Wed, 21 Feb 2024 12:54:13 +0100 Subject: [PATCH] [FIX] fs_file: Support for empty file Before this change the creation of empty file was not supported. The issue was mainly due to the fact that at create of a FSFileValue instance with a name but without content, the name was no preserved. As result, the insert of the attachement into the DB failed since the name is a required field. If a FSFileValue instance is now created without content but with a name, the name is now preserved on an empty buffer. --- fs_file/README.rst | 2 +- fs_file/fields.py | 5 ++++- fs_file/readme/newsfragments/341.bugfix | 7 +++++++ fs_file/static/description/index.html | 2 +- fs_file/tests/test_fs_file.py | 15 +++++++++++++++ fs_image/README.rst | 2 +- fs_image/readme/newsfragments/305.bugfix | 0 fs_image/static/description/index.html | 2 +- 8 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 fs_file/readme/newsfragments/341.bugfix create mode 100644 fs_image/readme/newsfragments/305.bugfix diff --git a/fs_file/README.rst b/fs_file/README.rst index 3fd5241f66..a6d36aa9cf 100644 --- a/fs_file/README.rst +++ b/fs_file/README.rst @@ -7,7 +7,7 @@ Fs File !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:e85a98065914bf2696dd3d083e4b5eab6b9d91d8c3ce69ba63cb5abef93587f7 + !! source digest: sha256:ba969b14fbeee8a8a16e0297d2468467a7140cb7a51b84281269f8ee5137ae2b !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png diff --git a/fs_file/fields.py b/fs_file/fields.py index ae70125c5a..b11ba93c3f 100644 --- a/fs_file/fields.py +++ b/fs_file/fields.py @@ -55,6 +55,9 @@ def __init__( self._buffer.name = name else: raise ValueError("value must be bytes or io.BytesIO") + elif name: + self._buffer = BytesIO(b"") + self._buffer.name = name @property def write_buffer(self) -> BytesIO: @@ -164,7 +167,7 @@ def read_buffer(self) -> BytesIO: content = b"" name = None if self._attachment: - content = self._attachment.raw + content = self._attachment.raw or b"" name = self._attachment.name self._buffer = BytesIO(content) self._buffer.name = name diff --git a/fs_file/readme/newsfragments/341.bugfix b/fs_file/readme/newsfragments/341.bugfix new file mode 100644 index 0000000000..2ad6b8d9ca --- /dev/null +++ b/fs_file/readme/newsfragments/341.bugfix @@ -0,0 +1,7 @@ +Fixes the creation of empty files. + +Before this change, the creation of empty files resulted in a constraint +violation error. This was due to the fact that even if a name was given +to the file it was not preserved into the FSFileValue object if no content +was given. As result, when the corresponding ir.attachment was created in +the database, the name was not set and the 'required' constraint was violated. diff --git a/fs_file/static/description/index.html b/fs_file/static/description/index.html index 8b0f50ecdd..368fefec81 100644 --- a/fs_file/static/description/index.html +++ b/fs_file/static/description/index.html @@ -367,7 +367,7 @@

Fs File

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:e85a98065914bf2696dd3d083e4b5eab6b9d91d8c3ce69ba63cb5abef93587f7 +!! source digest: sha256:ba969b14fbeee8a8a16e0297d2468467a7140cb7a51b84281269f8ee5137ae2b !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Alpha License: AGPL-3 OCA/storage Translate me on Weblate Try me on Runboat

This addon defines a new field type FSFile which is a file field that stores diff --git a/fs_file/tests/test_fs_file.py b/fs_file/tests/test_fs_file.py index 2c105405e2..60111f63b8 100644 --- a/fs_file/tests/test_fs_file.py +++ b/fs_file/tests/test_fs_file.py @@ -145,6 +145,21 @@ def test_write_with_io(self): self.assertTrue(isinstance(instance.fs_file, FSFileValue)) self.assertEqual(instance.fs_file.getvalue(), b"test3") + def test_create_with_empty_value(self): + instance = self.env["test.model"].create( + {"fs_file": FSFileValue(name=self.filename, value=b"")} + ) + self.assertEqual(instance.fs_file.getvalue(), b"") + self.assertEqual(instance.fs_file.name, self.filename) + + def test_write_with_empty_value(self): + instance = self.env["test.model"].create( + {"fs_file": FSFileValue(name=self.filename, value=self.create_content)} + ) + instance.write({"fs_file": FSFileValue(name=self.filename, value=b"")}) + self.assertEqual(instance.fs_file.getvalue(), b"") + self.assertEqual(instance.fs_file.name, self.filename) + def test_modify_fsfilebytesio(self): """If you modify the content of the FSFileValue, the changes will be directly applied diff --git a/fs_image/README.rst b/fs_image/README.rst index 37a88a8f0f..68af2ad3fd 100644 --- a/fs_image/README.rst +++ b/fs_image/README.rst @@ -7,7 +7,7 @@ Fs Image !! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - !! source digest: sha256:12aa6d80ddfd410f208443e5310a2f25fff947aa627b08d582649dd2187fa471 + !! source digest: sha256:73f0f6a521e4fb7c934a122c5ed688cfe01b0113eb11d560b9440b1eb7b7b58c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! .. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png diff --git a/fs_image/readme/newsfragments/305.bugfix b/fs_image/readme/newsfragments/305.bugfix new file mode 100644 index 0000000000..e69de29bb2 diff --git a/fs_image/static/description/index.html b/fs_image/static/description/index.html index 3d38bf187a..b61c599956 100644 --- a/fs_image/static/description/index.html +++ b/fs_image/static/description/index.html @@ -367,7 +367,7 @@

Fs Image

!! This file is generated by oca-gen-addon-readme !! !! changes will be overwritten. !! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -!! source digest: sha256:12aa6d80ddfd410f208443e5310a2f25fff947aa627b08d582649dd2187fa471 +!! source digest: sha256:73f0f6a521e4fb7c934a122c5ed688cfe01b0113eb11d560b9440b1eb7b7b58c !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

Alpha License: AGPL-3 OCA/storage Translate me on Weblate Try me on Runboat

This addon defines a new field FSImage to use in your models. It is a