<commit_before># -*- 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 and name not in self._columns:
        field = fields.One2many('ir.attachment', string='Attachments', automatic=True,
                                compute='_get_attachments', search='_search_attachments')
        setattr(type(self), name, 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 or name in self._columns):
        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 or name in self._columns):
        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<commit_msg>[FIX] Adapt smile_attachment to the last version of Odoo<commit_after># -*- 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
{'fraction_non_alphanumeric': 0.14239773604322098, 'fraction_numerical': 0.002829945973758683, 'mean_word_length': 3.4555873925501435, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6324289', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2232, 'n_words': 603}