Skip to content

Commit

Permalink
[MIG] project_task_code: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedas Żilinskas committed Oct 22, 2024
1 parent f2723dc commit 680712a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 33 deletions.
2 changes: 1 addition & 1 deletion project_task_code/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Sequential Code for Tasks",
"version": "16.0.1.1.0",
"version": "17.0.1.0.0",
"category": "Project Management",
"author": "OdooMRP team, "
"AvanzOSC, "
Expand Down
13 changes: 5 additions & 8 deletions project_task_code/hooks.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
# 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):
def pre_init_hook(env):
"""
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;")
env.cr.execute("ALTER TABLE project_task ADD COLUMN code character varying;")
env.cr.execute("UPDATE project_task SET code = id;")


def post_init_hook(cr, registry):
def post_init_hook(env):
"""
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(
env.cr.execute(
"UPDATE project_task SET code = %s WHERE id = %s;",
(
sequence_obj.next_by_code("project.task"),
Expand Down
24 changes: 14 additions & 10 deletions project_task_code/models/project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
from odoo import _, api, fields, models


PROJECT_TASK_WRITABLE_FIELDS = {
"code",
}

class ProjectTask(models.Model):
_inherit = "project.task"
_rec_names_search = ["name", "code"]
Expand All @@ -24,21 +28,21 @@ class ProjectTask(models.Model):
),
]

@property
def SELF_WRITABLE_FIELDS(self):
return super().SELF_WRITABLE_FIELDS | PROJECT_TASK_WRITABLE_FIELDS

@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
if vals.get("code", "/") == "/":
vals["code"] = (
self.env["ir.sequence"].next_by_code("project.task") or "/"
# `sudo()` for portal users
self.env["ir.sequence"].sudo().next_by_code("project.task") or "/"
)
return super().create(vals_list)

def name_get(self):
result = super().name_get()
new_result = []

for task in result:
rec = self.browse(task[0])
name = f"[{rec.code}] {task[1]}"
new_result.append((rec.id, name))
return new_result
@api.depends("name", "code")
def _compute_display_name(self):
for task in self:
task.display_name = f"[{task.code}] {task.name}" if task.code else task.name
1 change: 1 addition & 0 deletions project_task_code/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- Saran Lim. \<<[email protected]>\>
- Tharathip Chaweewongphan \<<[email protected]>\>
- Ruchir Shukla \<<[email protected]>\>
- Nedas Žilinskas \<<[email protected]>\>
4 changes: 2 additions & 2 deletions project_task_code/tests/test_project_task_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_name_get(self):
"name": "Task Testing Get Name",
}
)
result = project_task.name_get()
self.assertEqual(result[0][1], "[%s] Task Testing Get Name" % code)
result = project_task.display_name
self.assertEqual(result, "[%s] Task Testing Get Name" % code)

def test_name_search(self):
project_task = self.env["project.task"].create(
Expand Down
15 changes: 3 additions & 12 deletions project_task_code/views/project_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_kanban" />
<field name="arch" type="xml">
<xpath
expr="//strong[hasclass('o_kanban_record_title')]/t/field[@name='name']"
position="before"
>
<span>[<field name="code" />] </span>
</xpath>
<xpath
expr="//strong[hasclass('o_kanban_record_title')]/s/field[@name='name']"
position="before"
>
<span>[<field name="code" />] </span>
</xpath>
<field name="name" position="before">
<field name="code" />
</field>
</field>
</record>
<record id="project_task_code_search_view" model="ir.ui.view">
Expand Down

0 comments on commit 680712a

Please sign in to comment.