Skip to content

Commit

Permalink
[MIG] project_key: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Borruso committed Nov 29, 2024
1 parent 5f5b550 commit 68c3589
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 70 deletions.
2 changes: 1 addition & 1 deletion project_key/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Project Key",
"summary": "Module decorates projects and tasks with Project Key",
"category": "Project",
"version": "16.0.1.0.2",
"version": "18.0.1.0.0",
"license": "LGPL-3",
"author": "Modoolar, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/project",
Expand Down
5 changes: 1 addition & 4 deletions project_key/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@
# License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html).


def post_init_hook(cr, registry):
from odoo import SUPERUSER_ID, api

env = api.Environment(cr, SUPERUSER_ID, {})
def post_init_hook(env):
env["project.project"]._set_default_project_key()
52 changes: 28 additions & 24 deletions project_key/models/project_project.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2017 - 2018 Modoolar <[email protected]>
# License LGPLv3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.en.html).

from odoo import _, api, fields, models
from odoo import api, fields, models
from odoo.tools import config


Expand All @@ -13,7 +13,7 @@ class Project(models.Model):
comodel_name="ir.sequence", string="Key Sequence", ondelete="restrict"
)

key = fields.Char(size=10, required=False, index=True, copy=False)
key = fields.Char(size=10, index=True, copy=False)

_sql_constraints = [
("project_key_unique", "UNIQUE(key)", "Project key must be unique")
Expand All @@ -32,34 +32,36 @@ def _onchange_project_name(self):

@api.model_create_multi
def create(self, vals_list):
new_projects = self.env["project.project"]
for vals in vals_list:
key = vals.get("key", False)
if not key:
vals["key"] = self.generate_project_key(vals["name"])

# Tasks must be created after the project.
if vals.get("task_ids", False):
task_vals = vals.pop("task_ids")
else:
task_vals = []
task_vals = vals.pop("task_ids", [])

# The key sequences to create stories and tasks with keys, created with
# a project, must be linked to the project company to avoid security
# issues.
# Propagate the company ID, using the context key, to fill the
# sequences company.
company_id = vals.get("company_id")
if company_id:
self = self.with_context(project_sequence_company=company_id)
new_project = super().create(vals)
new_projects |= new_project

# The key sequences to create stories and tasks with keys, created with
# a project, must be linked to the project company to avoid security
# issues.
# Propagate the company ID, using the context key, to fill the
# sequences company.
company_id = vals.get("company_id")
if company_id:
new_project = new_project.with_context(

Check warning on line 54 in project_key/models/project_project.py

View check run for this annotation

Codecov / codecov/patch

project_key/models/project_project.py#L54

Added line #L54 was not covered by tests
project_sequence_company=company_id
)

new_project = super(Project, self).create(vals)
new_project.create_sequence()
new_project.create_sequence()

# Tasks must be created after the project.
if task_vals:
new_project.write({"task_ids": task_vals})
# Tasks must be created after the project.
if task_vals:
new_project.write({"task_ids": task_vals})

Check warning on line 62 in project_key/models/project_project.py

View check run for this annotation

Codecov / codecov/patch

project_key/models/project_project.py#L62

Added line #L62 was not covered by tests

return new_project
return new_projects

def write(self, values):
update_key = False
Expand All @@ -68,7 +70,7 @@ def write(self, values):
key = values["key"]
update_key = self.key != key

res = super(Project, self).write(values)
res = super().write(values)

if update_key:
# Here we don't expect to have more than one record
Expand All @@ -83,7 +85,7 @@ def unlink(self):
sequence = project.task_key_sequence_id
project.task_key_sequence_id = False
sequence.sudo().unlink()
return super(Project, self).unlink()
return super().unlink()

def create_sequence(self):
"""
Expand Down Expand Up @@ -111,7 +113,9 @@ def _prepare_sequence_data(self, init=True):
for number_increment and number_next_actual
"""
values = {
"name": "{} {}".format(_("Project task sequence for project"), self.name),
"name": "{} {}".format(
self.env._("Project task sequence for project"), self.name
),
"implementation": "standard",
"code": f"project.task.key.{self.id}",
"prefix": f"{self.key}-",
Expand Down Expand Up @@ -163,7 +167,7 @@ def _generate_project_unique_key(self, text):
counter = 0
while not unique_key:
if counter != 0:
res = "%s%s" % (text, counter)
res = f"{text}{counter}"
unique_key = not bool(self.search([("key", "=", res)]))
counter += 1

Expand Down
27 changes: 12 additions & 15 deletions project_key/models/project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Task(models.Model):
_inherit = "project.task"
_rec_names_search = ["key", "name"]

key = fields.Char(size=20, required=False, index=True)
key = fields.Char(size=20, index=True)

url = fields.Char(string="URL", compute="_compute_task_url")

Expand All @@ -30,17 +30,17 @@ def create(self, vals_list):
project_id = ctx("default_project_id", False)

if not project_id and ctx("active_model", False) == "project.project":
project_id = ctx("active_id", False)
project_id = ctx("id", False)

if project_id:
project = self.env["project.project"].browse(project_id)
vals["key"] = project.get_next_task_key()
return super(Task, self).create(vals_list)
return super().create(vals_list)

def write(self, vals):
project_id = vals.get("project_id", False)
if not project_id:
return super(Task, self).write(vals)
return super().write(vals)

project = self.env["project.project"].browse(project_id)
for task in self:
Expand All @@ -50,7 +50,7 @@ def write(self, vals):
values = self.prepare_task_for_project_switch(task, project)
super(Task, task).write(values)

return super(Task, self).write(vals)
return super().write(vals)

def prepare_task_for_project_switch(self, task, project):
data = {"key": project.get_next_task_key(), "project_id": project.id}
Expand All @@ -62,14 +62,11 @@ def prepare_task_for_project_switch(self, task, project):
]
return data

def name_get(self):
result = []

for record in self:
@api.depends("key", "name")
def _compute_display_name(self):
for task in self:
task_name = []
if record.key:
task_name.append(record.key)
task_name.append(record.name)
result.append((record.id, " - ".join(task_name)))

return result
if task.key:
task_name.append(task.key)
task_name.append(task.name)
task.display_name = " - ".join(task_name)
6 changes: 3 additions & 3 deletions project_key/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_02_compute_task_url(self):

def test_03_create_task_project_in_context(self):
self.Task.with_context(
active_model="project.project", active_id=self.project_1.id
active_model="project.project", id=self.project_1.id
).create({"name": "4"})

def test_04_no_switch_project(self):
Expand All @@ -33,11 +33,11 @@ def test_05_switch_project(self):
self.assertEqual(self.task12.key, "ODOO-3")

def test_06_name_search(self):
oca_tasks = self.Task.name_search("OCA")
oca_tasks = self.Task.name_search("OCA-")
self.assertEqual(len(oca_tasks), 2)

non_oca_task_ids = [
x[0] for x in self.Task.name_search("OCA", operator="not ilike")
x[0] for x in self.Task.name_search("OCA-", operator="not ilike")
]

oca_tasks = self.Task.browse(non_oca_task_ids).filtered(
Expand Down
30 changes: 7 additions & 23 deletions project_key/views/project_key_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
<field name="inherit_id" ref="project.edit_project" />
<field name="arch" type="xml">
<field name="partner_id" position="before">
<field name="key" required="1" />
<field name="key" required="True" />
</field>
</field>
</record>
<record id="view_project_extend_with_key" model="ir.ui.view">
<field name="name">project.project.tree</field>
<field name="name">project.project.list</field>
<field name="model">project.project</field>
<field name="inherit_id" ref="project.view_project" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="key" required="0" readonly="1" />
<field name="key" readonly="True" />
</field>
</field>
</record>
Expand All @@ -42,12 +42,12 @@
<field name="inherit_id" ref="project.view_task_form2" />
<field name="arch" type="xml">
<field name="name" position="before">
<field name="key" readonly="1" nolabel="1" class="oe_read_only" />
<field name="key" readonly="True" nolabel="1" class="oe_read_only" />
</field>
</field>
</record>
<record id="view_task_tree2_extend_with_key" model="ir.ui.view">
<field name="name">project.task.tree</field>
<record id="view_task_list2_extend_with_key" model="ir.ui.view">
<field name="name">project.task.list</field>
<field name="model">project.task</field>
<field name="inherit_id" ref="project.view_task_tree2" />
<field eval="2" name="priority" />
Expand Down Expand Up @@ -78,10 +78,7 @@
<field name="url" />
<field name="key" />
</field>
<xpath
expr="//t[@t-name='kanban-box']//field[@name='name']/parent::s/parent::strong"
position="before"
>
<xpath expr="//t[@t-name='card']//field[@name='name']" position="before">
<a t-att-href="record.url.raw_value">
<field name="key" />
</a>
Expand All @@ -98,17 +95,4 @@
</div>
</field>
</record>
<record id="view_project_kanban_details" model="ir.ui.view">
<field name="name">project.project.kanban</field>
<field name="model">project.project</field>
<field name="inherit_id" ref="project.view_project_kanban" />
<field name="arch" type="xml">
<field name="display_name" position="after">
<field name="key" />
</field>
<xpath expr="//t[@t-esc='record.display_name.value']" position="before">
<t t-esc="record.key.value" /> -
</xpath>
</field>
</record>
</odoo>

0 comments on commit 68c3589

Please sign in to comment.