Skip to content

Commit

Permalink
[16.0][FIX] document_page_tag: Use api.model_create_multi
Browse files Browse the repository at this point in the history
   Using decorator api.model_create_multi for create
method in document_page_tag model
  • Loading branch information
anusriNPS committed Jul 31, 2024
1 parent b1ef710 commit e6c793d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
15 changes: 9 additions & 6 deletions document_page_tag/models/document_page_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ class DocumentPageTag(models.Model):
("unique_name", "unique(name)", "Tags must be unique"),
]

@api.model
def create(self, vals):
@api.model_create_multi
def create(self, vals_list):
"""Be nice when trying to create duplicates"""
existing = self.search([("name", "=ilike", vals["name"])], limit=1)
if existing:
return existing
return super().create(vals)
for vals in vals_list:
existing = self.search([("name", "=ilike", vals["name"])], limit=1)
if existing:
vals_list.remove(vals)
if not vals_list:
return existing
return super().create(vals_list)
18 changes: 15 additions & 3 deletions document_page_tag/tests/test_document_page_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,21 @@ def test_document_page_tag(self):
testtag,
self.env["document.page.tag"].name_create("Test"),
)

# check multiple record creation
testtag1 = self.env["document.page.tag"].create(
[{"name": "test1"}, {"name": "test"}, {"name": "test2"}]
)
self.assertEqual(len(testtag1), 2)
self.assertEqual(testtag1[0].name, "test1")
self.assertEqual(testtag1[1].name, "test2")
testtag2 = self.env["document.page.tag"].create([{"name": "test"}])
self.assertEqual(len(testtag2), 1)
self.assertEqual(testtag2[0].name, "test")

# check we can't create nonunique tags
with self.assertRaises(IntegrityError):
with mute_logger("odoo.sql_db"):
testtag2 = self.env["document.page.tag"].create({"name": "test2"})
testtag2.write({"name": "test"})
testtag2.flush_model()
testtag3 = self.env["document.page.tag"].create([{"name": "test3"}])
testtag3.write({"name": "test"})
testtag3.flush_model()

0 comments on commit e6c793d

Please sign in to comment.