[IMP] document_page_reference: auto-fill reference when not supplied.

also, make reference optional in tree view and not the first field.
This commit is contained in:
Lois Rilo 2022-11-16 18:15:26 +01:00 committed by Anusha
parent 8cef3f268b
commit 9eb3d42dad
3 changed files with 51 additions and 9 deletions

View File

@ -7,6 +7,8 @@ from odoo import _, api, fields, models, tools
from odoo.exceptions import ValidationError from odoo.exceptions import ValidationError
from odoo.tools.misc import html_escape from odoo.tools.misc import html_escape
from odoo.addons.http_routing.models.ir_http import slugify
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
try: try:
@ -76,12 +78,19 @@ class DocumentPage(models.Model):
for record in self: for record in self:
if not record.reference: if not record.reference:
continue continue
if not name_re.match(record.reference): record._validate_reference(record=record)
raise ValidationError(_("Reference is not valid"))
if self.search( @api.model
[("reference", "=", record.reference), ("id", "!=", record.id)] def _validate_reference(self, record=None, reference=None):
): if not reference:
raise ValidationError(_("Reference must be unique")) 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): def _get_document(self, code):
# Hook created in order to add check on other models # Hook created in order to add check on other models
@ -125,3 +134,17 @@ class DocumentPage(models.Model):
def get_raw_content(self): def get_raw_content(self):
return self.with_context(raw_reference=True).get_content() 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)

View File

@ -39,3 +39,19 @@ class TestDocumentReference(TransactionCase):
def test_no_reference(self): def test_no_reference(self):
self.page2.reference = "r3" self.page2.reference = "r3"
self.assertRegex(self.page1.content_parsed, ".*r2.*") 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)

View File

@ -9,7 +9,10 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<xpath expr="//h1" position="before"> <xpath expr="//h1" position="before">
<h2> <h2>
<field name="reference" placeholder="internal_reference" /> <field
name="reference"
placeholder="internal_reference (autofilled if not value is provided)"
/>
</h2> </h2>
</xpath> </xpath>
<field name="content" position="attributes"> <field name="content" position="attributes">
@ -56,8 +59,8 @@
<field name="model">document.page</field> <field name="model">document.page</field>
<field name="inherit_id" ref="document_page.view_wiki_tree" /> <field name="inherit_id" ref="document_page.view_wiki_tree" />
<field name="arch" type="xml"> <field name="arch" type="xml">
<field name="name" position="before"> <field name="name" position="after">
<field name="reference" /> <field name="reference" optional="hidden" />
</field> </field>
</field> </field>
</record> </record>