Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] utm: set counters from 0 #162229

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions addons/utm/models/utm_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import re
from collections import defaultdict
import itertools

from odoo import api, fields, models
from odoo.http import request
Expand Down Expand Up @@ -113,15 +114,14 @@ def _split_name_and_count(name):

# Count for each names, based on the names list given in argument
# and the record names in database
count_per_names = defaultdict(lambda: 0)
count_per_names.update({
name: max((
_split_name_and_count(existing_name)[1] + 1
counters_per_name = {
name: {
_split_name_and_count(existing_name)[1]
for existing_name in existing_names
if existing_name == name or existing_name.startswith(f'{name} [')
), default=1)
for name in names_without_counter
})
} for name in names_without_counter
}
count_per_names = defaultdict(lambda: itertools.count(1))

result = []
for name in names:
Expand All @@ -130,8 +130,10 @@ def _split_name_and_count(name):
continue

name_without_counter = _split_name_and_count(name)[0]
counter = count_per_names[name_without_counter]
result.append(f'{name_without_counter} [{counter}]' if counter > 1 else name)
count_per_names[name_without_counter] += 1
# keep going until the count is not already used
for count in count_per_names[name_without_counter]:
if count not in counters_per_name.get(name_without_counter, set()):
break
result.append(f'{name_without_counter} [{count}]' if count > 1 else name)

return result
13 changes: 11 additions & 2 deletions addons/utm/tests/test_utm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def test_find_or_create_record(self):
self.assertNotIn(source_4, source_1 | source_2)
self.assertEqual(source_4.name, 'Source 3')

# Create a new record when there are already duplicates
self.env['utm.source'].create({'name': 'Source 5 [2]'})
source_5 = self.env['utm.mixin']._find_or_create_record('utm.source', 'Source 5')
self.assertNotIn(source_5, source_1 | source_2 | source_4)
self.assertEqual(source_5.name, 'Source 5')

def test_name_generation(self):
"""Test that the name is always unique.

Expand Down Expand Up @@ -53,8 +59,11 @@ def test_name_generation(self):

(utm_0 | utm_3 | utm_4).unlink()

utm_6 = self.env[utm_model].create({'name': 'UTM dup'})
self.assertEqual(utm_6.name, 'UTM dup [5]')
utm_6 = self.env[utm_model].create([{'name': 'UTM dup'} for _ in range(4)])
self.assertListEqual(
utm_6.mapped('name'),
['UTM dup'] + [f'UTM dup [{counter}]' for counter in [2, 3, 5]],
'Duplicate counters should be filled in order of missing.')

utm_7 = self.env[utm_model].create({'name': 'UTM d'})
self.assertEqual(utm_7.name, 'UTM d', msg='Even if this name has the same prefix as the other, it is still unique')
Expand Down