-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
35 lines (30 loc) · 1.3 KB
/
hooks.py
File metadata and controls
35 lines (30 loc) · 1.3 KB
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
###############################################################################
# License, author and contributors information in: #
# __manifest__.py file at the root folder of this module. #
###############################################################################
from odoo import SUPERUSER_ID, api
def pre_init_hook(cr):
"""
With this pre-init-hook we want to avoid error when creating the UNIQUE
number constraint when the module is installed and before the
post-init-hook is launched.
"""
cr.execute("ALTER TABLE contract_contract ADD COLUMN number character varying;")
cr.execute("UPDATE contract_contract SET number = id;")
def post_init_hook(cr, registry):
"""
This post-init-hook will update all existing contract assigning them the
corresponding sequence number.
"""
env = api.Environment(cr, SUPERUSER_ID, dict())
contract_obj = env["contract.contract"]
sequence_obj = env["ir.sequence"]
contracts = contract_obj.search([], order="id")
for contract_id in contracts.ids:
cr.execute(
"UPDATE contract_contract SET number = %s WHERE id = %s;",
(
sequence_obj.next_by_code("contract.contract"),
contract_id,
),
)