Launchpad automatic translations update.
[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, link=True):
33         index = []
34         for subpage in page.child_ids:
35             index += ["<li>"+ self._get_page_index(cr, uid, subpage) +"</li>"]
36         r = ''
37         if link:
38             r = '<a href="#id=%s">%s</a>'%(page.id,page.name)
39         if index:
40             r += "<ul>" + "".join(index) + "</ul>"
41         return r
42
43     def _get_display_content(self, cr, uid, ids, name, args, context=None):
44         res = {}
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)
48             else:
49                content = page.content
50             res[page.id] =  content
51         return res
52
53     _columns = {
54         'name': fields.char('Title', required=True),
55         'type':fields.selection([('content','Content'), ('category','Category')], 'Type', help="Page type"), 
56
57         'parent_id': fields.many2one('document.page', 'Category', domain=[('type','=','category')]),
58         'child_ids': fields.one2many('document.page', 'parent_id', 'Children'),
59
60         'content': fields.text("Content"),
61         'display_content': fields.function(_get_display_content, string='Displayed Content', type='text'),
62
63         'history_ids': fields.one2many('document.page.history', 'page_id', 'History'),
64         'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
65
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),
70     }
71     _defaults = {
72         'type':'content',
73     }
74
75     def onchange_parent_id(self, cr, uid, ids, parent_id, content, context=None):
76         res = {}
77         if parent_id and not content:
78             parent = self.browse(cr, uid, parent_id, context=context)
79             if parent.type == "category":
80                 res['value'] = {
81                     'content': parent.content,
82                 }
83         return res
84
85     def create_history(self, cr, uid, ids, vals, context=None):
86         for i in ids:
87             history = self.pool.get('document.page.history')
88             if vals.get('content'):
89                 res = {
90                     'content': vals.get('content', ''),
91                     'page_id': i,
92                 }
93                 history.create(cr, uid, res)
94
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)
98         return page_id
99
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)
103         return result
104
105 class document_page_history(osv.osv):
106     _name = "document.page.history"
107     _description = "Document Page History"
108     _order = 'id DESC'
109     _rec_name = "create_date"
110
111     _columns = {
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"),
117     }
118
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']
123         line1 = line2 = ''
124         if text1:
125             line1 = text1.splitlines(1)
126         if text2:
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)
132
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: