| 1 |
|
| 2 |
|
| 3 |
import uuid |
| 4 |
from odoo import fields, models, api |
| 5 |
|
| 6 |
|
| 7 |
class PosBusMixin(models.AbstractModel): |
| 8 |
_name = 'pos.bus.mixin' |
| 9 |
_description = "Bus Mixin" |
| 10 |
|
| 11 |
access_token = fields.Char('Security Token', copy=False) |
| 12 |
|
| 13 |
@api.model_create_multi |
| 14 |
def create(self, vals_list): |
| 15 |
records = super().create(vals_list) |
| 16 |
for record in records: |
| 17 |
record._ensure_access_token() |
| 18 |
return records |
| 19 |
|
| 20 |
def _ensure_access_token(self): |
| 21 |
if self.access_token: |
| 22 |
return self.access_token |
| 23 |
token = self.access_token = str(uuid.uuid4()) |
| 24 |
return token |
| 25 |
|
| 26 |
def _notify(self, *notifications, private=True) -> None: |
| 27 |
""" Send a notification to the bus. |
| 28 |
ex: one notification: ``self._notify('STATUS', {'status': 'closed'})`` |
| 29 |
multiple notifications: ``self._notify(('STATUS', {'status': 'closed'}), ('TABLE_ORDER_COUNT', {'count': 2}))`` |
| 30 |
""" |
| 31 |
self.ensure_one() |
| 32 |
self._ensure_access_token() |
| 33 |
if isinstance(notifications[0], str): |
| 34 |
if len(notifications) != 2: |
| 35 |
raise ValueError("If you want to send a single notification, you must provide a name: str and a message: any") |
| 36 |
notifications = [notifications] |
| 37 |
for name, message in notifications: |
| 38 |
self.env["bus.bus"]._sendone( |
| 39 |
self.access_token, f"{self.access_token}-{name}" if private else name, message |
| 40 |
) |