Skip to content

Commit

Permalink
[IMP] stock_barcodes: Allow display notifications to web client
Browse files Browse the repository at this point in the history
  • Loading branch information
sergio-teruel committed Mar 12, 2024
1 parent 96f3853 commit 559cfd4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions stock_barcodes/models/stock_barcodes_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions stock_barcodes/static/src/js/basic_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions stock_barcodes/views/stock_barcodes_option_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
</group>
<group>
<field name="code" />
<field
name="display_notification"
widget="boolean_toggle"
/>
</group>
</group>
<group string="Behavior settings">
Expand Down
20 changes: 20 additions & 0 deletions stock_barcodes/wizard/stock_barcodes_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

0 comments on commit 559cfd4

Please sign in to comment.