Skip to content

Commit

Permalink
[MIG] fs_file: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenminhchien committed Apr 9, 2024
1 parent 9502421 commit 733d32e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 99 deletions.
6 changes: 4 additions & 2 deletions fs_file/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ Authors
Contributors
------------
Laurent Mignon <laurent.mignon@acsone.eu> Marie Lejeune
<marie.lejeune@acsone.eu> Hugues Damry <hughes.damry@acsone.eu>
- Laurent Mignon <laurent.mignon@acsone.eu>
- Marie Lejeune <marie.lejeune@acsone.eu>
- Hugues Damry <hughes.damry@acsone.eu>
- Nguyen Minh Chien <chien@trobz.com>
Maintainers
-----------
Expand Down
2 changes: 1 addition & 1 deletion fs_file/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 16 additions & 18 deletions fs_file/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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"
)
6 changes: 4 additions & 2 deletions fs_file/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Laurent Mignon \<<laurent.mignon@acsone.eu>\> Marie Lejeune
\<<marie.lejeune@acsone.eu>\> Hugues Damry \<<hughes.damry@acsone.eu>\>
- Laurent Mignon \<<laurent.mignon@acsone.eu>\>
- Marie Lejeune \<<marie.lejeune@acsone.eu>\>
- Hugues Damry \<<hughes.damry@acsone.eu>\>
- Nguyen Minh Chien \<<chien@trobz.com>\>
9 changes: 6 additions & 3 deletions fs_file/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -605,8 +604,12 @@ <h2><a class="toc-backref" href="#toc-entry-10">Authors</a></h2>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#toc-entry-11">Contributors</a></h2>
<p>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt; Marie Lejeune
&lt;<a class="reference external" href="mailto:marie.lejeune&#64;acsone.eu">marie.lejeune&#64;acsone.eu</a>&gt; Hugues Damry &lt;<a class="reference external" href="mailto:hughes.damry&#64;acsone.eu">hughes.damry&#64;acsone.eu</a>&gt;</p>
<ul class="simple">
<li>Laurent Mignon &lt;<a class="reference external" href="mailto:laurent.mignon&#64;acsone.eu">laurent.mignon&#64;acsone.eu</a>&gt;</li>
<li>Marie Lejeune &lt;<a class="reference external" href="mailto:marie.lejeune&#64;acsone.eu">marie.lejeune&#64;acsone.eu</a>&gt;</li>
<li>Hugues Damry &lt;<a class="reference external" href="mailto:hughes.damry&#64;acsone.eu">hughes.damry&#64;acsone.eu</a>&gt;</li>
<li>Nguyen Minh Chien &lt;<a class="reference external" href="mailto:chien&#64;trobz.com">chien&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-12">Maintainers</a></h2>
Expand Down
63 changes: 22 additions & 41 deletions fs_file/static/src/views/fields/fsfile_field.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}
}
Expand All @@ -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);
78 changes: 46 additions & 32 deletions fs_file/static/src/views/fields/fsfile_field.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,60 @@

<t t-name="fs_file.FSFileField" owl="1">
<t t-if="!props.readonly">
<div class="w-100 d-inline-flex">
<FileUploader
acceptedFileExtensions="props.acceptedFileExtensions"
t-key="props.record.resId"
onUploaded.bind="onFileUploaded"
>
<t t-if="props.value">
<t t-if="state.url">
<a
<t t-if="props.record.data[props.name]">
<div class="w-100 d-inline-flex gap-1">
<FileUploader
acceptedFileExtensions="props.acceptedFileExtensions"
t-key="props.record.resId"
onUploaded.bind="onFileUploaded"
>
<t t-if="url">
<a
class="o_form_uri fs_file_link"
t-att-href="state.url + '?download=1'"
t-att-href="url + '?download=1'"
>
<span class="fa fa-download me-2" />
<t t-if="state.filename" t-esc="state.filename" />
</a>
<span class="fa fa-download me-2" />
<t t-if="filename" t-esc="filename" />
</a>
</t>
<t t-else="">
<t t-if="state.filename" t-esc="state.filename" />
<t t-if="filename" t-esc="filename" />
</t>
</t>
<t t-set-slot="toggler">
<button
class="btn btn-secondary fa fa-pencil o_select_file_button py-0"
data-tooltip="Edit"
aria-label="Edit"
<t t-set-slot="toggler">
<button
class="btn btn-link btn-sm lh-1 fa fa-pencil o_select_file_button"
data-tooltip="Edit"
aria-label="Edit"
/>
</t>
<button
class="btn btn-link btn-sm lh-1 fa fa-trash o_clear_file_button"
data-tooltip="Clear"
aria-label="Clear"
t-on-click="onFileRemove"
t-if="props.record.data[props.name]"
/>
</t>
<button
class="btn btn-secondary fa fa-trash o_clear_file_button py-0"
data-tooltip="Clear"
aria-label="Clear"
t-on-click="onFileRemove"
/>
</FileUploader>
</div>
</FileUploader>
</div>
</t>
<t t-else="">
<label class="o_select_file_button btn btn-primary">
<FileUploader
acceptedFileExtensions="props.acceptedFileExtensions"
t-key="props.record.resId"
onUploaded.bind="onFileUploaded"
>
<t t-set-slot="toggler">
Upload your file
</t>
</FileUploader>
</label>
</t>
</t>
<t t-elif="props.value">
<a class="o_form_uri fs_file_link" t-att-href="state.url">
<t t-elif="props.record.data[props.name]">
<a class="o_form_uri fs_file_link" t-att-href="url">
<span class="fa fa-download me-2" />
<t t-if="state.filename" t-esc="state.filename" />
<t t-if="filename" t-esc="filename" />
</a>
</t>
</t>
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
odoo-test-helper

0 comments on commit 733d32e

Please sign in to comment.