Browse Source

[IMP] base_comment_template: Code refactorization

myc-14.0-py3o
teodoralexandru@nexterp.ro 4 years ago
committed by Víctor Martínez
parent
commit
e1011fa519
  1. 28
      base_comment_template/models/base_comment_template.py
  2. 42
      base_comment_template/tests/test_base_comment_template.py

28
base_comment_template/models/base_comment_template.py

@ -16,34 +16,6 @@ class CommentTemplate(models.AbstractModel):
"in reports based on created comment templates"
)
base_comment_template_ids = fields.Many2many(
comodel_name="base.comment.template",
string="Comment templates",
compute="_compute_base_comment_template_ids",
search="_search_follower_partners",
help="Templates that can be included in your reports",
)
def _compute_base_comment_template_ids(self):
company_id = self.env.company.id
present_model_id = self.env["ir.model"].search([("name", "=", self._name)])
templates = (
self.env["base.comment.template"]
.sudo()
.search(
[
("model_ids", "in", present_model_id.id),
"|",
("company_id", "=", company_id),
("company_id", "=", False),
]
)
)
# using read() below is much faster than followers.mapped('res_id')
model_mako_templates_ids = templates.mapped("id")
for record in self:
record.base_comment_template_ids = record.id in model_mako_templates_ids
def get_comment_template(
self, position="before_lines", company_id=False, partner_id=False
):

42
base_comment_template/tests/test_base_comment_template.py

@ -1,5 +1,7 @@
# Copyright 2020 NextERP Romania SRL
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from lxml import etree
from odoo.tests import common
from .fake_models import ResUsers, setup_test_model, teardown_test_model
@ -19,7 +21,7 @@ class TestCommentTemplate(common.SavepointCase):
cls.user2 = cls.env.ref("base.demo_user0")
cls.partner_id = cls.env.ref("base.res_partner_12")
cls.partner2_id = cls.env.ref("base.res_partner_10")
cls.company = cls.env["res.company"].create({"name": "Test Company 1"})
cls.company = cls.env["res.company"].search([], limit=1)
cls.before_template_id = cls.env["base.comment.template"].create(
{
"name": "before_lines",
@ -126,3 +128,41 @@ class TestCommentTemplate(common.SavepointCase):
)
self.assertEqual(new_template.text, "Text before lines 1")
def test_check_partners_in_company_id(self):
""" should rise any error because exist the same model,
domain, position and priority"""
with self.assertRaises(Exception) as context:
self.before_template_id_2 = self.env["base.comment.template"].create(
{
"name": "before_lines",
"text": "Text before lines",
"model_ids": [(6, 0, self.user_obj.ids)],
"priority": 5,
}
)
self.assertTrue(
"The are other records with same models, priority, domain and position."
== context.exception.args[0]
)
# should not rise any error
self.before_template_id_3 = self.env["base.comment.template"].create(
{
"name": "before_lines",
"text": "Text before lines",
"model_ids": [(6, 0, self.user_obj.ids)],
"priority": 55,
}
)
def test_fields_view_get(
self, view_id=None, view_type="form", toolbar=False, submenu=False
):
bf_tmp_form_view = self.before_template_id.fields_view_get()
if view_type == "form":
doc = etree.XML(bf_tmp_form_view["arch"])
model_ids = doc.xpath("//field[@name='model_ids']")
domain = model_ids[0].attrib["domain"]
# if domain exist means that the filtering is done and the function is ok
self.assertTrue(domain != "")
Loading…
Cancel
Save