Merge PR #491 into 16.0

Signed-off-by dreispt
This commit is contained in:
OCA-git-bot 2024-12-27 22:50:00 +00:00
commit 3903d91f1f
2 changed files with 27 additions and 11 deletions

View File

@ -15,10 +15,13 @@ class DocumentPageTag(models.Model):
("unique_name", "unique(name)", "Tags must be unique"), ("unique_name", "unique(name)", "Tags must be unique"),
] ]
@api.model @api.model_create_multi
def create(self, vals): def create(self, vals_list):
"""Be nice when trying to create duplicates""" """Be nice when trying to create duplicates"""
existing = self.search([("name", "=ilike", vals["name"])], limit=1) for vals in vals_list:
if existing: existing = self.search([("name", "=ilike", vals["name"])], limit=1)
return existing if existing:
return super().create(vals) vals_list.remove(vals)
if not vals_list:
return existing
return super().create(vals_list)

View File

@ -8,15 +8,28 @@ from odoo.tools.misc import mute_logger
class TestDocumentPageTag(TransactionCase): class TestDocumentPageTag(TransactionCase):
def test_document_page_tag(self): def test_document_page_tag(self):
testtag = self.env["document.page.tag"].name_create("test") test_tag = self.env["document.page.tag"].name_create("test")
# check we're charitable on duplicates # check we're charitable on duplicates
self.assertEqual( self.assertEqual(
testtag, test_tag,
self.env["document.page.tag"].name_create("Test"), self.env["document.page.tag"].name_create("Test"),
) )
# check multiple record creation
test_tag1 = self.env["document.page.tag"].create(
[{"name": "test1"}, {"name": "test"}, {"name": "test2"}]
)
self.assertEqual(len(test_tag1), 2)
self.assertEqual(test_tag1[0].name, "test1")
self.assertEqual(test_tag1[1].name, "test2")
# check single record creation where record already exist
test_tag2 = self.env["document.page.tag"].create([{"name": "test"}])
self.assertEqual(len(test_tag2), 1)
self.assertEqual(test_tag2[0].name, "test")
# check we can't create nonunique tags # check we can't create nonunique tags
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
with mute_logger("odoo.sql_db"): with mute_logger("odoo.sql_db"):
testtag2 = self.env["document.page.tag"].create({"name": "test2"}) test_tag3 = self.env["document.page.tag"].create([{"name": "test3"}])
testtag2.write({"name": "test"}) test_tag3.write({"name": "test"})
testtag2.flush_model() test_tag3.flush_model()