mirror of
https://github.com/OCA/knowledge.git
synced 2025-12-19 11:52:18 -06:00
[IMP][10.0][document_page] Change Requests and workflow improvements on documents (#155)
This commit is contained in:
committed by
Justine Doutreloux
parent
fe6ebff289
commit
4e446a5f34
@@ -2,11 +2,8 @@
|
||||
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
import logging
|
||||
from odoo import api, fields, models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DocumentPage(models.Model):
|
||||
"""This class is use to manage Document."""
|
||||
@@ -37,17 +34,48 @@ class DocumentPage(models.Model):
|
||||
'Children'
|
||||
)
|
||||
|
||||
content = fields.Text("Content")
|
||||
content = fields.Text(
|
||||
"Content",
|
||||
compute='_compute_content',
|
||||
inverse='_inverse_content',
|
||||
search='_search_content',
|
||||
required=True,
|
||||
)
|
||||
|
||||
# no-op computed field
|
||||
summary = fields.Char(
|
||||
help='Describe the changes made',
|
||||
compute=lambda x: x,
|
||||
inverse=lambda x: x,
|
||||
)
|
||||
|
||||
template = fields.Html(
|
||||
"Template",
|
||||
help="Template that will be used as a content template "
|
||||
"for all new page of this category.",
|
||||
)
|
||||
|
||||
# deprecated - should be removed on 11.0
|
||||
# left here because some modules might still need it
|
||||
display_content = fields.Text(
|
||||
string='Displayed Content',
|
||||
compute='_get_display_content'
|
||||
compute='_compute_display_content'
|
||||
)
|
||||
|
||||
history_head = fields.Many2one(
|
||||
'document.page.history',
|
||||
'HEAD',
|
||||
compute='_compute_history_head',
|
||||
store=True,
|
||||
auto_join=True,
|
||||
)
|
||||
|
||||
history_ids = fields.One2many(
|
||||
'document.page.history',
|
||||
'page_id',
|
||||
'History'
|
||||
'History',
|
||||
order='create_date DESC',
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
menu_id = fields.Many2one(
|
||||
@@ -56,82 +84,88 @@ class DocumentPage(models.Model):
|
||||
readonly=True
|
||||
)
|
||||
|
||||
create_date = fields.Datetime(
|
||||
"Created on",
|
||||
readonly=True
|
||||
content_date = fields.Datetime(
|
||||
'Last Contribution Date',
|
||||
related='history_head.create_date',
|
||||
store=True,
|
||||
index=True,
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
create_uid = fields.Many2one(
|
||||
content_uid = fields.Many2one(
|
||||
'res.users',
|
||||
'Author',
|
||||
readonly=True
|
||||
'Last Contributor',
|
||||
related='history_head.create_uid',
|
||||
store=True,
|
||||
index=True,
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
write_date = fields.Datetime(
|
||||
"Modification Date",
|
||||
readonly=True
|
||||
)
|
||||
|
||||
write_uid = fields.Many2one(
|
||||
'res.users',
|
||||
"Last Contributor",
|
||||
readonly=True
|
||||
)
|
||||
|
||||
def _get_page_index(self, page, link=True):
|
||||
@api.multi
|
||||
def _get_page_index(self, link=True):
|
||||
"""Return the index of a document."""
|
||||
self.ensure_one()
|
||||
index = []
|
||||
for subpage in page.child_ids:
|
||||
index += ["<li>" + self._get_page_index(subpage) +
|
||||
"</li>"]
|
||||
for subpage in self.child_ids:
|
||||
index += ["<li>" + subpage._get_page_index() + "</li>"]
|
||||
r = ''
|
||||
if link:
|
||||
r = '<a href="#id=%s">%s</a>' % (page.id, page.name)
|
||||
r = '<a href="#id=%s">%s</a>' % (self.id, self.name)
|
||||
|
||||
if index:
|
||||
r += "<ul>" + "".join(index) + "</ul>"
|
||||
return r
|
||||
|
||||
def _get_display_content(self):
|
||||
"""Return the content of a document."""
|
||||
for page in self:
|
||||
if page.type == "category":
|
||||
display_content = self._get_page_index(page, link=False)
|
||||
else:
|
||||
display_content = page.content
|
||||
page.display_content = display_content
|
||||
|
||||
@api.onchange("parent_id")
|
||||
def do_set_content(self):
|
||||
"""We Set it the right content to the new parent."""
|
||||
if self.parent_id and not self.content:
|
||||
if self.parent_id.type == "category":
|
||||
self.content = self.parent_id.content
|
||||
|
||||
def create_history(self, page_id, content):
|
||||
"""Create the first history of a newly created document."""
|
||||
history = self.env['document.page.history']
|
||||
return history.create({
|
||||
"content": content,
|
||||
"page_id": page_id
|
||||
})
|
||||
@api.multi
|
||||
@api.depends('content')
|
||||
def _compute_display_content(self):
|
||||
# @deprecated, simply use content
|
||||
for rec in self:
|
||||
rec.display_content = rec.content
|
||||
|
||||
@api.multi
|
||||
def write(self, vals):
|
||||
"""Write the content and set the history."""
|
||||
result = super(DocumentPage, self).write(vals)
|
||||
content = vals.get('content')
|
||||
if content:
|
||||
for page in self:
|
||||
self.create_history(page.id, content)
|
||||
return result
|
||||
@api.depends('history_head', 'history_ids')
|
||||
def _compute_content(self):
|
||||
for rec in self:
|
||||
if rec.type == 'category':
|
||||
rec.content = rec._get_page_index(link=False)
|
||||
else:
|
||||
if rec.history_head:
|
||||
rec.content = rec.history_head.content
|
||||
else:
|
||||
# html widget's default, so it doesn't trigger ghost save
|
||||
rec.content = '<p><br></p>'
|
||||
|
||||
@api.model
|
||||
@api.returns('self', lambda value: value.id)
|
||||
def create(self, vals):
|
||||
"""Create the first history of a document."""
|
||||
page_id = super(DocumentPage, self).create(vals)
|
||||
content = vals.get('content')
|
||||
if content:
|
||||
self.create_history(page_id.id, content)
|
||||
return page_id
|
||||
@api.multi
|
||||
def _inverse_content(self):
|
||||
for rec in self:
|
||||
if rec.type == 'content':
|
||||
rec._create_history({
|
||||
'content': rec.content,
|
||||
'summary': rec.summary,
|
||||
})
|
||||
|
||||
@api.multi
|
||||
def _search_content(self, operator, value):
|
||||
return [('history_head.content', operator, value)]
|
||||
|
||||
@api.multi
|
||||
@api.depends('history_ids')
|
||||
def _compute_history_head(self):
|
||||
for rec in self:
|
||||
if rec.history_ids:
|
||||
rec.history_head = rec.history_ids[0]
|
||||
|
||||
@api.multi
|
||||
def _create_history(self, vals):
|
||||
self.ensure_one()
|
||||
history = self.env['document.page.history']
|
||||
vals['page_id'] = self.id
|
||||
return history.create(vals)
|
||||
|
||||
@api.onchange("parent_id")
|
||||
def _onchange_parent_id(self):
|
||||
"""We Set it the right content to the new parent."""
|
||||
if not self.content or self.content == '<p><br></p>':
|
||||
if self.parent_id and self.parent_id.type == "category":
|
||||
self.content = self.parent_id.template
|
||||
|
||||
Reference in New Issue
Block a user