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 osv import fields, osv
23 from tools.translate import _
27 class document_page(osv.osv):
28 _name = "document.page"
29 _description = "Document Page"
32 def _get_page_index(self, cr, uid, page):
34 for subpage in page.child_ids:
35 index += ["<li>"+ self._get_page_index(cr, uid, subpage) +"</li>"]
36 r = '<a href="#id=%s">%s</a>'%(page.id,page.name)
38 r += "<ul>" + "".join(index) + "</ul>"
41 def _get_display_content(self, cr, uid, ids, name, args, context=None):
43 for page in self.browse(cr, uid, ids, context=context):
44 if page.type == "category":
45 content = self._get_page_index(cr, uid, page)
47 content = page.content
48 res[page.id] = content
52 'name': fields.char('Title', required=True),
53 'type':fields.selection([('content','Content'), ('category','Category')], 'Type', help="Page type"),
55 'parent_id': fields.many2one('document.page', 'Category', domain=[('type','=','category')]),
56 'child_ids': fields.one2many('document.page', 'parent_id', 'Children'),
58 'content': fields.text("Content"),
59 'display_content': fields.function(_get_display_content, string='Displayed Content', type='text'),
61 'history_ids': fields.one2many('document.page.history', 'page_id', 'History'),
62 'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
64 'create_date': fields.datetime("Created on", select=True, readonly=True),
65 'create_uid': fields.many2one('res.users', 'Author', select=True, readonly=True),
66 'write_date': fields.datetime("Modification Date", select=True, readonly=True),
67 'write_uid': fields.many2one('res.users', "Last Contributor", select=True),
73 def onchange_parent_id(self, cr, uid, ids, parent_id, content, context=None):
75 if parent_id and not content:
76 parent = self.browse(cr, uid, parent_id, context=context)
77 if parent.type == "category":
79 'content': parent.content,
83 def create_history(self, cr, uid, ids, vals, context=None):
85 history = self.pool.get('document.page.history')
86 if vals.get('content'):
88 'content': vals.get('content', ''),
91 history.create(cr, uid, res)
93 def create(self, cr, uid, vals, context=None):
94 page_id = super(document_page, self).create(cr, uid, vals, context)
95 self.create_history(cr, uid, [page_id], vals, context)
98 def write(self, cr, uid, ids, vals, context=None):
99 result = super(document_page, self).write(cr, uid, ids, vals, context)
100 self.create_history(cr, uid, ids, vals, context)
103 class document_page_history(osv.osv):
104 _name = "document.page.history"
105 _description = "Document Page History"
107 _rec_name = "create_date"
110 'page_id': fields.many2one('document.page', 'Page'),
111 'summary': fields.char('Summary', size=256, select=True),
112 'content': fields.text("Content"),
113 'create_date': fields.datetime("Date"),
114 'create_uid': fields.many2one('res.users', "Modified By"),
117 def getDiff(self, cr, uid, v1, v2, context=None):
118 history_pool = self.pool.get('document.page.history')
119 text1 = history_pool.read(cr, uid, [v1], ['content'])[0]['content']
120 text2 = history_pool.read(cr, uid, [v2], ['content'])[0]['content']
123 line1 = tools.ustr(text1.splitlines(1))
125 line2=tools.ustr(text2.splitlines(1))
126 if (not line1 and not line2) or (line1 == line2):
127 raise osv.except_osv(_('Warning!'), _('There are no changes in revisions.'))
128 diff = difflib.HtmlDiff()
129 return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: