From a082d1f71b44b32089b5cf44d6636a2ff14cc8a2 Mon Sep 17 00:00:00 2001 From: darshan-serpent Date: Wed, 4 Jan 2017 17:54:23 +0530 Subject: [PATCH] [IMP] new field to perform search --- mail_message_name_search/models/__init__.py | 1 + .../models/mail_message.py | 67 +++++++++++-------- .../models/res_partner.py | 13 ++++ 3 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 mail_message_name_search/models/res_partner.py diff --git a/mail_message_name_search/models/__init__.py b/mail_message_name_search/models/__init__.py index 0b53dd7c..ee15c669 100644 --- a/mail_message_name_search/models/__init__.py +++ b/mail_message_name_search/models/__init__.py @@ -5,3 +5,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from . import mail_message +from . import res_partner diff --git a/mail_message_name_search/models/mail_message.py b/mail_message_name_search/models/mail_message.py index 0250e427..0c8ca94b 100644 --- a/mail_message_name_search/models/mail_message.py +++ b/mail_message_name_search/models/mail_message.py @@ -4,9 +4,37 @@ # © 2016 Serpent Consulting Services Pvt. Ltd. () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). -from openerp import api, models +from openerp import api, fields, models from lxml import etree from openerp.osv import expression +from openerp.osv.orm import setup_modifiers + + +class MailThread(models.AbstractModel): + + _inherit = 'mail.thread' + + def _search_message_content(self, operator, value): + domain = [('model', '=', self._name), '|', '|', + ('record_name', operator, value), + ('subject', operator, value), ('body', operator, value)] + + if operator in expression.NEGATIVE_TERM_OPERATORS: + domain = domain[2:] + recs = self.env['mail.message'].search(domain) + return [('id', 'in', recs.mapped('res_id'))] + + @api.multi + def _compute_message_content(self): + """ We don't really need to show any content. This field is to be + used only by searches""" + return '' + + message_content = fields.Text( + string='Messages', + help='Message content, to be used only in searches', + compute="_compute_message_content", + search='_search_message_content') _base_fields_view_get = models.BaseModel.fields_view_get @@ -22,41 +50,24 @@ def _custom_fields_view_get(self, view_id=None, view_type='form', # Tricky super call res = _base_fields_view_get(self, view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu) - if view_type == 'search' and self._fields.get('message_ids'): + if view_type == 'search' and self._fields.get('message_content'): doc = etree.XML(res['arch']) + res['fields'].update({ + 'message_content': { + 'type': 'char', + 'string': 'Message content', + } + }) + for node in doc.xpath("//field[1]"): # Add message_ids in search view elem = etree.Element('field', { - 'name': 'message_ids', - 'domain': "[('model','=', %s)]" % self._model + 'name': 'message_content', }) + setup_modifiers(elem) node.addnext(elem) - # Register message_ids in fields - res['fields'].update({ - 'message_ids': { - 'type': 'one2many', - 'relation': 'mail.message', - 'string': 'Messages', - } - }) res['arch'] = etree.tostring(doc) return res models.BaseModel.fields_view_get = _custom_fields_view_get - - -class MailMessage(models.Model): - - _inherit = 'mail.message' - - @api.model - def name_search(self, name='', args=None, operator='ilike', limit=100): - """Override to search on multiple fields""" - args = args or [] - domain = ['|', '|', ('record_name', operator, name), - ('subject', operator, name), ('body', operator, name)] - if operator in expression.NEGATIVE_TERM_OPERATORS: - domain = domain[2:] - rec = self.search(domain + args, limit=limit) - return rec.name_get() diff --git a/mail_message_name_search/models/res_partner.py b/mail_message_name_search/models/res_partner.py new file mode 100644 index 00000000..6aa6da6c --- /dev/null +++ b/mail_message_name_search/models/res_partner.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +# © 2016 Eficent Business and IT Consulting Services S.L. +# (http://www.eficent.com) +# © 2016 Serpent Consulting Services Pvt. Ltd. () +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from openerp import models + + +class Partner(models.Model): + + _name = 'res.partner' + _inherit = ['res.partner', 'mail.thread']