# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2010 Smile (<http://www.smile.fr>). All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from lxml import etree

from openerp import api, fields
from openerp.models import Model

native_setup_fields = Model._setup_fields
native_fields_view_get = Model.fields_view_get


def _get_attachments_field_name(self):
    name = 'attachment_ids'
    if self._inherits:
        name = 'attachment_%s_ids' % self._table
    return name


@api.one
@api.depends()
def _get_attachments(self):
    name = self._get_attachments_field_name()
    setattr(self, name, False)


def _search_attachments(self, operator, value):
    recs = self.env['ir.attachment'].search([('res_model', '=', self._name),
                                             '|', '|',
                                             ('description', operator, value),
                                             ('index_content', operator, value),
                                             ('datas_fname', operator, value)])
    return [('id', 'in', [rec.res_id for rec in recs])]


@api.model
def new_setup_fields(self):
    name = self._get_attachments_field_name()
    if name not in self._fields:
        new_field = fields.One2many('ir.attachment', string='Attachments',
                                    compute='_get_attachments', search='_search_attachments')
        setattr(type(self), name, new_field)
        self._add_field(name, new_field)
    native_setup_fields(self)


@api.v7
def new_fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
    res = native_fields_view_get(self, cr, uid, view_id, view_type, context, toolbar, submenu)
    name = self._get_attachments_field_name()
    if view_type == 'search' and (name in self._fields):
        View = self.pool['ir.ui.view']
        arch_etree = etree.fromstring(res['arch'])
        element = etree.Element('field', name=name)
        arch_etree.insert(-1, element)
        res['arch'], res['fields'] = View.postprocess_and_fields(cr, uid, self._name, arch_etree, view_id, context=context)
    return res


@api.v8
def new_fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    res = native_fields_view_get(self, view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)
    name = self._get_attachments_field_name()
    if view_type == 'search' and (name in self._fields):
        View = self.env['ir.ui.view']
        arch_etree = etree.fromstring(res['arch'])
        element = etree.Element('field', name=name)
        arch_etree.insert(-1, element)
        res['arch'], res['fields'] = View.postprocess_and_fields(self._name, arch_etree, view_id)
    return res

Model._get_attachments_field_name = _get_attachments_field_name
Model._setup_fields = new_setup_fields
Model._get_attachments = _get_attachments
Model._search_attachments = _search_attachments
Model.fields_view_get = new_fields_view_get

@@ -1494,180 +1494,71 @@
     
-recs = self.env%5B'ir.attachment'%5D.search(%5B('res_model', '=', self._name),%0A                                             '%7C', '%7C',%0A                                            
+domain = %5B%0A        ('res_model', '=', self._name),%0A        '%7C',
  ('d
@@ -1600,159 +1600,308 @@
     
-       
+('datas_fname', operator, value),%0A
     
+%5D%0A
     
-                      ('index_content', operator, value),%0A                                             ('datas_fname', operator, value)%5D
+if self.env%5B'module.module'%5D.search(%5B('name', '=', 'document'), ('state', 'in', ('to upgrade', 'installed'))%5D, limit=1):%0A        domain = domain%5B:2%5D + %5B'%7C', ('index_content', operator, value)%5D + domain%5B2:%5D%0A    recs = self.env%5B'ir.attachment'%5D.search(domain
 )%0A  

Correct _search_attachments