Skip to content

Commit

Permalink
Merge PR #1790 into 14.0
Browse files Browse the repository at this point in the history
Signed-off-by dreispt
  • Loading branch information
OCA-git-bot committed Sep 23, 2024
2 parents 021ee48 + 62cfa0e commit 8d3e415
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
20 changes: 14 additions & 6 deletions partner_helper/models/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Author: Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models
from odoo import _, models
from odoo.exceptions import UserError


def split_char(char, output_number, size):
words = char.split(" ")
def split_char(char, output_number, size, strict=False):
words = char.strip().split(" ")
result = []
word = words.pop(0)
for index in range(0, output_number):
Expand All @@ -19,13 +20,18 @@ def split_char(char, output_number, size):
else:
result[index] += " %s" % word
word = ""
if words and strict:
raise UserError(
_("The address is too long following word can not be processed '%s'")
% " ".join(words)
)
return result


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

def _get_split_address(self, output_number, max_size):
def _get_split_address(self, output_number, max_size, strict=False):
"""This method allows to get a number of street fields according to
your choice. Default is 2 large fields in Odoo (128 chars).
In some countries you may use 3 or 4 shorter street fields.
Expand All @@ -43,6 +49,8 @@ def _get_split_address(self, output_number, max_size):
result[1] = street2
return result
elif len(street) <= max_size:
return [street] + split_char(street2, output_number - 1, max_size)
return [street] + split_char(street2, output_number - 1, max_size, strict)
else:
return split_char("{} {}".format(street, street2), output_number, max_size)
return split_char(
"{} {}".format(street, street2), output_number, max_size, strict
)
17 changes: 14 additions & 3 deletions partner_helper/tests/test_split_address.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

from odoo.exceptions import UserError
from odoo.tests.common import TransactionCase

_logger = logging.getLogger(__name__)
Expand All @@ -10,21 +11,31 @@ def setUp(self):
super(TestSplit, self).setUp()
self.partnerX = self.env.ref("base.res_partner_12")
self.partnerX.street = (
"278 route pitoresque de la vallee de" " l'ours qui fuit les chasseurs"
"278 route pitoresque de la vallee de l'ours qui fuit les chasseurs "
"en courant très très vite"
)

def test_split1(self):
address1, address2 = self.partnerX._get_split_address(2, 40)
self.assertEqual("278 route pitoresque de la vallee de", address1)
self.assertEqual("l'ours qui fuit les chasseurs ", address2)
self.assertEqual("l'ours qui fuit les chasseurs en courant", address2)
self.assertTrue(len(address1) <= 40)
self.assertTrue(len(address2) <= 40)

def test_split2(self):
address1, address2, address3 = self.partnerX._get_split_address(3, 25)
self.assertEqual("278 route pitoresque de", address1)
self.assertEqual("la vallee de l'ours qui", address2)
self.assertEqual("fuit les chasseurs ", address3)
self.assertEqual("fuit les chasseurs en", address3)
self.assertTrue(len(address1) <= 25)
self.assertTrue(len(address2) <= 25)
self.assertTrue(len(address3) <= 25)

def test_split3(self):
with self.assertRaises(UserError) as e:
self.partnerX._get_split_address(3, 15, strict=True)
self.assertEqual(
e.exception.name,
"The address is too long following word can not be "
"processed 'qui fuit les chasseurs en courant très très vite'",
)

0 comments on commit 8d3e415

Please sign in to comment.