<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import locale
import re
import threading

from contextlib import contextmanager
from datetime import datetime

from odoo import api, models, fields, _

LOCALE_LOCK = threading.Lock()


@contextmanager
def setlocale(name):
    with LOCALE_LOCK:
        saved = locale.setlocale(locale.LC_ALL)
        try:
            yield locale.setlocale(locale.LC_ALL, (name, 'UTF-8'))
        except:
            yield locale.setlocale(locale.LC_ALL, name)
        finally:
            locale.setlocale(locale.LC_ALL, saved)


class ResPartner(models.Model):
    """ Add fields for retrieving values for communications.

        - Short address
            Mr. <NAME>
            Street
            City
            Country
    """
    _name = 'res.partner'
    _inherit = 'res.partner'

    salutation = fields.Char(compute='_compute_salutation')
    short_salutation = fields.Char(compute='_compute_salutation')
    informal_salutation = fields.Char(compute='_compute_salutation')
    gender = fields.Selection(related='title.gender', readonly=True)
    thankyou_preference = fields.Selection(
        '_get_delivery_preference', default='auto_digital', required=True)
    full_name = fields.Char(compute='_compute_full_name')
    short_address = fields.Char(compute='_compute_address')
    date_communication = fields.Char(compute='_compute_date_communication')

    @api.multi
    def _compute_salutation(self):
        for partner in self:
            if partner.title and partner.firstname and not partner.is_company:
                title = partner.title
                title_salutation = partner.env['ir.advanced.translation'].get(
                    'salutation', female=title.gender == 'F',
                    plural=title.plural
                ).title()
                title_name = title.name
                partner.salutation = title_salutation + ' ' + \
                    title_name + ' ' + partner.lastname
                partner.short_salutation = title_salutation + ' ' + partner.firstname
                partner.informal_salutation = title_salutation + ' ' + \
                    partner.firstname
            else:
                partner.salutation = _("Dear friends of ") + \
                    self.env.user.company_id.name
                partner.short_salutation = p.salutation
                partner.informal_salutation = ("Dear friend of ") + \
                    self.env.user.company_id.name

    @api.multi
    def _compute_full_name(self):
        for partner in self.filtered('firstname'):
            partner.full_name = partner.firstname + ' ' + partner.lastname

    @api.multi
    def _compute_address(self):
        # Replace line returns
        p = re.compile('\\n+')
        for partner in self:
            res = ''
            t_partner = partner.with_context(lang=partner.lang)
            if not partner.is_company and partner.title.shortcut:
                res = t_partner.title.shortcut + ' '
                if partner.firstname:
                    res += partner.firstname + ' '
                res += partner.lastname + '<br/>'
            res += t_partner.contact_address
            partner.short_address = p.sub('<br/>', res)

    @api.multi
    def _compute_date_communication(self):
        lang_map = {
            'fr_CH': u'le %d %B %Y',
            'fr': u'le %d %B %Y',
            'de_DE': u'%d. %B %Y',
            'de_CH': u'%d. %B %Y',<commit_msg>FIX bad flake8<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2016 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: <NAME> <<EMAIL>>
#
#    The licence is in the file __manifest__.py
#
##############################################################################
import locale
import re
import threading

from contextlib import contextmanager
from datetime import datetime

from odoo import api, models, fields, _

LOCALE_LOCK = threading.Lock()


@contextmanager
def setlocale(name):
    with LOCALE_LOCK:
        saved = locale.setlocale(locale.LC_ALL)
        try:
            yield locale.setlocale(locale.LC_ALL, (name, 'UTF-8'))
        except:
            yield locale.setlocale(locale.LC_ALL, name)
        finally:
            locale.setlocale(locale.LC_ALL, saved)


class ResPartner(models.Model):
    """ Add fields for retrieving values for communications.

        - Short address
            Mr. <NAME>
            Street
            City
            Country
    """
    _name = 'res.partner'
    _inherit = 'res.partner'

    salutation = fields.Char(compute='_compute_salutation')
    short_salutation = fields.Char(compute='_compute_salutation')
    informal_salutation = fields.Char(compute='_compute_salutation')
    gender = fields.Selection(related='title.gender', readonly=True)
    thankyou_preference = fields.Selection(
        '_get_delivery_preference', default='auto_digital', required=True)
    full_name = fields.Char(compute='_compute_full_name')
    short_address = fields.Char(compute='_compute_address')
    date_communication = fields.Char(compute='_compute_date_communication')

    @api.multi
    def _compute_salutation(self):
        for partner in self:
            if partner.title and partner.firstname and not partner.is_company:
                title = partner.title
                title_salutation = partner.env['ir.advanced.translation'].get(
                    'salutation', female=title.gender == 'F',
                    plural=title.plural
                ).title()
                title_name = title.name
                partner.salutation = title_salutation + ' ' + \
                    title_name + ' ' + partner.lastname
                partner.short_salutation =\
                    title_salutation + ' ' + partner.firstname
                partner.informal_salutation = title_salutation + ' ' + \
                    partner.firstname
            else:
                partner.salutation = _("Dear friends of ") + \
                    self.env.user.company_id.name
                partner.short_salutation = partner.salutation
                partner.informal_salutation = ("Dear friend of ") + \
                    self.env.user.company_id.name

    @api.multi
    def _compute_full_name(self):
        for partner in self.filtered('firstname'):
            partner.full_name = partner.firstname + ' ' + partner.lastname

    @api.multi
    def _compute_address(self):
        # Replace line returns
        p = re.compile('\\n+')
        for partner in self:
            res = ''
            t_partner = partner.with_context(lang=partner.lang)
            if not partner.is_company and partner.title.shortcut:
                res = t_partner.title.shortcut + ' '
                if partner.firstname:
                    res += partner.firstname + ' '
                res += partner.lastname + '<br/>'
            res += t_partner.contact_address
            partner.short_address = p.sub('<br/>', res)

    @api.multi
    def _compute_date_communication(self):
        lang_map = {
            'fr_CH': u'le %d %B %Y',
            'fr': u'le %d %B %Y',
            'de_DE': u'%d. %B %Y',
            'de_CH': u'%d. %B %Y',
{'fraction_non_alphanumeric': 0.14317468619246862, 'fraction_numerical': 0.0016997907949790796, 'mean_word_length': 2.200418410041841, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '5436007', 'n_tokens_mistral': 2212, 'n_tokens_neox': 2084, 'n_words': 443}