From 559cfd44d1c2edb23717973235d4e834dfdae9dc Mon Sep 17 00:00:00 2001 From: sergio-teruel Date: Sun, 10 Mar 2024 10:01:48 +0100 Subject: [PATCH] [IMP] stock_barcodes: Allow display notifications to web client --- .../models/stock_barcodes_option.py | 3 +++ .../static/src/js/basic_controller.js | 10 ++++++++++ .../views/stock_barcodes_option_view.xml | 4 ++++ stock_barcodes/wizard/stock_barcodes_read.py | 20 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/stock_barcodes/models/stock_barcodes_option.py b/stock_barcodes/models/stock_barcodes_option.py index ae84cb5b4d3a..f9f93adb5093 100644 --- a/stock_barcodes/models/stock_barcodes_option.py +++ b/stock_barcodes/models/stock_barcodes_option.py @@ -91,6 +91,9 @@ class StockBarcodesOptionGroup(models.Model): help="If checked quantity will be accumulated to the existing record instead of " "overwrite it with the new quantity value", ) + display_notification = fields.Boolean( + string="Display Odoo notifications", + ) def get_option_value(self, field_name, attribute): option = self.option_ids.filtered(lambda op: op.field_name == field_name)[:1] diff --git a/stock_barcodes/static/src/js/basic_controller.js b/stock_barcodes/static/src/js/basic_controller.js index df9c4324ce6c..7fae7d0815b6 100644 --- a/stock_barcodes/static/src/js/basic_controller.js +++ b/stock_barcodes/static/src/js/basic_controller.js @@ -121,6 +121,16 @@ odoo.define("stock_barcodes.BasicController", function (require) { } else if (message.sound === "ko") { this.$sound_ko[0].play(); } + } else if ( + notif_type === + "stock_barcodes_notify-" + this.initialState.data.id + ) { + this.displayNotification({ + title: notif.payload.title, + message: notif.payload.message, + type: notif.payload.type, + sticky: true, + }); } } }, diff --git a/stock_barcodes/views/stock_barcodes_option_view.xml b/stock_barcodes/views/stock_barcodes_option_view.xml index 4e1b1cdbdd10..da2e112fd4f6 100644 --- a/stock_barcodes/views/stock_barcodes_option_view.xml +++ b/stock_barcodes/views/stock_barcodes_option_view.xml @@ -13,6 +13,10 @@ + diff --git a/stock_barcodes/wizard/stock_barcodes_read.py b/stock_barcodes/wizard/stock_barcodes_read.py index bb11b4214ae1..23d6725a0fe7 100644 --- a/stock_barcodes/wizard/stock_barcodes_read.py +++ b/stock_barcodes/wizard/stock_barcodes_read.py @@ -815,3 +815,23 @@ def action_clean_message(self): def action_keep_result_package(self): self.keep_result_package = not self.keep_result_package + + def display_notification( + self, message, message_type="warning", title=False, sticky=True + ): + """Send notifications to web client + message_type: + [options.type='warning'] 'info', 'success', 'warning', 'danger' or '' + See web/static/src/legacy/js/core/service_mixins.js#L241 to implement more + options. + sticky: Permanent notification until user removes it + """ + if self.option_group_id.display_notification: + message = {"message": message, "type": message_type, "sticky": sticky} + if title: + message["title"] = title + self.env["bus.bus"]._sendone( + "stock_barcodes-{}".format(self.ids[0]), + "stock_barcodes_notify-{}".format(self.ids[0]), + message, + )