-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] project_key: Migration to 18.0
- Loading branch information
Showing
6 changed files
with
52 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
@@ -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") | ||
|
@@ -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( | ||
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}) | ||
|
||
return new_project | ||
return new_projects | ||
|
||
def write(self, values): | ||
update_key = False | ||
|
@@ -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 | ||
|
@@ -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): | ||
""" | ||
|
@@ -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}-", | ||
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters