forked from OCA/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.py
30 lines (25 loc) · 1.01 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
# Copyright 2016 Tecnativa <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
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
code constraint when the module is installed and before the post-init-hook
is launched.
"""
cr.execute("ALTER TABLE project_task " "ADD COLUMN code character varying;")
cr.execute("UPDATE project_task " "SET code = id;")
def post_init_hook(cr, registry):
"""
This post-init-hook will update all existing task assigning them the
corresponding sequence code.
"""
env = api.Environment(cr, SUPERUSER_ID, dict())
task_obj = env["project.task"]
sequence_obj = env["ir.sequence"]
tasks = task_obj.search([], order="id")
for task_id in tasks.ids:
cr.execute(
"UPDATE project_task " "SET code = %s " "WHERE id = %s;",
(sequence_obj.next_by_code("project.task"), task_id),
)