diff --git a/document_page_reference/models/document_page.py b/document_page_reference/models/document_page.py index 3ee8d527..3c5460bc 100644 --- a/document_page_reference/models/document_page.py +++ b/document_page_reference/models/document_page.py @@ -7,6 +7,8 @@ from odoo import _, api, fields, models, tools from odoo.exceptions import ValidationError from odoo.tools.misc import html_escape +from odoo.addons.http_routing.models.ir_http import slugify + _logger = logging.getLogger(__name__) try: @@ -76,12 +78,19 @@ class DocumentPage(models.Model): for record in self: if not record.reference: continue - if not name_re.match(record.reference): - raise ValidationError(_("Reference is not valid")) - if self.search( - [("reference", "=", record.reference), ("id", "!=", record.id)] - ): - raise ValidationError(_("Reference must be unique")) + record._validate_reference(record=record) + + @api.model + def _validate_reference(self, record=None, reference=None): + if not reference: + reference = self.reference + if not name_re.match(reference): + raise ValidationError(_("Reference is not valid")) + uniq_domain = [("reference", "=", reference)] + if record: + uniq_domain += [("id", "!=", record.id)] + if self.search(uniq_domain): + raise ValidationError(_("Reference must be unique")) def _get_document(self, code): # Hook created in order to add check on other models @@ -125,3 +134,17 @@ class DocumentPage(models.Model): def get_raw_content(self): return self.with_context(raw_reference=True).get_content() + + @api.model + def create(self, vals): + if not vals.get("reference"): + # Propose a default reference + reference = slugify(vals.get("name")).replace("-", "_") + try: + self._validate_reference(reference=reference) + vals["reference"] = reference + except ValidationError: # pylint: disable=W7938 + # Do not fill reference. + pass + + return super(DocumentPage, self).create(vals) diff --git a/document_page_reference/tests/test_document_reference.py b/document_page_reference/tests/test_document_reference.py index ab972fe0..8c5d41f8 100644 --- a/document_page_reference/tests/test_document_reference.py +++ b/document_page_reference/tests/test_document_reference.py @@ -39,3 +39,19 @@ class TestDocumentReference(TransactionCase): def test_no_reference(self): self.page2.reference = "r3" self.assertRegex(self.page1.content_parsed, ".*r2.*") + + def test_auto_reference(self): + """Test if reference is proposed when saving a page without one.""" + self.assertEqual(self.page1.reference, "R1") + new_page = self.page_obj.create( + {"name": "Test Page with no rEfErenCe", "content": "some content"} + ) + self.assertEqual(new_page.reference, "test_page_with_no_reference") + new_page_duplicated_name = self.page_obj.create( + { + "name": "test page with no reference", + "content": "this should have an empty reference " + "because reference must be unique", + } + ) + self.assertFalse(new_page_duplicated_name.reference) diff --git a/document_page_reference/views/document_page.xml b/document_page_reference/views/document_page.xml index 30606b1d..a9459fcc 100644 --- a/document_page_reference/views/document_page.xml +++ b/document_page_reference/views/document_page.xml @@ -9,7 +9,10 @@

- +

@@ -56,8 +59,8 @@ document.page - - + +