Browse Source

[IMP] new field to perform search

pull/143/head
darshan-serpent 8 years ago
parent
commit
a082d1f71b
  1. 1
      mail_message_name_search/models/__init__.py
  2. 67
      mail_message_name_search/models/mail_message.py
  3. 13
      mail_message_name_search/models/res_partner.py

1
mail_message_name_search/models/__init__.py

@ -5,3 +5,4 @@
# 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 . import mail_message from . import mail_message
from . import res_partner

67
mail_message_name_search/models/mail_message.py

@ -4,9 +4,37 @@
# © 2016 Serpent Consulting Services Pvt. Ltd. (<http://www.serpentcs.com>) # © 2016 Serpent Consulting Services Pvt. Ltd. (<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 openerp import api, models
from openerp import api, fields, models
from lxml import etree from lxml import etree
from openerp.osv import expression 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 _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 # Tricky super call
res = _base_fields_view_get(self, view_id=view_id, view_type=view_type, res = _base_fields_view_get(self, view_id=view_id, view_type=view_type,
toolbar=toolbar, submenu=submenu) 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']) doc = etree.XML(res['arch'])
res['fields'].update({
'message_content': {
'type': 'char',
'string': 'Message content',
}
})
for node in doc.xpath("//field[1]"): for node in doc.xpath("//field[1]"):
# Add message_ids in search view # Add message_ids in search view
elem = etree.Element('field', { elem = etree.Element('field', {
'name': 'message_ids',
'domain': "[('model','=', %s)]" % self._model
'name': 'message_content',
}) })
setup_modifiers(elem)
node.addnext(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) res['arch'] = etree.tostring(doc)
return res return res
models.BaseModel.fields_view_get = _custom_fields_view_get 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()

13
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. (<http://www.serpentcs.com>)
# 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']
Loading…
Cancel
Save