adding the wizard to create a menu for the groups
[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 WikiGroup(osv.osv):
35     _name = "wiki.groups"
36     _description="Wiki Groups"
37     _order = 'name'
38     _columns={
39        'name':fields.char('Name', size=256, select=True, required=True),
40        'notes':fields.text("Description", select=True),
41        'create_date':fields.datetime("Created on", select=True),
42     }
43 WikiGroup()
44
45 class Wiki(osv.osv):
46     _name="wiki.wiki"
47     _description="Wiki"
48     _order = 'name'
49     _columns={
50         'group_id':fields.many2one('wiki.groups', 'Group'),
51         'name':fields.char('Title', size=256, select=True, required=True),
52         'write_uid':fields.many2one('res.users',"Last Modify By"),
53         'text_area':fields.text("Content", select=True),
54         'create_uid':fields.many2one('res.users','Authour', select=True),
55         'create_date':fields.datetime("Created on", select=True),
56         'write_date':fields.datetime("Last modified", select=True),
57         'tags':fields.char('Tags', size=1024),
58         'history_id':fields.one2many('wiki.wiki.history','history_wiki_id','History Lines'),
59         'minor_edit':fields.boolean('Thisd is a minor edit', select=True),
60         'summary':fields.char('Summary',size=256, select=True),
61     }
62
63     def __init__(self, cr, pool):
64         super(Wiki, self).__init__(cr, pool)
65         self.oldmodel = None
66
67     def read(self, cr, uid, cids, fields=None, context=None, load='_classic_read'):
68         ids = []
69         for id in cids:
70             if type(id) == type(1):
71                 ids.append(id)
72             elif type(id) == type(u''):
73                 ids.append(10)
74                 
75         result = super(Wiki, self).read(cr, uid, ids, fields, None, load='_classic_read')
76         return result
77
78     def create(self, cr, uid, vals, context=None):
79         if not vals.has_key('minor_edit'):
80             return super(Wiki,self).create(cr, uid, vals, context)
81         vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'summary':vals['summary']}]]
82         return super(Wiki,self).create(cr, uid, vals, context)
83
84     def write(self, cr, uid, ids, vals, context=None):
85         if vals.get('text_area'):
86             if vals.has_key('minor_edit') and vals.has_key('summary'):
87                 vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':vals['summary']}]]
88             elif vals.has_key('minor_edit'):
89                 vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':wiki_data['summary']}]]
90             elif vals.has_key('summary'):
91                 vals['history_id']=[[0,0,{'minor_edit':wiki_data['summary'],'text_area':vals['text_area'],'modify_by':uid,'summary':vals['summary']}]]
92             else:
93                 vals['history_id']=[[0,0,{'minor_edit':wiki_data['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':wiki_data['summary']}]]
94         return super(Wiki,self).write(cr, uid, ids, vals, context)
95 Wiki()
96
97 class History(osv.osv):
98     _name="wiki.wiki.history"
99     _description="Wiki History"
100     _rec_name="date_time"
101     _order = 'id DESC'
102     _columns={
103       'date_time':fields.datetime("Date",select=True),
104       'text_area':fields.text("Text area",select=True),
105       'minor_edit':fields.boolean('This is a major edit ?',select=True),
106       'summary':fields.char('Summary',size=256, select=True),
107       'modify_by':fields.many2one('res.users',"Modify By", select=True),
108       'hist_write_date':fields.datetime("Last modified", select=True),
109       'history_wiki_id':fields.many2one('wiki.wiki','Wiki Id', select=True)
110     }
111     _defaults = {
112         'hist_write_date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
113         'modify_by': lambda obj,cr,uid,context: uid,
114     }
115     
116     def getDiff(self, cr, uid, v1, v2, context={}):
117         import difflib
118         
119         history_pool = self.pool.get('wiki.wiki.history')
120         
121         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
122         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
123         
124         line1 = text1.splitlines(1)
125         line2 = text2.splitlines(1)
126         
127         diff = difflib.HtmlDiff()
128         
129         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
130 History()