diff --git a/fs_file/README.rst b/fs_file/README.rst new file mode 100644 index 0000000000..38929e8775 --- /dev/null +++ b/fs_file/README.rst @@ -0,0 +1,35 @@ +**This file is going to be generated by oca-gen-addon-readme.** + +*Manual changes will be overwritten.* + +Please provide content in the ``readme`` directory: + +* **DESCRIPTION.rst** (required) +* INSTALL.rst (optional) +* CONFIGURE.rst (optional) +* **USAGE.rst** (optional, highly recommended) +* DEVELOP.rst (optional) +* ROADMAP.rst (optional) +* HISTORY.rst (optional, recommended) +* **CONTRIBUTORS.rst** (optional, highly recommended) +* CREDITS.rst (optional) + +Content of this README will also be drawn from the addon manifest, +from keys such as name, authors, maintainers, development_status, +and license. + +A good, one sentence summary in the manifest is also highly recommended. + + +Automatic changelog generation +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +`HISTORY.rst` can be auto generated using `towncrier `_. + +Just put towncrier compatible changelog fragments into `readme/newsfragments` +and the changelog file will be automatically generated and updated when a new fragment is added. + +Please refer to `towncrier` documentation to know more. + +NOTE: the changelog will be automatically generated when using `/ocabot merge $option`. +If you need to run it manually, refer to `OCA/maintainer-tools README `_. diff --git a/fs_file/__init__.py b/fs_file/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/fs_file/__manifest__.py b/fs_file/__manifest__.py new file mode 100644 index 0000000000..eb996d1786 --- /dev/null +++ b/fs_file/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Fs File", + "summary": """ + Field to store files into filesystem storages""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/storage", + "depends": ["fs_attachment"], + "data": [], + "demo": [], + "maintainers": ["lmignon"], + "development_status": "Alpha", + "assets": { + "web.assets_backend": [ + "fs_file/static/src/**/*", + ], + }, +} diff --git a/fs_file/fields.py b/fs_file/fields.py new file mode 100644 index 0000000000..7ec3cff2fa --- /dev/null +++ b/fs_file/fields.py @@ -0,0 +1,402 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +# pylint: disable=method-required-super +import base64 +import itertools +import os.path +from io import BytesIO, IOBase + +from odoo import fields + +from odoo.addons.fs_attachment.models.ir_attachment import IrAttachment + + +class FSFileValue: + def __init__( + self, + attachment: IrAttachment = None, + name: str = None, + value: bytes | IOBase = None, + ) -> None: + """ + This class holds the information related to FSFile field. It can be + used to assign a value to a FSFile field. In such a case, you can pass + the name and the file content as parameters. + + When + + :param attachment: the attachment to use to store the file. + :param name: the name of the file. If not provided, the name will be + taken from the attachment or the io.IOBase. + :param value: the content of the file. It can be bytes or an io.IOBase. + """ + self._is_new: bool = attachment is None + self._buffer: IOBase = None + self._attachment: IrAttachment = attachment + if name and attachment: + raise ValueError("Cannot set name and attachment at the same time") + if value: + if isinstance(value, IOBase): + self._buffer = value + if not hasattr(value, "name") and name: + self._buffer.name = name + elif not name: + raise ValueError( + "name must be set when value is an io.IOBase " + "and is not provided by the io.IOBase" + ) + elif isinstance(value, bytes): + self._buffer = BytesIO(value) + if not name: + raise ValueError("name must be set when value is bytes") + self._buffer.name = name + else: + raise ValueError("value must be bytes or io.BytesIO") + + @property + def write_buffer(self) -> BytesIO: + if self._buffer is None: + name = self._attachment.name if self._attachment else None + self._buffer = BytesIO() + self._buffer.name = name + return self._buffer + + @property + def name(self) -> str | None: + name = ( + self._attachment.name + if self._attachment + else self._buffer.name + if self._buffer + else None + ) + if name: + return os.path.basename(name) + return None + + @name.setter + def name(self, value: str) -> None: + # the name should only be updatable while the file is not yet stored + # TODO, we could also allow to update the name of the file and rename + # the file in the external file system + if self._is_new: + self.write_buffer.name = value + else: + raise ValueError( + "The name of the file can only be updated while the file is not " + "yet stored" + ) + + @property + def mimetype(self) -> str | None: + return self._attachment.mimetype if self._attachment else None + + @property + def size(self) -> int: + return self._attachment.file_size if self._attachment else len(self._buffer) + + @property + def url(self) -> str | None: + return self._attachment.url if self._attachment else None + + @property + def internal_url(self) -> str | None: + return self._attachment.internal_url if self._attachment else None + + @property + def attachment(self) -> IrAttachment | None: + return self._attachment + + @attachment.setter + def attachment(self, value: IrAttachment) -> None: + self._attachment = value + self._buffer = None + + @property + def read_buffer(self) -> BytesIO: + if self._buffer is None: + content = b"" + name = None + if self._attachment: + content = self._attachment.raw + name = self._attachment.name + self._buffer = BytesIO(content) + self._buffer.name = name + return self._buffer + + def getvalue(self) -> bytes: + buffer = self.read_buffer + current_pos = buffer.tell() + buffer.seek(0) + value = buffer.read() + buffer.seek(current_pos) + return value + + def open( + self, + mode="rb", + block_size=None, + cache_options=None, + compression=None, + new_version=True, + **kwargs + ) -> IOBase: + """ + Return a file-like object that can be used to read and write the file content. + See the documentation of open() into the ir.attachment model from the + fs_attachment module for more information. + """ + if not self._attachment: + raise ValueError("Cannot open a file that is not stored") + return self._attachment.open( + mode=mode, + block_size=block_size, + cache_options=cache_options, + compression=compression, + new_version=new_version, + **kwargs, + ) + + +class FSFile(fields.Binary): + """ + This field is a binary field that stores the file content in an external + filesystem storage referenced by a storage code. + + A major difference with the standard Odoo binary field is that the value + is not encoded in base64 but is a bytes object. + + Moreover, the field is designed to always return an instance of + :class:`FSFileValue` when reading the value. This class is a file-like + object that can be used to read the file content and to get information + about the file (filename, mimetype, url, ...). + + To update the value of the field, the following values are accepted: + + - a bytes object (e.g. ``b"..."``) + - a dict with the following keys: + - ``filename``: the filename of the file + - ``content``: the content of the file encoded in base64 + - a FSFileValue instance + - a file-like object (e.g. an instance of :class:`io.BytesIO`) + + When the value is provided is a bytes object the filename is set to the + name of the field. You can override this behavior by providing specifying + a fs_filename key in the context. For example: + + .. code-block:: python + + record.with_context(fs_filename='my_file.txt').write({ + 'field': b'...', + }) + + The same applies when the value is provided as a file-like object but the + filename is set to the name of the file-like object or not a property of + the file-like object. (e.g. ``io.BytesIO(b'...')``). + + + When the value is converted to the read format, it's always an instance of + dict with the following keys: + + - ``filename``: the filename of the file + - ``mimetype``: the mimetype of the file + - ``size``: the size of the file + - ``url``: the url to access the file + + """ + + type = "fs_file" + + attachment: bool = True + + def __init__(self, **kwargs): + kwargs["attachment"] = True + super().__init__(**kwargs) + + def read(self, records): + domain = [ + ("res_model", "=", records._name), + ("res_field", "=", self.name), + ("res_id", "in", records.ids), + ] + data = { + att.res_id: FSFileValue(attachment=att) + for att in records.env["ir.attachment"].sudo().search(domain) + } + records.env.cache.insert_missing(records, self, map(data.get, records._ids)) + + def create(self, record_values): + if not record_values: + return + env = record_values[0][0].env + with env.norecompute(): + ir_attachment = ( + env["ir.attachment"] + .sudo() + .with_context( + binary_field_real_user=env.user, + ) + ) + for record, value in record_values: + if value: + cache_value = self.convert_to_cache(value, record) + attachment = ir_attachment.create( + { + "name": cache_value.name, + "raw": cache_value.getvalue(), + "res_model": record._name, + "res_field": self.name, + "res_id": record.id, + "type": "binary", + } + ) + record.env.cache.update( + record, + self, + [FSFileValue(attachment=attachment)], + dirty=False, + ) + + def write(self, records, value): + # the code is copied from the standard Odoo Binary field + # with the following changes: + # - the value is not encoded in base64 and we therefore write on + # ir.attachment.raw instead of ir.attachment.datas + + # discard recomputation of self on records + records.env.remove_to_compute(self, records) + # update the cache, and discard the records that are not modified + cache = records.env.cache + cache_value = self.convert_to_cache(value, records) + records = cache.get_records_different_from(records, self, cache_value) + if not records: + return records + if self.store: + # determine records that are known to be not null + not_null = cache.get_records_different_from(records, self, None) + + cache.update(records, self, itertools.repeat(cache_value)) + + # retrieve the attachments that store the values, and adapt them + if self.store and any(records._ids): + real_records = records.filtered("id") + atts = ( + records.env["ir.attachment"] + .sudo() + .with_context( + binary_field_real_user=records.env.user, + ) + ) + if not_null: + atts = atts.search( + [ + ("res_model", "=", self.model_name), + ("res_field", "=", self.name), + ("res_id", "in", real_records.ids), + ] + ) + if value: + filename = cache_value.name + content = cache_value.getvalue() + # update the existing attachments + atts.write({"raw": content, "name": filename}) + atts_records = records.browse(atts.mapped("res_id")) + # create the missing attachments + missing = real_records - atts_records + if missing: + create_vals = [] + for record in missing: + create_vals.append( + { + "name": filename, + "res_model": record._name, + "res_field": self.name, + "res_id": record.id, + "type": "binary", + "raw": content, + } + ) + created = atts.create(create_vals) + for att in created: + record = records.browse(att.res_id) + record.env.cache.update( + record, self, [FSFileValue(attachment=att)], dirty=False + ) + else: + atts.unlink() + + return records + + def _get_filename(self, record): + return record.env.context.get("fs_filename", self.name) + + def convert_to_cache(self, value, record, validate=True): + if value is None or value is False: + return None + if isinstance(value, FSFileValue): + return value + if isinstance(value, dict): + return FSFileValue( + name=value["filename"], value=base64.b64decode(value["content"]) + ) + if isinstance(value, IOBase): + name = getattr(value, "name", None) + if name is None: + name = self._get_filename(record) + return FSFileValue(name=name, value=value) + if isinstance(value, bytes): + return FSFileValue( + name=self._get_filename(record), value=base64.b64decode(value) + ) + raise ValueError( + "Invalid value for %s: %r\n" + "Should be base64 encoded bytes or a file-like object" % (self, value) + ) + + def convert_to_write(self, value, record): + return self.convert_to_cache(value, record) + + def __convert_to_column(self, value, record, values=None, validate=True): + if value is None or value is False: + return None + if isinstance(value, IOBase): + if hasattr(value, "getvalue"): + value = value.getvalue() + else: + v = value.read() + value.seek(0) + value = v + return value + if isinstance(value, bytes): + return base64.b64decode(value) + raise ValueError( + "Invalid value for %s: %r\n" + "Should be base64 encoded bytes or a file-like object" % (self, value) + ) + + def __convert_to_record(self, value, record): + if value is None or value is False: + return None + if isinstance(value, IOBase): + return value + if isinstance(value, bytes): + return FSFileValue(value=value) + raise ValueError( + "Invalid value for %s: %r\n" + "Should be base64 encoded bytes or a file-like object" % (self, value) + ) + + def convert_to_read(self, value, record, use_name_get=True): + if value is None or value is False: + return None + if isinstance(value, FSFileValue): + return { + "filename": value.name, + "url": value.internal_url, + "size": value.size, + "mimetype": value.mimetype, + } + raise ValueError( + "Invalid value for %s: %r\n" + "Should be base64 encoded bytes or a file-like object" % (self, value) + ) diff --git a/fs_file/readme/CONTRIBUTORS.rst b/fs_file/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..ce84680771 --- /dev/null +++ b/fs_file/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +Laurent Mignon +Marie Lejeune diff --git a/fs_file/readme/DESCRIPTION.rst b/fs_file/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..e69de29bb2 diff --git a/fs_file/readme/USAGE.rst b/fs_file/readme/USAGE.rst new file mode 100644 index 0000000000..affe202714 --- /dev/null +++ b/fs_file/readme/USAGE.rst @@ -0,0 +1,100 @@ +The new field **FSFile** has been developed to allows you to store files +in an external filesystem storage. Its design is based on the following +principles: + +* The content of the file must be read from the filesystem only when + needed. +* It must be possible to manipulate the file content as a stream by default. +* Unlike Odoo's Binary field, the content is the raw file content by default + (no base64 encoding). +* To allows to exchange the file content with other systems, writing the + content as base64 is possible. The read operation will return a json + structure with the filename, the mimetype, the size and a url to download the file. + +This design allows to minimize the memory consumption of the server when +manipulating large files and exchanging them with other systems through +the default jsonrpc interface. + +Concretely, this design allows you to write code like this: + +.. code-block:: python + + from IO import BytesIO + from odoo import models, fields + from odoo.addons.fs_file.fields import FSFile + + class MyModel(models.Model): + _name = 'my.model' + + name = fields.Char() + file = FSFile() + + # Create a new record with a raw content + my_model = MyModel.create({ + 'name': 'My File', + 'file': BytesIO(b"content"), + }) + + assert(my_model.file.read() == b"content") + + # Create a new record with a base64 encoded content + my_model = MyModel.create({ + 'name': 'My File', + 'file': b"content".encode('base64'), + }) + assert(my_model.file.read() == b"content") + + # Create a new record with a file content + my_model = MyModel.create({ + 'name': 'My File', + 'file': open('my_file.txt', 'rb'), + }) + assert(my_model.file.read() == b"content") + assert(my_model.file.name == "my_file.txt") + + # create a record with a file content as base64 encoded and a filename + # This method is useful to create a record from a file uploaded + # through the web interface. + my_model = MyModel.create({ + 'name': 'My File', + 'file': { + 'filename': 'my_file.txt', + 'content': base64.b64encode(b"content"), + }, + }) + assert(my_model.file.read() == b"content") + assert(my_model.file.name == "my_file.txt") + + # write the content of the file as base64 encoded and a filename + # This method is useful to update a record from a file uploaded + # through the web interface. + my_model.write({ + 'file': { + 'name': 'my_file.txt', + 'file': base64.b64encode(b"content"), + }, + }) + + # the call to read() will return a json structure with the filename, + # the mimetype, the size and a url to download the file. + info = my_model.file.read() + assert(info["file"] == { + "filename": "my_file.txt", + "mimetype": "text/plain", + "size": 7, + "url": "/web/content/1234/my_file.txt", + }) + + # use the field as a file stream + # In such a case, the content is read from the filesystem without being + # stored in memory. + with my_model.file.open("rb) as f: + assert(f.read() == b"content") + + # use the field as a file stream to write the content + # In such a case, the content is written to the filesystem without being + # stored in memory. This kind of approach is useful to manipulate large + # files and to avoid to use too much memory. + # Transactional behaviour is ensured by the implementation! + with my_model.file.open("wb") as f: + f.write(b"content") diff --git a/fs_file/static/description/icon.png b/fs_file/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/fs_file/static/description/icon.png differ diff --git a/fs_file/static/description/index.html b/fs_file/static/description/index.html new file mode 100644 index 0000000000..166f0ed9dd --- /dev/null +++ b/fs_file/static/description/index.html @@ -0,0 +1,493 @@ + + + + + + +Fs File + + + +
+

Fs File

+ + +

Beta License: AGPL-3 OCA/storage Translate me on Weblate Try me on Runbot

+

Table of contents

+ +
+

Usage

+

The new field FSFile has been developed to allows you to store files +in an external filesystem storage. Its design is based on the following +principles:

+
    +
  • The content of the file must be read from the filesystem only when +needed.
  • +
  • It must be possible to manipulate the file content as a stream by default.
  • +
  • Unlike Odoo’s Binary field, the content is the raw file content by default +(no base64 encoding).
  • +
  • To allows to exchange the file content with other systems, writing the +content as base64 is possible. The read operation will return a json +structure with the filename, the mimetype and a url to download the file.
  • +
+

This design allows to minimize the memory consumption of the server when +manipulating large files and exchanging them with other systems through +the default jsonrpc interface.

+

Concretely, this design allows you to write code like this:

+
+from IO import BytesIO
+from odoo import models, fields
+from odoo.addons.fs_file.fields import FSFile
+
+class MyModel(models.Model):
+    _name = 'my.model'
+
+    name = fields.Char()
+    filename = fields.Char()
+    file = FSFile(field_name='filename', storage_code="my_storage")
+
+# Create a new record with a raw content
+my_model = MyModel.create({
+    'name': 'My File',
+    'filename': 'my_file.txt',
+    'file': BytesIO(b"content"),
+})
+
+assert(my_model.file.read() == b"content")
+assert(my_model.file.name == "my_file.txt")
+
+# Create a new record with a base64 encoded content
+my_model = MyModel.create({
+    'name': 'My File',
+    'filename': 'my_file.txt',
+    'file': b"content".encode('base64'),
+})
+assert(my_model.file.read() == b"content")
+assert(my_model.file.name == "my_file.txt")
+
+# Create a new record with a file content
+with open('my_file.txt', 'rb') as f:
+    f.write(b"content")
+
+my_model = MyModel.create({
+    'name': 'My File',
+    'file': open('my_file.txt', 'rb'),
+})
+assert(my_model.file.read() == b"content")
+assert(my_model.file.name == "my_file.txt")
+
+with open(my_model.file, 'wb') as f:
+    f.write(b"new content")
+
+# the call to read() will return a json structure with the filename,
+# the mimetype and a url to download the file.
+info = my_model.file.read()
+assert(info["file"] == {
+    "filename": "my_file.txt",
+    "mimetype": "text/plain",
+    "url": "/web/content/1234/my_file.txt",
+})
+
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+ +
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

lmignon

+

This module is part of the OCA/storage project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/fs_file/static/src/scss/fsfile_field.scss b/fs_file/static/src/scss/fsfile_field.scss new file mode 100644 index 0000000000..e69de29bb2 diff --git a/fs_file/static/src/views/fields/fsfile_field.esm.js b/fs_file/static/src/views/fields/fsfile_field.esm.js new file mode 100644 index 0000000000..30149cf4c6 --- /dev/null +++ b/fs_file/static/src/views/fields/fsfile_field.esm.js @@ -0,0 +1,87 @@ +/** @odoo-module */ + +/** + * Copyright 2023 ACSONE SA/NV + */ + +import {registry} from "@web/core/registry"; +import {session} from "@web/session"; +import {formatFloat} from "@web/views/fields/formatters"; +import {useService} from "@web/core/utils/hooks"; +import {sprintf} from "@web/core/utils/strings"; +import {getDataURLFromFile} from "@web/core/utils/urls"; +import {standardFieldProps} from "@web/views/fields/standard_field_props"; +import {Component, onWillUpdateProps, useState} from "@odoo/owl"; + +const DEFAULT_MAX_FILE_SIZE = 128 * 1024 * 1024; // 128MB + +export class FSFileField extends Component { + setup() { + this.notification = useService("notification"); + this.state = useState({ + ...this.props.value, + isUploading: false, + }); + onWillUpdateProps((nextProps) => { + this.state.isUploading = false; + const {filename, mimetype, url} = nextProps.value || {}; + this.state.filename = filename; + this.state.mimetype = mimetype; + this.state.url = url; + }); + } + get maxUploadSize() { + return session.max_file_upload_size || DEFAULT_MAX_FILE_SIZE; + } + + edit() { + var input = document.createElement("input"); + input.type = "file"; + input.accept = this.props.acceptedFileExtensions; + input.onchange = (e) => { + const file = e.target.files[0]; + if (file) { + if (file.size > this.maxUploadSize) { + this.notification.add( + sprintf( + this.env._t( + "The file size exceeds the maximum allowed size of %s MB." + ), + formatFloat(this.maxUploadSize / 1024 / 1024) + ), + {type: "danger"} + ); + return; + } + this.uploadFile(file); + } + }; + input.click(); + } + + async uploadFile(file) { + this.state.isUploading = true; + const data = await getDataURLFromFile(file); + this.props.record.update({ + [this.props.name]: { + filename: file.name, + content: data.split(",")[1], + }, + }); + this.state.isUploading = false; + } + + clear() { + this.props.record.update({[this.props.name]: false}); + } +} + +FSFileField.template = "fs_file.FSFileField"; +FSFileField.props = { + ...standardFieldProps, + acceptedFileExtensions: {type: String, optional: true}, +}; +FSFileField.defaultProps = { + acceptedFileExtensions: "*", +}; +registry.category("fields").add("fs_file", FSFileField); diff --git a/fs_file/static/src/views/fields/fsfile_field.xml b/fs_file/static/src/views/fields/fsfile_field.xml new file mode 100644 index 0000000000..34840a5e5e --- /dev/null +++ b/fs_file/static/src/views/fields/fsfile_field.xml @@ -0,0 +1,48 @@ + + + + + Uploading... + +
+ + + + + + + + +
+
+ + + + + + +
+ +
diff --git a/fs_file/tests/__init__.py b/fs_file/tests/__init__.py new file mode 100644 index 0000000000..9c12d36073 --- /dev/null +++ b/fs_file/tests/__init__.py @@ -0,0 +1 @@ +from . import test_fs_file diff --git a/fs_file/tests/models.py b/fs_file/tests/models.py new file mode 100644 index 0000000000..145c85c343 --- /dev/null +++ b/fs_file/tests/models.py @@ -0,0 +1,15 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + +from ..fields import FSFile + + +class TestModel(models.Model): + + _name = "test.model" + _description = "Test Model" + _log_access = False + + fs_file = FSFile() diff --git a/fs_file/tests/test_fs_file.py b/fs_file/tests/test_fs_file.py new file mode 100644 index 0000000000..193e31375d --- /dev/null +++ b/fs_file/tests/test_fs_file.py @@ -0,0 +1,154 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 +import io +import os +import tempfile + +from odoo_test_helper import FakeModelLoader + +from odoo.tests.common import TransactionCase + +from odoo.addons.fs_storage.models.fs_storage import FSStorage + +from ..fields import FSFileValue + + +class TestFsFile(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.loader = FakeModelLoader(cls.env, cls.__module__) + cls.loader.backup_registry() + from .models import TestModel + + cls.loader.update_registry((TestModel,)) + + cls.create_content = b"content" + cls.write_content = b"new content" + cls.tmpfile_path = tempfile.mkstemp(suffix=".txt")[1] + with open(cls.tmpfile_path, "wb") as f: + f.write(cls.create_content) + cls.filename = os.path.basename(cls.tmpfile_path) + + def setUp(self): + super().setUp() + self.temp_dir: FSStorage = self.env["fs.storage"].create( + { + "name": "Temp FS Storage", + "protocol": "memory", + "code": "mem_dir", + "directory_path": "/tmp/", + "model_xmlids": "fs_file.model_test_model", + } + ) + + @classmethod + def tearDownClass(cls): + if os.path.exists(cls.tmpfile_path): + os.remove(cls.tmpfile_path) + cls.loader.restore_registry() + return super().tearDownClass() + + def _test_create(self, fs_file_value): + model = self.env["test.model"] + instance = model.create({"fs_file": fs_file_value}) + self.assertTrue(isinstance(instance.fs_file, FSFileValue)) + self.assertEqual(instance.fs_file.getvalue(), self.create_content) + self.assertEqual(instance.fs_file.name, self.filename) + + def _test_write(self, fs_file_value, **ctx): + instance = self.env["test.model"].create({}) + if ctx: + instance = instance.with_context(**ctx) + instance.fs_file = fs_file_value + self.assertEqual(instance.fs_file.getvalue(), self.write_content) + self.assertEqual(instance.fs_file.name, self.filename) + + def test_read(self): + instance = self.env["test.model"].create( + {"fs_file": FSFileValue(name=self.filename, value=self.create_content)} + ) + info = instance.read(["fs_file"])[0] + self.assertDictEqual( + info["fs_file"], + { + "filename": self.filename, + "mimetype": "text/plain", + "size": 7, + "url": instance.fs_file.internal_url, + }, + ) + + def test_create_with_fsfilebytesio(self): + self._test_create(FSFileValue(name=self.filename, value=self.create_content)) + + def test_create_with_dict(self): + self._test_create( + { + "filename": self.filename, + "content": base64.b64encode(self.create_content), + } + ) + + def test_write_with_dict(self): + self._test_write( + { + "filename": self.filename, + "content": base64.b64encode(self.write_content), + } + ) + + def test_create_with_file_like(self): + with open(self.tmpfile_path, "rb") as f: + self._test_create(f) + + def test_create_in_b64(self): + instance = self.env["test.model"].create( + {"fs_file": base64.b64encode(self.create_content)} + ) + self.assertTrue(isinstance(instance.fs_file, FSFileValue)) + self.assertEqual(instance.fs_file.getvalue(), self.create_content) + + def test_write_in_b64(self): + instance = self.env["test.model"].create({"fs_file": b"test"}) + instance.write({"fs_file": base64.b64encode(self.create_content)}) + self.assertTrue(isinstance(instance.fs_file, FSFileValue)) + self.assertEqual(instance.fs_file.getvalue(), self.create_content) + + def test_write_in_b64_with_specified_filename(self): + self._test_write( + base64.b64encode(self.write_content), fs_filename=self.filename + ) + + def test_create_with_io(self): + instance = self.env["test.model"].create( + {"fs_file": io.BytesIO(self.create_content)} + ) + self.assertTrue(isinstance(instance.fs_file, FSFileValue)) + self.assertEqual(instance.fs_file.getvalue(), self.create_content) + + def test_write_with_io(self): + instance = self.env["test.model"].create( + {"fs_file": io.BytesIO(self.create_content)} + ) + instance.write({"fs_file": io.BytesIO(b"test3")}) + self.assertTrue(isinstance(instance.fs_file, FSFileValue)) + self.assertEqual(instance.fs_file.getvalue(), b"test3") + + def test_modify_fsfilebytesio(self): + """If you modify the content of the FSFileValue, + the changes will be directly applied + and a new file in the storage must be created for the new content. + """ + instance = self.env["test.model"].create( + {"fs_file": FSFileValue(name=self.filename, value=self.create_content)} + ) + initial_store_fname = instance.fs_file.attachment.store_fname + with instance.fs_file.open(mode="wb") as f: + f.write(b"new_content") + self.assertNotEqual( + instance.fs_file.attachment.store_fname, initial_store_fname + ) + self.assertEqual(instance.fs_file.getvalue(), b"new_content") diff --git a/fs_file_demo/README.rst b/fs_file_demo/README.rst new file mode 100644 index 0000000000..a9e2ce3ea7 --- /dev/null +++ b/fs_file_demo/README.rst @@ -0,0 +1,23 @@ +============ +Fs File Test +============ + +test + +Configuration +============= + +To configure this module, you need to: + +#. Go to ... + +Usage +===== + +To use this module, you need to: + +#. Go to ... + + +Changelog +========= diff --git a/fs_file_demo/__init__.py b/fs_file_demo/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/fs_file_demo/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/fs_file_demo/__manifest__.py b/fs_file_demo/__manifest__.py new file mode 100644 index 0000000000..f987579cbc --- /dev/null +++ b/fs_file_demo/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Fs File Demo", + "summary": """Demo addon for fs_file""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/storage", + "depends": [ + "fs_file", + ], + "data": [ + "security/fs_file.xml", + "views/fs_file.xml", + ], + "demo": [], + "development_status": "Alpha", +} diff --git a/fs_file_demo/models/__init__.py b/fs_file_demo/models/__init__.py new file mode 100644 index 0000000000..e5b23f8e9a --- /dev/null +++ b/fs_file_demo/models/__init__.py @@ -0,0 +1 @@ +from . import fs_file diff --git a/fs_file_demo/models/fs_file.py b/fs_file_demo/models/fs_file.py new file mode 100644 index 0000000000..8fd747a9b8 --- /dev/null +++ b/fs_file_demo/models/fs_file.py @@ -0,0 +1,15 @@ +# Copyright 2023 ACSONE SA/NV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + +from odoo.addons.fs_file import fields as fs_fields + + +class FsFile(models.Model): + + _name = "fs.file" + _description = "Fs File" + + name = fields.Char() + file = fs_fields.FSFile(string="File") diff --git a/fs_file_demo/readme/CONTRIBUTORS.rst b/fs_file_demo/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..ce84680771 --- /dev/null +++ b/fs_file_demo/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +Laurent Mignon +Marie Lejeune diff --git a/fs_file_demo/readme/DESCRIPTION.rst b/fs_file_demo/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..b5321ede9c --- /dev/null +++ b/fs_file_demo/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This addon is a demo addon for ``fs_file``. diff --git a/fs_file_demo/readme/USAGE.rst b/fs_file_demo/readme/USAGE.rst new file mode 100644 index 0000000000..43a90275e0 --- /dev/null +++ b/fs_file_demo/readme/USAGE.rst @@ -0,0 +1,2 @@ +Go into Settings > Technical > Fs File to create a new record of model +``fs.file``, which contains a FSFile record. diff --git a/fs_file_demo/security/fs_file.xml b/fs_file_demo/security/fs_file.xml new file mode 100644 index 0000000000..70db6f0494 --- /dev/null +++ b/fs_file_demo/security/fs_file.xml @@ -0,0 +1,16 @@ + + + + + + fs.file default access + + + + + + + + + diff --git a/fs_file_demo/views/fs_file.xml b/fs_file_demo/views/fs_file.xml new file mode 100644 index 0000000000..030150e2d7 --- /dev/null +++ b/fs_file_demo/views/fs_file.xml @@ -0,0 +1,49 @@ + + + + + + fs.file.form (in fs_file_demo) + fs.file + +
+
+ + + + + + +
+ + + + + + + fs.file.tree (in fs_file_demo) + fs.file + + + + + + + + + Fs File + fs.file + tree,form + [] + {} + + + + Fs File + + + + + + diff --git a/setup/fs_file/odoo/addons/fs_file b/setup/fs_file/odoo/addons/fs_file new file mode 120000 index 0000000000..78391e08e3 --- /dev/null +++ b/setup/fs_file/odoo/addons/fs_file @@ -0,0 +1 @@ +../../../../fs_file \ No newline at end of file diff --git a/setup/fs_file/setup.py b/setup/fs_file/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/fs_file/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/setup/fs_file_demo/odoo/addons/fs_file_demo b/setup/fs_file_demo/odoo/addons/fs_file_demo new file mode 120000 index 0000000000..106ba8bf04 --- /dev/null +++ b/setup/fs_file_demo/odoo/addons/fs_file_demo @@ -0,0 +1 @@ +../../../../fs_file_demo \ No newline at end of file diff --git a/setup/fs_file_demo/setup.py b/setup/fs_file_demo/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/fs_file_demo/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/test-requirements.txt b/test-requirements.txt index 932a8957f7..58bf70f5a2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1 +1,2 @@ mock +odoo-test-helper