From d09db20d321bf35e4319f5db9e9b527888a9b5a8 Mon Sep 17 00:00:00 2001 From: Benjamin Willig Date: Wed, 15 May 2024 09:41:53 +0200 Subject: [PATCH 1/2] [CHG] display kanban view by default for product images --- .../views/product_template.xml | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/fs_product_multi_image/views/product_template.xml b/fs_product_multi_image/views/product_template.xml index 98b4f0d2bf..eebe4cdf0d 100644 --- a/fs_product_multi_image/views/product_template.xml +++ b/fs_product_multi_image/views/product_template.xml @@ -20,26 +20,20 @@ - - - - - - - - - - - + + + + + + + + + + From 0df8ce76abef265ab3e8d1dabfd52cbe6473ebef Mon Sep 17 00:00:00 2001 From: Benjamin Willig Date: Mon, 29 Jan 2024 16:31:43 +0100 Subject: [PATCH 2/2] [CHG] use list.addNew to add relation records instead of calling the ORM explicitely --- .../models/fs_image_relation_mixin.py | 14 ++-- .../fs_image_relation_dnd_upload.esm.js | 67 ++++++++++--------- .../views/fs_image_relation_mixin.xml | 4 +- .../views/fs_product_image.xml | 2 +- 4 files changed, 49 insertions(+), 38 deletions(-) diff --git a/fs_base_multi_image/models/fs_image_relation_mixin.py b/fs_base_multi_image/models/fs_image_relation_mixin.py index bb6ebfeba4..c934ca3281 100644 --- a/fs_base_multi_image/models/fs_image_relation_mixin.py +++ b/fs_base_multi_image/models/fs_image_relation_mixin.py @@ -82,12 +82,14 @@ def _inverse_image(self): @api.model def _cleanup_vals(self, vals): - if ( - "link_existing" in vals - and vals["link_existing"] - and "specific_image" in vals - ): - vals["specific_image"] = False + link_existing = vals.get("link_existing") + if link_existing: + if "specific_image" in vals: + vals.pop("specific_image") + if "image" in vals: + # image is set when using the kanban renderer so it + # prevents the name field to be computed well + vals.pop("image") return vals @api.model_create_multi diff --git a/fs_base_multi_image/static/src/fields/fs_image_relation_dnd_upload/fs_image_relation_dnd_upload.esm.js b/fs_base_multi_image/static/src/fields/fs_image_relation_dnd_upload/fs_image_relation_dnd_upload.esm.js index 7d0aed7446..984f169d6f 100644 --- a/fs_base_multi_image/static/src/fields/fs_image_relation_dnd_upload/fs_image_relation_dnd_upload.esm.js +++ b/fs_base_multi_image/static/src/fields/fs_image_relation_dnd_upload/fs_image_relation_dnd_upload.esm.js @@ -4,10 +4,20 @@ import {X2ManyField, x2ManyField} from "@web/views/fields/x2many/x2many_field"; import {onWillRender, useRef, useState} from "@odoo/owl"; import {registry} from "@web/core/registry"; +import {useX2ManyCrud} from "@web/views/fields/relational_utils"; + export class FsImageRelationDndUploadField extends X2ManyField { + /** + * When using this widget, displayed image relation views must contains + * following fields: + * - sequence + * - image_id + * - specific_image + * - link_existing + */ setup() { super.setup(); - this.relationField = this.field.relation_field; + this.options = this.activeField.options; this.defaultTarget = this.props.crudOptions.target || "specific"; this.state = useState({ dragging: false, @@ -16,6 +26,8 @@ export class FsImageRelationDndUploadField extends X2ManyField { this.fileInput = useRef("fileInput"); this.defaultSequence = 0; + this.operations = useX2ManyCrud(() => this.list, this.isMany2Many); + onWillRender(() => { this.initDefaultSequence(); }); @@ -25,6 +37,10 @@ export class FsImageRelationDndUploadField extends X2ManyField { return this.state.target; } + get relationRecordId() { + return this.props.record.data.id; + } + get displayDndZone() { const activeActions = this.activeActions; return ( @@ -89,18 +105,15 @@ export class FsImageRelationDndUploadField extends X2ManyField { async uploadFsImage(imagesDesc) { const self = this; - const createValues = []; self.env.model.orm .call("fs.image", "create", [imagesDesc]) .then((fsImageIds) => { let values = {}; $.each(fsImageIds, (i, fsImageId) => { values = self.getFsImageRelationValues(fsImageId); - createValues.push(values); + self.createFieldRelationRecords(values); }); - }) - .then(() => { - self.createFieldRelationRecords(createValues); + self.env.services.ui.unblock(); }) .catch(() => { self.displayUploadError(); @@ -120,8 +133,8 @@ export class FsImageRelationDndUploadField extends X2ManyField { getFsImageRelationValues(fsImageId) { let values = { - image_id: fsImageId, - link_existing: true, + default_image_id: fsImageId, + default_link_existing: true, }; values = {...values, ...this.getRelationCommonValues()}; return values; @@ -129,40 +142,34 @@ export class FsImageRelationDndUploadField extends X2ManyField { async uploadSpecificImage(imagesDesc) { const self = this; - const createValues = []; $.each(imagesDesc, (i, imageDesc) => { - createValues.push(self.getSpecificImageRelationValues(imageDesc)); + self.createFieldRelationRecords( + self.getSpecificImageRelationValues(imageDesc) + ); }); - self.createFieldRelationRecords(createValues); + self.env.services.ui.unblock(); } getSpecificImageRelationValues(imageDesc) { - return {...imageDesc, ...this.getRelationCommonValues()}; + return { + ...this.getRelationCommonValues(), + default_specific_image: imageDesc.image, + }; } getRelationCommonValues() { - const values = { - sequence: this.getNewSequence(), + return { + default_sequence: this.getNewSequence(), }; - values[this.relationField] = this.props.record.data.id; - return values; } async createFieldRelationRecords(createValues) { - const self = this; - const model = self.env.model; - model.orm - .call(self.field.relation, "create", [createValues]) - .then(() => { - model.root.load(); - model.root.save(); - }) - .then(() => { - self.env.services.ui.unblock(); - }) - .catch(() => { - self.displayUploadError(); - }); + await this.list.addNewRecord({ + position: "bottom", + context: createValues, + mode: "readonly", + allowWarning: true, + }); } async uploadImages(files) { diff --git a/fs_base_multi_image/views/fs_image_relation_mixin.xml b/fs_base_multi_image/views/fs_image_relation_mixin.xml index d8b8e8f486..35ba648925 100644 --- a/fs_base_multi_image/views/fs_image_relation_mixin.xml +++ b/fs_base_multi_image/views/fs_image_relation_mixin.xml @@ -42,8 +42,10 @@ - + + +
diff --git a/fs_product_multi_image/views/fs_product_image.xml b/fs_product_multi_image/views/fs_product_image.xml index 67c436b359..4374b3d113 100644 --- a/fs_product_multi_image/views/fs_product_image.xml +++ b/fs_product_multi_image/views/fs_product_image.xml @@ -38,7 +38,7 @@ /> primary - +