[IMP] base_search_mail_content: black, isort, prettier

This commit is contained in:
Ernesto Tejeda 2020-07-09 16:13:40 -04:00 committed by Eduardo De Miguel
parent 008e63597b
commit d7cd6e40eb
4 changed files with 39 additions and 47 deletions

View File

@ -7,16 +7,11 @@
{ {
"name": "Base Search Mail Content", "name": "Base Search Mail Content",
"version": "12.0.1.0.0", "version": "12.0.1.0.0",
"author": "Eficent," "author": "Eficent," "SerpentCS," "Tecnativa," "Odoo Community Association (OCA)",
"SerpentCS,"
"Tecnativa,"
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/social", "website": "https://github.com/OCA/social",
"category": "Social", "category": "Social",
"data": [ "data": ["data/trgm_index_data.xml"],
"data/trgm_index_data.xml",
],
"depends": ["mail", "base_search_fuzzy"], "depends": ["mail", "base_search_fuzzy"],
"license": "AGPL-3", "license": "AGPL-3",
'installable': True, "installable": True,
} }

View File

@ -4,64 +4,60 @@
# (<http://www.serpentcs.com>) # (<http://www.serpentcs.com>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from odoo import _, api, fields, models
from lxml import etree from lxml import etree
from odoo import _, api, fields, models
from odoo.osv import expression from odoo.osv import expression
from odoo.osv.orm import setup_modifiers from odoo.osv.orm import setup_modifiers
class MailThread(models.AbstractModel): class MailThread(models.AbstractModel):
_inherit = 'mail.thread' _inherit = "mail.thread"
def _search_message_content(self, operator, value): def _search_message_content(self, operator, value):
model_domain = [('model', '=', self._name)] model_domain = [("model", "=", self._name)]
if operator not in expression.NEGATIVE_TERM_OPERATORS: if operator not in expression.NEGATIVE_TERM_OPERATORS:
model_domain += ["|"] * 4 model_domain += ["|"] * 4
model_domain += [ model_domain += [
('record_name', operator, value), ("record_name", operator, value),
('subject', operator, value), ("subject", operator, value),
('body', operator, value), ("body", operator, value),
('email_from', operator, value), ("email_from", operator, value),
('reply_to', operator, value) ("reply_to", operator, value),
] ]
recs = self.env['mail.message'].search(model_domain) recs = self.env["mail.message"].search(model_domain)
return [('id', 'in', recs.mapped('res_id'))] return [("id", "in", recs.mapped("res_id"))]
message_content = fields.Text( message_content = fields.Text(
string='Message Content', string="Message Content",
help='Message content, to be used only in searches', help="Message content, to be used only in searches",
compute=lambda self: False, compute=lambda self: False,
search='_search_message_content') search="_search_message_content",
)
@api.model @api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, def fields_view_get(
submenu=False): self, view_id=None, view_type="form", toolbar=False, submenu=False
):
""" """
Override to add message_content field in all the objects Override to add message_content field in all the objects
that inherits mail.thread that inherits mail.thread
""" """
res = super(MailThread, self).fields_view_get( res = super(MailThread, self).fields_view_get(
view_id=view_id, view_type=view_type, toolbar=toolbar, view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu
submenu=submenu) )
if view_type == 'search' and self._fields.get('message_content'): if view_type == "search" and self._fields.get("message_content"):
doc = etree.XML(res['arch']) doc = etree.XML(res["arch"])
res['fields'].update({ res["fields"].update(
'message_content': { {"message_content": {"type": "char", "string": _("Message Content")}}
'type': 'char', )
'string': _('Message Content'),
}
})
for node in doc.xpath("//field[last()]"): for node in doc.xpath("//field[last()]"):
# Add message_content in search view # Add message_content in search view
elem = etree.Element( elem = etree.Element("field", {"name": "message_content"})
'field',
{
'name': 'message_content',
})
setup_modifiers(elem) setup_modifiers(elem)
node.addnext(elem) node.addnext(elem)
res['arch'] = etree.tostring(doc) res["arch"] = etree.tostring(doc)
return res return res

View File

@ -8,7 +8,7 @@ from odoo import fields, models
class TrgmIndex(models.Model): class TrgmIndex(models.Model):
_inherit = 'trgm.index' _inherit = "trgm.index"
# We take advantage of field inheritance to redefine help instead of do # We take advantage of field inheritance to redefine help instead of do
# inheritance in views that throws an error # inheritance in views that throws an error

View File

@ -10,15 +10,16 @@ class TestBaseSearchMailContent(TransactionCase):
self.channel_obj = self.env["mail.channel"] self.channel_obj = self.env["mail.channel"]
def test_base_search_mail_content_1(self): def test_base_search_mail_content_1(self):
res = self.channel_obj.search( res = self.channel_obj.search([("message_content", "ilike", "xxxyyyzzz")])
[('message_content', 'ilike', 'xxxyyyzzz')])
self.assertFalse(res, "You have a channel with xxxyyyzzz :O") self.assertFalse(res, "You have a channel with xxxyyyzzz :O")
def test_base_search_mail_content_2(self): def test_base_search_mail_content_2(self):
res = self.channel_obj.load_views( res = self.channel_obj.load_views(
[[False, 'search']], {'load_fields': False, [[False, "search"]],
'load_filters': True, {"load_fields": False, "load_filters": True, "toolbar": True},
'toolbar': True}) )
self.assertIn( self.assertIn(
'message_content', res['fields_views']['search']['fields'], "message_content",
"message_content field was not detected") res["fields_views"]["search"]["fields"],
"message_content field was not detected",
)