diff --git a/fs_file/README.rst b/fs_file/README.rst index a7e5d96cf4..9b487b1c15 100644 --- a/fs_file/README.rst +++ b/fs_file/README.rst @@ -252,8 +252,10 @@ Authors Contributors ------------ -Laurent Mignon Marie Lejeune - Hugues Damry +- Laurent Mignon +- Marie Lejeune +- Hugues Damry +- Nguyen Minh Chien Maintainers ----------- diff --git a/fs_file/__manifest__.py b/fs_file/__manifest__.py index 520ca6e10c..2071cd87f7 100644 --- a/fs_file/__manifest__.py +++ b/fs_file/__manifest__.py @@ -5,7 +5,7 @@ "name": "Fs File", "summary": """ Field to store files into filesystem storages""", - "version": "16.0.1.0.6", + "version": "17.0.1.0.0", "license": "AGPL-3", "author": "ACSONE SA/NV,Odoo Community Association (OCA)", "website": "https://github.com/OCA/storage", diff --git a/fs_file/fields.py b/fs_file/fields.py index 66123eaaf2..150a9aa6ec 100644 --- a/fs_file/fields.py +++ b/fs_file/fields.py @@ -277,19 +277,17 @@ def read(self, records): def create(self, record_values): if not record_values: return - env = record_values[0][0].env - with env.norecompute(): - for record, value in record_values: - if value: - cache_value = self.convert_to_cache(value, record) - attachment = self._create_attachment(record, cache_value) - cache_value = self._convert_attachment_to_cache(attachment) - record.env.cache.update( - record, - self, - [cache_value], - dirty=False, - ) + for record, value in record_values: + if value: + cache_value = self.convert_to_cache(value, record) + attachment = self._create_attachment(record, cache_value) + cache_value = self._convert_attachment_to_cache(attachment) + record.env.cache.update( + record, + self, + [cache_value], + dirty=False, + ) def _create_attachment(self, record, cache_value: FSFileValue): ir_attachment = ( @@ -369,7 +367,7 @@ def write(self, records, value): # require to load the attachment record for record in atts_records: new_cache_value = self._convert_attachment_to_cache( - atts.filtered(lambda att: att.res_id == record.id) + atts.filtered(lambda att, rec=record: att.res_id == rec.id) ) cache.update(record, self, [new_cache_value], dirty=False) # create the missing attachments @@ -420,8 +418,8 @@ def convert_to_cache(self, value, record, validate=True): 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) + f"Invalid value for {self}: {value}\n" + "Should be base64 encoded bytes or a file-like object" ) def convert_to_write(self, value, record): @@ -442,6 +440,6 @@ def convert_to_read(self, value, record, use_name_get=True): res["content"] = base64.b64encode(value.getvalue()).decode("ascii") return res raise ValueError( - "Invalid value for %s: %r\n" - "Should be base64 encoded bytes or a file-like object" % (self, value) + f"Invalid value for {self}: {value}\n" + "Should be base64 encoded bytes or a file-like object" ) diff --git a/fs_file/readme/CONTRIBUTORS.md b/fs_file/readme/CONTRIBUTORS.md index 5a744bfed0..bfb139b352 100644 --- a/fs_file/readme/CONTRIBUTORS.md +++ b/fs_file/readme/CONTRIBUTORS.md @@ -1,2 +1,4 @@ -Laurent Mignon \<\> Marie Lejeune -\<\> Hugues Damry \<\> +- Laurent Mignon \<\> +- Marie Lejeune \<\> +- Hugues Damry \<\> +- Nguyen Minh Chien \<\> diff --git a/fs_file/static/description/index.html b/fs_file/static/description/index.html index 26c10af680..32529d7661 100644 --- a/fs_file/static/description/index.html +++ b/fs_file/static/description/index.html @@ -1,4 +1,3 @@ - @@ -605,8 +604,12 @@

Authors

Contributors

-

Laurent Mignon <laurent.mignon@acsone.eu> Marie Lejeune -<marie.lejeune@acsone.eu> Hugues Damry <hughes.damry@acsone.eu>

+

Maintainers

diff --git a/fs_file/static/src/views/fields/fsfile_field.esm.js b/fs_file/static/src/views/fields/fsfile_field.esm.js index 7ae3d13a2e..939219383d 100644 --- a/fs_file/static/src/views/fields/fsfile_field.esm.js +++ b/fs_file/static/src/views/fields/fsfile_field.esm.js @@ -3,61 +3,37 @@ /** * Copyright 2023 ACSONE SA/NV */ -import {Component, onWillUpdateProps, useState} from "@odoo/owl"; - +import {Component} from "@odoo/owl"; import {FileUploader} from "@web/views/fields/file_handler"; -import {getDataURLFromFile} from "@web/core/utils/urls"; +import {MAX_FILENAME_SIZE_BYTES} from "@web/views/fields/binary/binary_field"; import {registry} from "@web/core/registry"; import {standardFieldProps} from "@web/views/fields/standard_field_props"; +import {toBase64Length} from "@web/core/utils/binary"; import {useService} from "@web/core/utils/hooks"; export class FSFileField extends Component { setup() { this.notification = useService("notification"); - this.state = useState({ - ...this.props.value, - isValid: true, - }); - onWillUpdateProps((nextProps) => { - this.state.isUploading = false; - const {filename, mimetype, url} = nextProps.value || {}; - this.state.filename = filename; - this.state.mimetype = mimetype; - this.state.url = url; - }); } - - 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; + get filename() { + return (this.props.record.data[this.props.name].filename || "").slice( + 0, + toBase64Length(MAX_FILENAME_SIZE_BYTES) + ); } - - clear() { - this.props.record.update({[this.props.name]: false}); + get url() { + return this.props.record.data[this.props.name].url || ""; } onFileRemove() { - this.state.isValid = true; - this.props.update(false); + this.props.record.update({[this.props.name]: false}); } onFileUploaded(info) { - this.state.isValid = true; - this.props.update({ - filename: info.name, - content: info.data, - }); - } - onLoadFailed() { - this.state.isValid = false; - this.notification.add(this.env._t("Could not display the selected image"), { - type: "danger", + this.props.record.update({ + [this.props.name]: { + filename: info.name, + content: info.data, + }, }); } } @@ -73,4 +49,9 @@ FSFileField.props = { FSFileField.defaultProps = { acceptedFileExtensions: "*", }; -registry.category("fields").add("fs_file", FSFileField); + +export const fSFileField = { + component: FSFileField, +}; + +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 index e52454d4f8..6753c4528f 100644 --- a/fs_file/static/src/views/fields/fsfile_field.xml +++ b/fs_file/static/src/views/fields/fsfile_field.xml @@ -3,46 +3,60 @@ -
- - - - +
+ + + - - - + + + - + - - -
+
+
+
+ + +
- - + + - + diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000000..4ad8e0ecea --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1 @@ +odoo-test-helper