1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp.osv import fields, osv
23 from openerp.tools.translate import _
25 from openerp import tools
27 class document_page(osv.osv):
28 _name = "document.page"
29 _description = "Document Page"
32 def _get_page_index(self, cr, uid, page, link=True):
34 for subpage in page.child_ids:
35 index += ["<li>"+ self._get_page_index(cr, uid, subpage) +"</li>"]
38 r = '<a href="#id=%s">%s</a>'%(page.id,page.name)
40 r += "<ul>" + "".join(index) + "</ul>"
43 def _get_display_content(self, cr, uid, ids, name, args, context=None):
45 for page in self.browse(cr, uid, ids, context=context):
46 if page.type == "category":
47 content = self._get_page_index(cr, uid, page, link=False)
49 content = page.content
50 res[page.id] = content
54 'name': fields.char('Title', required=True),
55 'type':fields.selection([('content','Content'), ('category','Category')], 'Type', help="Page type"),
57 'parent_id': fields.many2one('document.page', 'Category', domain=[('type','=','category')]),
58 'child_ids': fields.one2many('document.page', 'parent_id', 'Children'),
60 'content': fields.text("Content"),
61 'display_content': fields.function(_get_display_content, string='Displayed Content', type='text'),
63 'history_ids': fields.one2many('document.page.history', 'page_id', 'History'),
64 'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
66 'create_date': fields.datetime("Created on", select=True, readonly=True),
67 'create_uid': fields.many2one('res.users', 'Author', select=True, readonly=True),
68 'write_date': fields.datetime("Modification Date", select=True, readonly=True),
69 'write_uid': fields.many2one('res.users', "Last Contributor", select=True),
75 def onchange_parent_id(self, cr, uid, ids, parent_id, content, context=None):
77 if parent_id and not content:
78 parent = self.browse(cr, uid, parent_id, context=context)
79 if parent.type == "category":
81 'content': parent.content,
85 def create_history(self, cr, uid, ids, vals, context=None):
87 history = self.pool.get('document.page.history')
88 if vals.get('content'):
90 'content': vals.get('content', ''),
93 history.create(cr, uid, res)
95 def create(self, cr, uid, vals, context=None):
96 page_id = super(document_page, self).create(cr, uid, vals, context)
97 self.create_history(cr, uid, [page_id], vals, context)
100 def write(self, cr, uid, ids, vals, context=None):
101 result = super(document_page, self).write(cr, uid, ids, vals, context)
102 self.create_history(cr, uid, ids, vals, context)
105 class document_page_history(osv.osv):
106 _name = "document.page.history"
107 _description = "Document Page History"
109 _rec_name = "create_date"
112 'page_id': fields.many2one('document.page', 'Page'),
113 'summary': fields.char('Summary', size=256, select=True),
114 'content': fields.text("Content"),
115 'create_date': fields.datetime("Date"),
116 'create_uid': fields.many2one('res.users', "Modified By"),
119 def getDiff(self, cr, uid, v1, v2, context=None):
120 history_pool = self.pool.get('document.page.history')
121 text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
122 text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
125 line1 = text1.splitlines(1)
127 line2 = text2.splitlines(1)
128 if (not line1 and not line2) or (line1 == line2):
129 raise osv.except_osv(_('Warning!'), _('There are no changes in revisions.'))
130 diff = difflib.HtmlDiff()
131 return diff.make_table(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=True)
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: