<commit_before># -*- coding: utf-8 -*-

import re

from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError


def normalize_iban(iban):
    return re.sub('[\W_]', '', iban or '')

def pretty_iban(iban):
    """ return iban in groups of four characters separated by a single space """
    return ' '.join([iban[i:i + 4] for i in range(0, len(iban), 4)])

def get_bban_from_iban(iban):
    """ Returns the basic bank account number corresponding to an IBAN.
        Note : the BBAN is not the same as the domestic bank account number !
        The relation between IBAN, BBAN and domestic can be found here : http://www.ecbs.org/iban.htm
    """
    return normalize_iban(iban)[4:]

def validate_iban(iban):
    iban = normalize_iban(iban)
    if not iban:
        raise ValidationError(_("No IBAN !"))

    country_code = iban[:2].lower()
    if country_code not in _map_iban_template:
        raise ValidationError(_("The IBAN is invalid, it should begin with the country code"))

    iban_template = _map_iban_template[country_code]
    if len(iban) != len(iban_template.replace(' ', '')):
        raise ValidationError(_("The IBAN does not seem to be correct. You should have entered something like this %s\n"
            "Where B = National bank code, S = Branch code, C = Account No, k = Check digit") % iban_template)

    check_chars = iban[4:] + iban[:4]
    digits = int(''.join(str(int(char, 36)) for char in check_chars))  # BASE 36: 0..9,A..Z -> 0..35
    if digits % 97 != 1:
        raise ValidationError(_("This IBAN does not pass the validation check, please verify it."))


class ResPartnerBank(models.Model):
    _inherit = "res.partner.bank"

    acc_type = fields.Selection(selection_add=[("iban", "IBAN")])

    @api.one
    @api.depends('acc_number')
    def _compute_acc_type(self):
        try:
            validate_iban(self.acc_number)
            self.acc_type = 'iban'
        except ValidationError:
            super(ResPartnerBank, self)._compute_acc_type()

    def get_bban(self):
        if self.acc_type != 'iban':
            raise UserError(_("Cannot compute the BBAN because the account number is not an IBAN."))
        return get_bban_from_iban(self.acc_number)

    @api.model
    def create(self, vals):
        if (vals.get('acc_type') == 'iban') and vals.get('acc_number'):
            vals['acc_number'] = pretty_iban(normalize_iban(vals['acc_number']))
        return super(ResPartnerBank, self).create(vals)

    @api.multi
    def write(self, vals):
        if (vals.get('acc_type') == 'iban') and vals.get('acc_number'):
            vals['acc_number'] = pretty_iban(normalize_iban(vals['acc_number']))
        return super(ResPartnerBank, self).write(vals)

    @api.one
    @api.constrains('acc_number')
    def _check_iban(self):
        if self.acc_type == 'iban':
            validate_iban(self.acc_number)<commit_msg>[FIX] base_iban : iban is never pretty formatted<commit_after># -*- coding: utf-8 -*-

import re

from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError


def normalize_iban(iban):
    return re.sub('[\W_]', '', iban or '')

def pretty_iban(iban):
    """ return iban in groups of four characters separated by a single space """
    try:
        validate_iban(iban)
        iban = ' '.join([iban[i:i + 4] for i in range(0, len(iban), 4)])
    except ValidationError:
        pass
    return iban

def get_bban_from_iban(iban):
    """ Returns the basic bank account number corresponding to an IBAN.
        Note : the BBAN is not the same as the domestic bank account number !
        The relation between IBAN, BBAN and domestic can be found here : http://www.ecbs.org/iban.htm
    """
    return normalize_iban(iban)[4:]

def validate_iban(iban):
    iban = normalize_iban(iban)
    if not iban:
        raise ValidationError(_("No IBAN !"))

    country_code = iban[:2].lower()
    if country_code not in _map_iban_template:
        raise ValidationError(_("The IBAN is invalid, it should begin with the country code"))

    iban_template = _map_iban_template[country_code]
    if len(iban) != len(iban_template.replace(' ', '')):
        raise ValidationError(_("The IBAN does not seem to be correct. You should have entered something like this %s\n"
            "Where B = National bank code, S = Branch code, C = Account No, k = Check digit") % iban_template)

    check_chars = iban[4:] + iban[:4]
    digits = int(''.join(str(int(char, 36)) for char in check_chars))  # BASE 36: 0..9,A..Z -> 0..35
    if digits % 97 != 1:
        raise ValidationError(_("This IBAN does not pass the validation check, please verify it."))


class ResPartnerBank(models.Model):
    _inherit = "res.partner.bank"

    acc_type = fields.Selection(selection_add=[("iban", "IBAN")])

    @api.one
    @api.depends('acc_number')
    def _compute_acc_type(self):
        try:
            validate_iban(self.acc_number)
            self.acc_type = 'iban'
        except ValidationError:
            super(ResPartnerBank, self)._compute_acc_type()

    def get_bban(self):
        if self.acc_type != 'iban':
            raise UserError(_("Cannot compute the BBAN because the account number is not an IBAN."))
        return get_bban_from_iban(self.acc_number)

    @api.model
    def create(self, vals):
        if vals.get('acc_number'):
            vals['acc_number'] = pretty_iban(normalize_iban(vals['acc_number']))
        return super(ResPartnerBank, self).create(vals)

    @api.multi
    def write(self, vals):
        if vals.get('acc_number'):
            vals['acc_number'] = pretty_iban(normalize_iban(vals['acc_number']))
        return super(ResPartnerBank, self).write(vals)

    @api.one
    @api.constrains('acc_number')
    def _check_iban(self):
        if self.acc_type == 'iban':
            validate_iban(self.acc_number)
{'fraction_non_alphanumeric': 0.1228936170212766, 'fraction_numerical': 0.006808510638297872, 'mean_word_length': 3.109090909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5314375', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1807, 'n_words': 516}