Víctor Martínez
4 years ago
10 changed files with 167 additions and 277 deletions
-
22base_comment_template/migrations/13.0.1.0.0/pre-migration.py
-
12base_comment_template/migrations/13.0.2.0.0/pre-migration.py
-
26base_comment_template/migrations/14.0.1.0.0/pre-migration.py
-
1base_comment_template/models/__init__.py
-
151base_comment_template/models/base_comment_template.py
-
45base_comment_template/models/comment_template.py
-
11base_comment_template/models/res_partner.py
-
151base_comment_template/tests/test_base_comment_template.py
-
6base_comment_template/views/base_comment_template_view.xml
-
19base_comment_template/views/res_partner_view.xml
@ -0,0 +1,22 @@ |
|||
# Copyright 2020 NextERP Romania SRL |
|||
# Copyright 2021 Tecnativa - Víctor Martínez |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
from openupgradelib import openupgrade |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
# Not tested |
|||
openupgrade.logged_query( |
|||
env.cr, |
|||
""" |
|||
INSERT INTO base_comment_template_res_partner_rel |
|||
(res_partner_id, base_comment_template_id) |
|||
SELECT SPLIT_PART(ip.res_id, ',', 2)::int AS res_partner_id, |
|||
SPLIT_PART(ip.value_reference, ',', 2)::int AS base_comment_template_id |
|||
FROM ir_property ip |
|||
JOIN ir_model_fields imf ON ip.fields_id = imf.id |
|||
WHERE imf.name = 'property_comment_template_id' |
|||
AND imf.model = 'res.partner' |
|||
""", |
|||
) |
@ -0,0 +1,12 @@ |
|||
# Copyright 2021 Tecnativa - Víctor Martínez |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
from openupgradelib import openupgrade # pylint: disable=W7936 |
|||
|
|||
field_renames = [ |
|||
("base.comment.template", "base_comment_template", "priority", "sequence"), |
|||
] |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
openupgrade.rename_fields(env, field_renames) |
@ -1,26 +0,0 @@ |
|||
# Copyright 2020 NextERP Romania SRL |
|||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
|||
from openupgradelib import openupgrade |
|||
|
|||
|
|||
@openupgrade.migrate() |
|||
def migrate(env, version): |
|||
# Not tested |
|||
properties = env["ir.property"].search( |
|||
[ |
|||
( |
|||
"fields_id", |
|||
"=", |
|||
env.ref("base.field_res_partner_property_comment_template_id").id, |
|||
) |
|||
] |
|||
) |
|||
if properties: |
|||
for template in properties.mapped("value_reference"): |
|||
template_id = template.value_reference.split(",")[-1] |
|||
if template_id: |
|||
template = env["base.comment.template"].browse(template_id) |
|||
part_prop = properties.filtered(lambda p: p.value_reference == template) |
|||
template.partner_ids = [ |
|||
(prop["res_id"] or "").split(",")[-1] for prop in part_prop |
|||
] |
@ -1,3 +1,4 @@ |
|||
from . import base_comment_template |
|||
from . import comment_template |
|||
from . import res_partner |
|||
from . import ir_model |
@ -0,0 +1,45 @@ |
|||
# Copyright 2014 Guewen Baconnier (Camptocamp SA) |
|||
# Copyright 2013-2014 Nicolas Bessi (Camptocamp SA) |
|||
# Copyright 2020 NextERP Romania SRL |
|||
# Copyright 2021 Tecnativa - Víctor Martínez |
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
|||
from odoo import api, fields, models |
|||
from odoo.tools.safe_eval import safe_eval |
|||
|
|||
|
|||
class CommentTemplate(models.AbstractModel): |
|||
_name = "comment.template" |
|||
_description = ( |
|||
"base.comment.template to put header and footer " |
|||
"in reports based on created comment templates" |
|||
) |
|||
# This field allows to set any given field that determines the source partner for |
|||
# the comment templates downstream. |
|||
# E.g.: other models where the partner field is called customer_id. |
|||
_comment_template_partner_field_name = "partner_id" |
|||
|
|||
comment_template_ids = fields.Many2many( |
|||
compute="_compute_comment_template_ids", |
|||
comodel_name="base.comment.template", |
|||
string="Comment Template", |
|||
domain=lambda self: [("model_ids.model", "=", self._name)], |
|||
store=True, |
|||
readonly=False, |
|||
) |
|||
|
|||
@api.depends(_comment_template_partner_field_name) |
|||
def _compute_comment_template_ids(self): |
|||
for record in self: |
|||
partner = record[self._comment_template_partner_field_name] |
|||
record.comment_template_ids = [(5,)] |
|||
templates = self.env["base.comment.template"].search( |
|||
[ |
|||
("id", "in", partner.base_comment_template_ids.ids), |
|||
("model_ids.model", "=", self._name), |
|||
] |
|||
) |
|||
for template in templates: |
|||
if not template.domain or self in self.search( |
|||
safe_eval(template.domain) |
|||
): |
|||
record.comment_template_ids = [(4, template.id)] |
Write
Preview
Loading…
Cancel
Save
Reference in new issue