From c5e90eb8d896b39dddcce1c5f665eab5010d2415 Mon Sep 17 00:00:00 2001 From: jayraj-omnia Date: Fri, 24 Jan 2025 18:41:35 +0530 Subject: [PATCH] [ADD] add plm_many2one_image widget --- plm/__manifest__.py | 4 + plm/models/product_product.py | 26 +++++++ .../src/js/many2one_widget/many2one_widget.js | 77 +++++++++++++++++++ .../js/many2one_widget/many2one_widget.xml | 15 ++++ 4 files changed, 122 insertions(+) create mode 100644 plm/static/src/js/many2one_widget/many2one_widget.js create mode 100644 plm/static/src/js/many2one_widget/many2one_widget.xml diff --git a/plm/__manifest__.py b/plm/__manifest__.py index b052cb6e..e959ec57 100644 --- a/plm/__manifest__.py +++ b/plm/__manifest__.py @@ -76,6 +76,10 @@ "plm/static/src/css/color_fields_tree.css", "plm/static/src/js/mrp_bom_overview_line.js", "plm/static/src/xml/mrp_bom_overview_table_inherit.xml", + + # many2one Widget + "plm/static/src/js/many2one_widget/many2one_widget.js", + "plm/static/src/js/many2one_widget/many2one_widget.xml", ], 'web.report_assets_common': [ "plm/static/src/scss/document_bom.scss", diff --git a/plm/models/product_product.py b/plm/models/product_product.py index 0b4d980f..0667abda 100755 --- a/plm/models/product_product.py +++ b/plm/models/product_product.py @@ -1919,6 +1919,32 @@ def fillUpSrvPath(p_p_id): fillUpSrvPath(product_id) return list(set(out_src)) + def action_open_linked_field(self, related_field): + view_id = self.env["ir.model.data"]._xmlid_to_res_id("plm.document_kanban_view") + related_field_value = getattr(self, related_field, False) + if related_field_value: + related_ids = related_field_value.ids + else: + related_ids = [] + ctx = self.env.context.copy() + domain = [('is_plm', '=', True), ('id', 'in', related_ids)] + ctx.update({ + "create": False, + "delete": False, + 'default_res_ids': related_ids, + 'readonly': True + }) + + return { + "type": "ir.actions.act_window", + "view_mode": "kanban", + 'domain': domain, + "res_model": "ir.attachment", + "target": "new", + "views": [[view_id, "kanban"]], + "context": ctx, + } + class PlmTemporayMessage(models.TransientModel): _name = "plm.temporary.message" diff --git a/plm/static/src/js/many2one_widget/many2one_widget.js b/plm/static/src/js/many2one_widget/many2one_widget.js new file mode 100644 index 00000000..95e86667 --- /dev/null +++ b/plm/static/src/js/many2one_widget/many2one_widget.js @@ -0,0 +1,77 @@ +/** @odoo-module */ +import {registry} from "@web/core/registry"; +import {Many2OneField, many2OneField} from "@web/views/fields/many2one/many2one_field"; +import {onWillUpdateProps} from "@odoo/owl"; + +export class PlmMany2oneWidget extends Many2OneField { + + static template = "plm.PlmMany2oneWidget"; + static props = { + ...Many2OneField.props, + options: {type: Object, optional: true}, + }; + static defaultProps = { + ...Many2OneField.defaultProps, + options: false, + }; + + async setup() { + super.setup(); + console.log("..........1"); + this.imageData = false; + this.relatedField = false; + this.imageToolTipData = false; + onWillUpdateProps(async (nextProps) => { + this.imageData = false; + this.imageToolTipData = false; + let fieldName = nextProps.name; + if (nextProps && nextProps.record && nextProps.record.data && nextProps.record.data[fieldName] && nextProps.record.data[fieldName].length != 0) { + let imageData = await this.env.model.orm.call(this.relation, "search_read", [], { + domain: [["id", "=", nextProps.record.data[fieldName][0]]], + fields: [nextProps.options.image_field], + }); + if (imageData && imageData.length != 0 && imageData[0][nextProps.options.image_field]) { + this.imageData = "data:image/png;base64, " + imageData[0][nextProps.options.image_field]; + this.imageToolTipData = JSON.stringify({"url": this.imageData}); + this.render(); + } + } + }); + if (this.props && this.props.record && this.props.record.data && this.props.record.data[this.props.name] && this.props.record.data[this.props.name].length != 0) { + + + let imageData = await this.env.model.orm.call(this.relation, "search_read", [], { + domain: [["id", "=", this.props.record.data[this.props.name][0]]], + fields: [this.props.options.image_field], + }); + this.relatedField = await this.env.model.orm.call(this.relation, "search_read", [], { + domain: [["id", "=", this.props.record.data[this.props.name][0]]], + fields: [this.props.options.linked_field], + }); + if (imageData && imageData.length != 0 && imageData[0][this.props.options.image_field]) { + this.imageData = "data:image/png;base64, " + imageData[0][this.props.options.image_field]; + this.imageToolTipData = JSON.stringify({"url": this.imageData}); + this.render(); + } + } + } + + onImageClicked() { + + let selectedProductId = this.props.record.data.product_id[0]; + let relatedFieldName = this.props.options.linked_field; + let model = this.props.record.model.root.model.config.fields[this.props.name].relation; + let action_open_linked_field = this.props.record.model.orm.call(model, "action_open_linked_field", [selectedProductId, relatedFieldName]); + return this.action.doAction(action_open_linked_field); + } +} + +const plmMany2oneField = { + ...many2OneField, + component: PlmMany2oneWidget, + extractProps: ({attrs, context, decorations, options, string}, dynamicInfo) => ({ + ...many2OneField.extractProps({attrs, context, decorations, options, string}, dynamicInfo), + options: options, + }), +}; +registry.category("fields").add("plm_many2one_image", plmMany2oneField); diff --git a/plm/static/src/js/many2one_widget/many2one_widget.xml b/plm/static/src/js/many2one_widget/many2one_widget.xml new file mode 100644 index 00000000..47ef1e5b --- /dev/null +++ b/plm/static/src/js/many2one_widget/many2one_widget.xml @@ -0,0 +1,15 @@ + + + + + + + + + + +