mirror of
https://github.com/OCA/knowledge.git
synced 2025-12-19 03:42:19 -06:00
[IMP][10.0][document_page] Change Requests and workflow improvements on documents (#155)
This commit is contained in:
committed by
FernandoRomera
parent
b862c87e8c
commit
47320de3a2
@@ -2,13 +2,10 @@
|
||||
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
import logging
|
||||
import difflib
|
||||
from odoo import fields, models
|
||||
from odoo import api, fields, models
|
||||
from odoo.tools.translate import _
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DocumentPageHistory(models.Model):
|
||||
"""This model is necessary to manage a document history."""
|
||||
@@ -16,24 +13,40 @@ class DocumentPageHistory(models.Model):
|
||||
_name = "document.page.history"
|
||||
_description = "Document Page History"
|
||||
_order = 'id DESC'
|
||||
_rec_name = "create_date"
|
||||
|
||||
page_id = fields.Many2one('document.page', 'Page')
|
||||
page_id = fields.Many2one('document.page', 'Page', ondelete='cascade')
|
||||
summary = fields.Char('Summary', index=True)
|
||||
content = fields.Text("Content")
|
||||
create_date = fields.Datetime("Date")
|
||||
create_uid = fields.Many2one('res.users', "Modified By")
|
||||
diff = fields.Text(compute='_compute_diff')
|
||||
|
||||
@api.multi
|
||||
@api.depends('content', 'page_id.history_ids')
|
||||
def _compute_diff(self):
|
||||
"""Shows a diff between this version and the previous version"""
|
||||
history = self.env['document.page.history']
|
||||
for rec in self:
|
||||
prev = history.search([
|
||||
('page_id', '=', rec.page_id.id),
|
||||
('create_date', '<', rec.create_date)],
|
||||
limit=1,
|
||||
order='create_date DESC')
|
||||
if prev:
|
||||
rec.diff = self.getDiff(prev.id, rec.id)
|
||||
else:
|
||||
rec.diff = self.getDiff(False, rec.id)
|
||||
|
||||
@api.model
|
||||
def getDiff(self, v1, v2):
|
||||
"""Return the difference between two version of document version."""
|
||||
text1 = self.browse(v1).content
|
||||
text2 = self.browse(v2).content
|
||||
line1 = line2 = ''
|
||||
if text1:
|
||||
line1 = text1.splitlines(1)
|
||||
if text2:
|
||||
line2 = text2.splitlines(1)
|
||||
if (not line1 and not line2) or (line1 == line2):
|
||||
text1 = v1 and self.browse(v1).content or ''
|
||||
text2 = v2 and self.browse(v2).content or ''
|
||||
# Include line breaks to make it more readable
|
||||
# TODO: consider using a beautify library directly on the content
|
||||
text1 = text1.replace('</p><p>', '</p>\r\n<p>')
|
||||
text2 = text2.replace('</p><p>', '</p>\r\n<p>')
|
||||
line1 = text1.splitlines(1)
|
||||
line2 = text2.splitlines(1)
|
||||
if line1 == line2:
|
||||
return _('There are no changes in revisions.')
|
||||
else:
|
||||
diff = difflib.HtmlDiff()
|
||||
@@ -43,3 +56,11 @@ class DocumentPageHistory(models.Model):
|
||||
"Revision-{}".format(v2),
|
||||
context=True
|
||||
)
|
||||
|
||||
@api.multi
|
||||
def name_get(self):
|
||||
result = []
|
||||
for rec in self:
|
||||
name = "%s #%i" % (rec.page_id.name, rec.id)
|
||||
result.append((rec.id, name))
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user