mirror of
https://github.com/OCA/knowledge.git
synced 2025-12-19 11:52:18 -06:00
use new API
This commit is contained in:
committed by
Dũng (Trần Đình)
parent
1561edddb8
commit
9aa5a1a9d4
@@ -18,126 +18,162 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
from openerp.osv import fields, osv
|
||||
from openerp.tools.translate import _
|
||||
import difflib
|
||||
from openerp import models, fields, api, _
|
||||
|
||||
|
||||
class document_page(osv.osv):
|
||||
class document_page(models.Model):
|
||||
_name = "document.page"
|
||||
_description = "Document Page"
|
||||
_order = 'name'
|
||||
|
||||
def _get_page_index(self, cr, uid, page, link=True):
|
||||
name = fields.Char('Title', required=True)
|
||||
|
||||
type = fields.Selection(
|
||||
[('content', 'Content'), ('category', 'Category')],
|
||||
'Type',
|
||||
help="Page type",
|
||||
default="content"
|
||||
)
|
||||
|
||||
parent_id = fields.Many2one(
|
||||
'document.page',
|
||||
'Category',
|
||||
domain=[('type', '=', 'category')]
|
||||
)
|
||||
|
||||
child_ids = fields.One2many(
|
||||
'document.page',
|
||||
'parent_id',
|
||||
'Children'
|
||||
)
|
||||
|
||||
content = fields.Text(
|
||||
"Content"
|
||||
)
|
||||
|
||||
display_content = fields.Text(
|
||||
string='Displayed Content',
|
||||
compute='_get_display_content'
|
||||
)
|
||||
|
||||
history_ids = fields.One2many(
|
||||
'document.page.history',
|
||||
'page_id',
|
||||
'History'
|
||||
)
|
||||
|
||||
menu_id = fields.Many2one(
|
||||
'ir.ui.menu',
|
||||
"Menu",
|
||||
readonly=True
|
||||
)
|
||||
|
||||
create_date = fields.Datetime(
|
||||
"Created on",
|
||||
select=True,
|
||||
readonly=True
|
||||
)
|
||||
|
||||
create_uid = fields.Many2one(
|
||||
'res.users',
|
||||
'Author',
|
||||
select=True,
|
||||
readonly=True
|
||||
)
|
||||
|
||||
write_date = fields.Datetime(
|
||||
"Modification Date",
|
||||
select=True,
|
||||
readonly=True)
|
||||
|
||||
write_uid = fields.Many2one(
|
||||
'res.users',
|
||||
"Last Contributor",
|
||||
select=True,
|
||||
readonly=True
|
||||
)
|
||||
|
||||
def _get_page_index(self, page, link=True):
|
||||
index = []
|
||||
for subpage in page.child_ids:
|
||||
index += ["<li>" + self._get_page_index(cr, uid, subpage) +
|
||||
index += ["<li>" + self._get_page_index(subpage) +
|
||||
"</li>"]
|
||||
r = ''
|
||||
if link:
|
||||
r = '<a href="#id=%s">%s</a>' % (page.id, page.name)
|
||||
|
||||
if index:
|
||||
r += "<ul>" + "".join(index) + "</ul>"
|
||||
return r
|
||||
|
||||
def _get_display_content(self, cr, uid, ids, name, args, context=None):
|
||||
res = {}
|
||||
for page in self.browse(cr, uid, ids, context=context):
|
||||
def _get_display_content(self):
|
||||
for page in self:
|
||||
if page.type == "category":
|
||||
content = self._get_page_index(cr, uid, page, link=False)
|
||||
display_content = self._get_page_index(page, link=False)
|
||||
else:
|
||||
content = page.content
|
||||
res[page.id] = content
|
||||
return res
|
||||
display_content = page.content
|
||||
page.display_content = display_content
|
||||
|
||||
_columns = {
|
||||
'name': fields.char('Title', required=True),
|
||||
'type': fields.selection([('content', 'Content'),
|
||||
('category', 'Category')],
|
||||
'Type', help="Page type"),
|
||||
'parent_id': fields.many2one('document.page', 'Category',
|
||||
domain=[('type', '=', 'category')]),
|
||||
'child_ids': fields.one2many('document.page', 'parent_id', 'Children'),
|
||||
'content': fields.text("Content"),
|
||||
'display_content': fields.function(_get_display_content,
|
||||
string='Displayed Content',
|
||||
type='text'),
|
||||
'history_ids': fields.one2many('document.page.history', 'page_id',
|
||||
'History'),
|
||||
'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
|
||||
@api.onchange("parent_id")
|
||||
def do_set_content(self):
|
||||
if self.parent_id and not self.content:
|
||||
if self.parent_id.type == "category":
|
||||
self.content = self.parent_id.content
|
||||
|
||||
'create_date': fields.datetime("Created on", select=True,
|
||||
readonly=True),
|
||||
'create_uid': fields.many2one('res.users', 'Author', select=True,
|
||||
readonly=True),
|
||||
'write_date': fields.datetime("Modification Date", select=True,
|
||||
readonly=True),
|
||||
'write_uid': fields.many2one('res.users', "Last Contributor",
|
||||
select=True, readonly=True),
|
||||
}
|
||||
_defaults = {
|
||||
'type': 'content',
|
||||
}
|
||||
def create_history(self, page_id, content):
|
||||
history = self.env['document.page.history']
|
||||
return history.create({
|
||||
"content": content,
|
||||
"page_id": page_id
|
||||
})
|
||||
|
||||
def onchange_parent_id(self, cr, uid, ids, parent_id, content,
|
||||
context=None):
|
||||
res = {}
|
||||
if parent_id and not content:
|
||||
parent = self.browse(cr, uid, parent_id, context=context)
|
||||
if parent.type == "category":
|
||||
res['value'] = {
|
||||
'content': parent.content,
|
||||
}
|
||||
return res
|
||||
|
||||
def create_history(self, cr, uid, ids, vals, context=None):
|
||||
for i in ids:
|
||||
history = self.pool.get('document.page.history')
|
||||
if vals.get('content'):
|
||||
res = {
|
||||
'content': vals.get('content', ''),
|
||||
'page_id': i,
|
||||
}
|
||||
history.create(cr, uid, res)
|
||||
|
||||
def create(self, cr, uid, vals, context=None):
|
||||
page_id = super(document_page, self).create(cr, uid, vals, context)
|
||||
self.create_history(cr, uid, [page_id], vals, context)
|
||||
return page_id
|
||||
|
||||
def write(self, cr, uid, ids, vals, context=None):
|
||||
result = super(document_page, self).write(cr, uid, ids, vals, context)
|
||||
self.create_history(cr, uid, ids, vals, context)
|
||||
@api.multi
|
||||
def write(self, vals):
|
||||
result = super(document_page, self).write(vals)
|
||||
content = vals.get('content')
|
||||
if content:
|
||||
for page in self:
|
||||
self.create_history(page.id, content)
|
||||
return result
|
||||
|
||||
@api.model
|
||||
@api.returns('self', lambda value: value.id)
|
||||
def create(self, vals):
|
||||
page_id = super(document_page, self).create(vals)
|
||||
content = vals.get('content')
|
||||
if content:
|
||||
self.create_history(page_id.id, content)
|
||||
return page_id
|
||||
|
||||
class document_page_history(osv.osv):
|
||||
|
||||
class document_page_history(models.Model):
|
||||
_name = "document.page.history"
|
||||
_description = "Document Page History"
|
||||
_order = 'id DESC'
|
||||
_rec_name = "create_date"
|
||||
|
||||
_columns = {
|
||||
'page_id': fields.many2one('document.page', 'Page'),
|
||||
'summary': fields.char('Summary', size=256, select=True),
|
||||
'content': fields.text("Content"),
|
||||
'create_date': fields.datetime("Date"),
|
||||
'create_uid': fields.many2one('res.users', "Modified By"),
|
||||
}
|
||||
page_id = fields.Many2one('document.page', 'Page')
|
||||
summary = fields.Char('Summary', size=256, select=True)
|
||||
content = fields.Text("Content")
|
||||
create_date = fields.Datetime("Date")
|
||||
create_uid = fields.Many2one('res.users', "Modified By")
|
||||
|
||||
def getDiff(self, cr, uid, v1, v2, context=None):
|
||||
history_pool = self.pool.get('document.page.history')
|
||||
text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
|
||||
text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
|
||||
def getDiff(self, v1, v2):
|
||||
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):
|
||||
raise osv.except_osv(_('Warning!'),
|
||||
_('There are no changes in revisions.'))
|
||||
diff = difflib.HtmlDiff()
|
||||
return diff.make_table(line1, line2, "Revision-%s" % (v1),
|
||||
"Revision-%s" % (v2), context=True)
|
||||
return _('There are no changes in revisions.')
|
||||
else:
|
||||
diff = difflib.HtmlDiff()
|
||||
return diff.make_table(
|
||||
line1, line2,
|
||||
"Revision-{}".format(v1),
|
||||
"Revision-{}".format(v2),
|
||||
context=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user