<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
# Odoo, an open source suite of business apps
# This module copyright (C) 2015 bloopark systems (<http://bloopark.de>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import types
from openerp import api, fields, models
from openerp.osv import expression


def modify_selection_to_be_translatable():

    for k, v in fields._String.__dict__.items():
        if not k.startswith('__') and type(v) == types.FunctionType:
            setattr(fields.Selection, k, v)

modify_selection_to_be_translatable()


def exists_short_code(cr):
    cr.execute("select column_name from information_schema.columns "
               "where table_name='res_lang' AND column_name='short_code'")
    return cr.fetchall()


def update_lang_code_from_alias_in_expression():

    def extended_init(self, cr, uid, exp, table, context):

        self._unaccent = expression.get_unaccent_wrapper(cr)
        self.joins = []
        self.root_model = table

        # normalize and prepare the expression for parsing
        self.expression = expression.distribute_not(expression.normalize_domain(exp))

        # look for real lang from context before parse
        parse_ctx = context.copy()
        if parse_ctx.get('lang', False) and exists_short_code(cr):
            cr.execute("select code from res_lang where short_code = '%s'" % parse_ctx['lang'])
            res = cr.fetchall()
            if res and res[0]:
                parse_ctx.update({'lang': res[0][0]})

        # parse the domain expression
        self.parse(cr, uid, context=parse_ctx)

    setattr(expression.expression, '__init__', extended_init)
<commit_msg>Add update_translated_field to core.py<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
# Odoo, an open source suite of business apps
# This module copyright (C) 2015 bloopark systems (<http://bloopark.de>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import types
from openerp import api, fields, models
from openerp.osv import expression


def modify_selection_to_be_translatable():

    for k, v in fields._String.__dict__.items():
        if not k.startswith('__') and type(v) == types.FunctionType:
            setattr(fields.Selection, k, v)

modify_selection_to_be_translatable()


def exists_short_code(cr):
    cr.execute("select column_name from information_schema.columns "
               "where table_name='res_lang' AND column_name='short_code'")
    return cr.fetchall()


def update_translated_field():

    @api.model
    def _extended_generate_translated_field(self, table_alias, field, query):
        """
        Change short code lang (eg. en) for real code lang (eg. en_US)
        """
        lang = self._context.get('lang')
        lang_model = self.env['res.lang']
        if hasattr(lang_model, 'get_code_from_alias') and exists_short_code(self.env.cr):
            lang = lang_model.get_code_from_alias(self._context.get('lang'))
        if lang and lang != 'en_US':
            alias, alias_statement = query.add_join(
                (table_alias, 'ir_translation', 'id', 'res_id', field),
                implicit=False,
                outer=True,
                extra='"{rhs}"."name" = %s AND "{rhs}"."lang" = %s AND "{rhs}"."value" != %s',
                extra_params=["%s,%s" % (self._name, field), lang, ""],
            )
            return 'COALESCE("%s"."%s", "%s"."%s")' % (alias, 'value', table_alias, field)
        else:
            return '"%s"."%s"' % (table_alias, field)

    setattr(models.Model, '_generate_translated_field', _extended_generate_translated_field)

update_translated_field()


def update_lang_code_from_alias_in_expression():

    def extended_init(self, cr, uid, exp, table, context):

        self._unaccent = expression.get_unaccent_wrapper(cr)
        self.joins = []
        self.root_model = table

        # normalize and prepare the expression for parsing
        self.expression = expression.distribute_not(expression.normalize_domain(exp))

        # look for real lang from context before parse
        parse_ctx = context.copy()
        if parse_ctx.get('lang', False) and exists_short_code(cr):
            cr.execute("select code from res_lang where short_code = '%s'" % parse_ctx['lang'])
            res = cr.fetchall()
            if res and res[0]:
                parse_ctx.update({'lang': res[0][0]})

        # parse the domain expression
        self.parse(cr, uid, context=parse_ctx)

    setattr(expression.expression, '__init__', extended_init)

{'fraction_non_alphanumeric': 0.15153027278775782, 'fraction_numerical': 0.0029940119760479044, 'mean_word_length': 3.454074074074074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099672', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1639, 'n_words': 529}