[MERGE] Merged Dhruti's branch for the fix of avoiding crash when reservation is...
[odoo/odoo.git] / addons / wiki / wiki.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 # Copyright (c) 2004-2006 TINY SPRL. (http://axelor.com) All Rights Reserved.
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from osv import fields, osv
30 import time
31 from StringIO import StringIO
32 from HTMLParser import HTMLParser
33
34 class Wiki(osv.osv):
35     _name="wiki.wiki"
36 Wiki()
37
38 class WikiGroup(osv.osv):
39     _name = "wiki.groups"
40     _description="Wiki Groups"
41     _order = 'name'
42     _columns={
43        'name':fields.char('Wiki Group', size=256, select=True, required=True),
44        'parent_id':fields.many2one('wiki.groups', 'Parent Group', ondelete='set null'),
45        'child_ids':fields.one2many('wiki.groups', 'parent_id', 'Child Groups'),
46        'page_ids':fields.one2many('wiki.wiki', 'group_id', 'Pages'),
47        'notes':fields.text("Description", select=True),
48        'create_date':fields.datetime("Created Date", select=True),
49        'template': fields.text('Wiki Template'),
50        'section': fields.boolean("Make Section ?"),
51        'home':fields.many2one('wiki.wiki', 'Pages')
52     }
53 WikiGroup()
54
55 class GroupLink(osv.osv):
56     _name = "wiki.groups.link"
57     _description="Wiki Groups Links"
58     _rec_name = 'action_id'
59     _columns={
60        'group_id':fields.many2one('wiki.groups', 'Parent Group', ondelete='set null'),
61        'action_id': fields.many2one('ir.ui.menu', 'Menu')
62     }
63 GroupLink()
64
65 class Wiki(osv.osv):
66     _inherit="wiki.wiki"
67     _description="Wiki Page"
68     _order = 'section,create_date desc'
69     _columns={
70         'name':fields.char('Title', size=256, select=True, required=True),
71         'write_uid':fields.many2one('res.users',"Last Author"),
72         'text_area':fields.text("Content"),
73         'create_uid':fields.many2one('res.users','Author', select=True),
74         'create_date':fields.datetime("Created on", select=True),
75         'write_date':fields.datetime("Modification Date", select=True),
76         'tags':fields.char('Tags', size=1024),
77         'history_id':fields.one2many('wiki.wiki.history','wiki_id','History Lines'),
78         'minor_edit':fields.boolean('Minor edit', select=True),
79         'summary':fields.char('Summary',size=256, select=True),
80         'section': fields.char('Section', size=32, help="Use page section code like 1.2.1"),
81         'group_id':fields.many2one('wiki.groups', 'Wiki Group', select=1, ondelete='set null'),
82         'toc':fields.boolean('Table of Contents'),
83         'review': fields.boolean('Need Review')
84     }
85     def onchange_group_id(self, cr, uid, ids, group_id, content, context={}):
86         if (not group_id) or content:
87             return {}
88         grp = self.pool.get('wiki.groups').browse(cr, uid, group_id)
89         section = '0'
90         for page in grp.page_ids:
91             if page.section: section = page.section
92         s = section.split('.')
93         template = grp.template
94         try:
95             s[-1] = str(int(s[-1])+1)
96         except:
97             pass
98         section = '.'.join(s)
99         return {
100             'value':{
101                 'text_area': template,
102                 'section': section
103             }
104         }
105     def copy_data(self, cr, uid, id, default=None, context=None):
106         return super(Wiki, self).copy_data(cr, uid, id, {'wiki_id':False}, context)
107
108     def create(self, cr, uid, vals, context=None):
109         id = super(Wiki,self).create(cr, uid, vals, context)
110         history = self.pool.get('wiki.wiki.history')
111         if vals.get('text_area'):
112             res = {
113                 'minor_edit':vals.get('minor_edit', True),
114                 'text_area':vals.get('text_area',''),
115                 'write_uid':uid,
116                 'wiki_id' : id,
117                 'summary':vals.get('summary','')
118             }
119             history.create(cr, uid, res)
120         return id
121
122     def write(self, cr, uid, ids, vals, context=None):
123         result = super(Wiki,self).write(cr, uid, ids, vals, context)
124         history = self.pool.get('wiki.wiki.history')
125         if vals.get('text_area'):
126             for id in ids:
127                 res = {
128                     'minor_edit':vals.get('minor_edit', True),
129                     'text_area':vals.get('text_area',''),
130                     'write_uid':uid,
131                     'wiki_id' : id,
132                     'summary':vals.get('summary','')
133                 }
134                 history.create(cr, uid, res)
135         return result
136
137 Wiki()
138
139 class History(osv.osv):
140     _name = "wiki.wiki.history"
141     _description = "Wiki History"
142     _rec_name = "summary"
143     _order = 'id DESC'
144     _columns = {
145       'create_date':fields.datetime("Date",select=True),
146       'text_area':fields.text("Text area"),
147       'minor_edit':fields.boolean('This is a major edit ?',select=True),
148       'summary':fields.char('Summary',size=256, select=True),
149       'write_uid':fields.many2one('res.users',"Modify By", select=True),
150       'wiki_id':fields.many2one('wiki.wiki','Wiki Id', select=True)
151     }
152     _defaults = {
153         'write_uid': lambda obj,cr,uid,context: uid,
154     }
155     def getDiff(self, cr, uid, v1, v2, context={}):
156         import difflib
157         history_pool = self.pool.get('wiki.wiki.history')
158         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
159         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
160         line1 = line2 = ''
161         if text1:
162             line1 = text1.splitlines(1)
163         if text2:
164             line2 = text2.splitlines(1)
165         if (not line1 and not line2) or (line1 == line2):
166             raise osv.except_osv(_('Warning !'), _('There are no chnages in revisions'))
167         diff = difflib.HtmlDiff()
168         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
169 History()
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: