mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-13 07:24:48 -06:00
[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:
parent
8cef3f268b
commit
9eb3d42dad
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -9,7 +9,10 @@
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//h1" position="before">
|
||||
<h2>
|
||||
<field name="reference" placeholder="internal_reference" />
|
||||
<field
|
||||
name="reference"
|
||||
placeholder="internal_reference (autofilled if not value is provided)"
|
||||
/>
|
||||
</h2>
|
||||
</xpath>
|
||||
<field name="content" position="attributes">
|
||||
@ -56,8 +59,8 @@
|
||||
<field name="model">document.page</field>
|
||||
<field name="inherit_id" ref="document_page.view_wiki_tree" />
|
||||
<field name="arch" type="xml">
|
||||
<field name="name" position="before">
|
||||
<field name="reference" />
|
||||
<field name="name" position="after">
|
||||
<field name="reference" optional="hidden" />
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
Loading…
Reference in New Issue
Block a user