-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
hooks.py
76 lines (67 loc) · 2.19 KB
/
hooks.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Copyright 2021 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
import logging
from odoo import SUPERUSER_ID, api, tools
from odoo.addons.base.models.ir_model import query_insert
_logger = logging.getLogger(__file__)
def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
_init_server_action(env)
def _init_server_action(env):
"""Create server action if missing."""
# This is actually a trick to work around this error:
#
# psycopg2.IntegrityError: null value in column "activity_user_type"
# violates not-null constraint
#
# which happens when `mail` is installed,
# since it adds this field as required in DB.
#
# We DO NOT want to depend on mail for this problem...
# hence, here we go with this crazy dance :S
#
# Moreover, we are forced to use a query for this
# because if you use `model.create` you get
#
# ValueError: Invalid field 'activity_user_type' on model 'ir.actions.server'
#
# because the field is not yet in the env if the mail modules is not loaded 1st.
xid = "endpoint.server_action_registry_sync"
rec = env.ref(xid, False)
if rec:
return
model = env.ref("endpoint.model_endpoint_endpoint")
values = {
"name": "Sync registry",
"type": "ir.actions.server",
"model_id": model.id,
"model_name": model.model,
"binding_model_id": model.id,
"binding_type": "action",
"usage": "ir_actions_server",
"state": "code",
"code": """
records.filtered(lambda x: not x.registry_sync).write({"registry_sync": True})
""",
}
if tools.sql.column_exists(env.cr, "ir_act_server", "activity_user_type"):
values["activity_user_type"] = "specific"
ids = query_insert(
env.cr,
"ir_act_server",
[
values,
],
)
# Finally add an xmlid
module, id_ = xid.split(".", 1)
env["ir.model.data"].create(
{
"name": id_,
"module": module,
"model": "ir.actions.server",
"res_id": ids[0],
"noupdate": True,
}
)
_logger.info("Server action created")