change implementation for the opening page / group list
[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('Wiki Group', size=256, select=True, required=True),
40        'parent_id':fields.many2one('wiki.groups', 'Parent Group', ondelete='set null'),
41        'child_ids':fields.one2many('wiki.groups', 'parent_id', 'Child Groups'),
42        'page_ids':fields.one2many('wiki.wiki', 'group_id', 'Pages'),
43        'notes':fields.text("Description", select=True),
44        'create_date':fields.datetime("Created Date", select=True),
45        'template': fields.text('Wiki Template'),
46        'section': fields.boolean("Make Section ?"),
47        'home':fields.many2one('wiki.wiki', 'Pages'),
48        'action_id': fields.many2one('ir.ui.menu', 'Menu')
49     }
50 WikiGroup()
51
52 class Wiki(osv.osv):
53     _name="wiki.wiki"
54     _description="Wiki Page"
55     _order = 'section,create_date desc'
56     _columns={
57         'name':fields.char('Title', size=256, select=True, required=True),
58         'write_uid':fields.many2one('res.users',"Last Author"),
59         'text_area':fields.text("Content", select=True),
60         'create_uid':fields.many2one('res.users','Author', select=True),
61         'create_date':fields.datetime("Created on", select=True),
62         'write_date':fields.datetime("Modification Date", select=True),
63         'tags':fields.char('Tags', size=1024),
64         'history_id':fields.one2many('wiki.wiki.history','wiki_id','History Lines'),
65         'minor_edit':fields.boolean('Minor edit', select=True),
66         'summary':fields.char('Summary',size=256, select=True),
67         'section': fields.char('Section', size=32, help="Use page section code like 1.2.1"),
68         'group_id':fields.many2one('wiki.groups', 'Wiki Group', select=1, ondelete='set null'),
69         'toc':fields.boolean('Table of Contents'),
70         'review': fields.boolean('Need Review')
71     }
72     def onchange_group_id(self, cr, uid, ids, group_id, content, context={}):
73         if (not group_id) or content:
74             return {}
75         grp = self.pool.get('wiki.groups').browse(cr, uid, group_id)
76         section = '0'
77         for page in grp.page_ids:
78             if page.section: section = page.section
79         s = section.split('.')
80         template = grp.template
81         try:
82             s[-1] = str(int(s[-1])+1)
83         except:
84             pass
85         section = '.'.join(s)
86         return {
87             'value':{
88                 'text_area': template,
89                 'section': section
90             }
91         }
92     def copy(self, cr, uid, id, default=None, context=None):
93         return super(Wiki, self).copy(cr, uid, id, {'wiki_id':False}, context)
94
95     def write(self, cr, uid, ids, vals, context=None):
96         result = super(Wiki,self).write(cr, uid, ids, vals, context)
97         history = self.pool.get('wiki.wiki.history')
98         if vals.get('text_area'):
99             for id in ids:
100                 res = {
101                     'minor_edit':vals.get('minor_edit', True),
102                     'text_area':vals.get('text_area',''),
103                     'write_uid':uid,
104                     'wiki_id' : id,
105                     'summary':vals.get('summary','')
106                 }
107                 history.create(cr, uid, res)
108         return result
109
110 Wiki()
111
112 class History(osv.osv):
113     _name="wiki.wiki.history"
114     _description="Wiki History"
115     _rec_name="date_time"
116     _order = 'id DESC'
117     _columns={
118       'create_date':fields.datetime("Date",select=True),
119       'text_area':fields.text("Text area",select=True),
120       'minor_edit':fields.boolean('This is a major edit ?',select=True),
121       'summary':fields.char('Summary',size=256, select=True),
122       'write_uid':fields.many2one('res.users',"Modify By", select=True),
123       'wiki_id':fields.many2one('wiki.wiki','Wiki Id', select=True)
124     }
125     _defaults = {
126         'write_uid': lambda obj,cr,uid,context: uid,
127     }
128     def getDiff(self, cr, uid, v1, v2, context={}):
129         import difflib
130         history_pool = self.pool.get('wiki.wiki.history')
131         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
132         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
133         line1 = text1.splitlines(1)
134         line2 = text2.splitlines(1)
135         diff = difflib.HtmlDiff()
136         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
137 History()