Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
##############################################################################
#
# Xbus Common, for Odoo
# Copyright © 2023 XCG Consulting <https://xcg-consulting.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import uuid
from odoo import fields, models, tools
class XbusEnvelope(models.Model):
"""Represents an xbus envelope."""
_name = "xbus.envelope"
_description = "Xbus Envelope"
_auto = False
# No automatic column for this table, it is used with an external system
_log_access = False
_order = "create_date DESC, id DESC"
_rec_name = "uid"
create_date = fields.Datetime(string="Creation Date", readonly=True)
uid = fields.Char(string="UID", default=lambda self: uuid.uuid4())
comment = fields.Char()
message_ids = fields.One2many(
string="Messages", comodel_name="xbus.message", inverse_name="envelope_id"
)
message_count = fields.Integer(
string="Number of messages", compute="_compute_message_count", store=False
)
def _compute_message_count(self):
for record in self:
record.message_count = len(record.message_ids)
def init(self):
super().init()
self.env.cr.execute(
"""CREATE TABLE IF NOT EXISTS xbus_envelope (
id serial primary key,
create_date timestamp without time zone DEFAULT (now() at time zone 'utc'),
comment character varying not null,
uid uuid not null
)"""
)
tools.create_unique_index(
self._cr, "xbus_envelope_uid_uniq", self._table, ["uid"]
)
tools.create_index(
self._cr, "xbus_envelope_create_date_index", self._table, ["create_date"]
)