[MERGE] latest trunk
[odoo/odoo.git] / addons / document_page / document_page.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23 from tools.translate import _
24 import difflib
25 import tools
26
27 class document_page(osv.osv):
28     _name = "document.page"
29     _description = "Document Page"
30     _order = 'name'
31
32     def _get_page_index(self, cr, uid, page):
33         index = []
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)
37         if index:
38             r += "<ul>" + "".join(index) + "</ul>"
39         return r
40
41     def _get_display_content(self, cr, uid, ids, name, args, context=None):
42         res = {}
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)
46             else:
47                content = page.content
48             res[page.id] =  content
49         return res
50
51     _columns = {
52         'name': fields.char('Title', required=True),
53         'type':fields.selection([('content','Content'), ('category','Category')], 'Type', help="Page type"), 
54
55         'parent_id': fields.many2one('document.page', 'Category', domain=[('type','=','category')]),
56         'child_ids': fields.one2many('document.page', 'parent_id', 'Children'),
57
58         'content': fields.text("Content"),
59         'display_content': fields.function(_get_display_content, string='Displayed Content', type='text'),
60
61         'history_ids': fields.one2many('document.page.history', 'page_id', 'History'),
62         'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
63
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),
68     }
69     _defaults = {
70         'type':'content',
71     }
72
73     def onchange_parent_id(self, cr, uid, ids, parent_id, content, context=None):
74         res = {}
75         if parent_id and not content:
76             parent = self.browse(cr, uid, parent_id, context=context)
77             if parent.type == "category":
78                 res['value'] = {
79                     'content': parent.content,
80                 }
81         return res
82
83     def create_history(self, cr, uid, ids, vals, context=None):
84         for i in ids:
85             history = self.pool.get('document.page.history')
86             if vals.get('content'):
87                 res = {
88                     'content': vals.get('content', ''),
89                     'page_id': i,
90                 }
91                 history.create(cr, uid, res)
92
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)
96         return page_id
97
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)
101         return result
102
103 class document_page_history(osv.osv):
104     _name = "document.page.history"
105     _description = "Document Page History"
106     _order = 'id DESC'
107     _rec_name = "create_date"
108
109     _columns = {
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"),
115     }
116
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']
121         line1 = line2 = ''
122         if text1:
123             line1 = tools.ustr(text1.splitlines(1))
124         if text2:
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)
130
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: