[IMP]: wiki: Minor improvements
[odoo/odoo.git] / addons / wiki / wiki.py
1 # -*- coding: 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 difflib
31
32 class Wiki(osv.osv):
33     """ wiki """
34
35     _name = "wiki.wiki"
36
37 Wiki()
38
39 class WikiGroup(osv.osv):
40     """ Wiki Groups """
41
42     _name = "wiki.groups"
43     _description = "Wiki Groups"
44     _order = 'name'
45
46     _columns = {
47        'name':fields.char('Wiki Group', size=256, select=True, required=True), 
48        'page_ids':fields.one2many('wiki.wiki', 'group_id', 'Pages'), 
49        'notes':fields.text("Description"), 
50        'create_date':fields.datetime("Created Date", select=True), 
51        'template': fields.text('Wiki Template'), 
52        'section': fields.boolean("Make Section ?"), 
53        'method':fields.selection([('list', 'List'), ('page', 'Home Page'), \
54                                    ('tree', 'Tree')], 'Display Method'), 
55        'home':fields.many2one('wiki.wiki', 'Pages'), 
56     }
57
58     _defaults = {
59         'method': lambda *a: 'page', 
60     }
61
62 WikiGroup()
63
64
65 class GroupLink(osv.osv):
66     """ Apply Group Link """
67
68     _name = "wiki.groups.link"
69     _description = "Wiki Groups Links"
70     _rec_name = 'action_id'
71
72     _columns = {
73        'group_id': fields.many2one('wiki.groups', 'Parent Group', ondelete='set null'), 
74        'action_id': fields.many2one('ir.ui.menu', 'Menu')
75     }
76
77 GroupLink()
78
79
80 class Wiki(osv.osv):
81     """ Wiki Page """
82
83     _inherit = "wiki.wiki"
84     _description = "Wiki Page"
85     _order = 'section,create_date desc'
86
87     _columns = {
88         'name': fields.char('Title', size=256, select=True, required=True), 
89         'write_uid': fields.many2one('res.users', "Last Author"), 
90         'text_area': fields.text("Content"), 
91         'create_uid': fields.many2one('res.users', 'Author', select=True), 
92         'create_date': fields.datetime("Created on", select=True), 
93         'write_date': fields.datetime("Modification Date", select=True), 
94         'tags': fields.char('Tags', size=1024), 
95         'history_id': fields.one2many('wiki.wiki.history', 'wiki_id', 'History Lines'), 
96         'minor_edit': fields.boolean('Minor edit', select=True), 
97         'summary': fields.char('Summary', size=256), 
98         'section': fields.char('Sequence', size=32, help="Use page section code like 1.2.1"), 
99         'group_id': fields.many2one('wiki.groups', 'Wiki Group', select=1, ondelete='set null'), 
100         'toc': fields.boolean('Table of Contents'), 
101         'review': fields.boolean('Need Review'), 
102         'parent_id': fields.many2one('wiki.wiki', 'Parent Page'), 
103         'child_ids': fields.one2many('wiki.wiki', 'parent_id', 'Child Pages'), 
104     }
105
106     def onchange_group_id(self, cr, uid, ids, group_id, content, context={}):
107
108         """ @param cr: the current row, from the database cursor,
109             @param uid: the current user’s ID for security checks,
110             @param ids: List of wiki page’s IDs
111             @return: dictionay of open wiki page on give page section  """
112
113         if (not group_id) or content:
114             return {}
115         grp = self.pool.get('wiki.groups').browse(cr, uid, group_id)
116         section = '0'
117         for page in grp.page_ids:
118             if page.section: section = page.section
119         s = section.split('.')
120         template = grp.template
121         try:
122             s[-1] = str(int(s[-1])+1)
123         except:
124             pass
125         section = '.'.join(s)
126         return {
127             'value':{
128                 'text_area': template, 
129                 'section': section
130             }
131         }
132
133     def copy_data(self, cr, uid, id, default=None, context=None):
134
135         """ @param cr: the current row, from the database cursor,
136             @param uid: the current user’s ID for security checks,
137             @param id: Give wiki page's ID """
138
139         return super(Wiki, self).copy_data(cr, uid, id, {'wiki_id': False}, context)
140
141     def create(self, cr, uid, vals, context=None):
142
143         """ @param cr: the current row, from the database cursor,
144             @param uid: the current user’s ID for security checks, """
145
146         id = super(Wiki, self).create(cr, uid, vals, context)
147         history = self.pool.get('wiki.wiki.history')
148         if vals.get('text_area'):
149             res = {
150                 'minor_edit': vals.get('minor_edit', True), 
151                 'text_area': vals.get('text_area', ''), 
152                 'write_uid': uid, 
153                 'wiki_id': id, 
154                 'summary':vals.get('summary', '')
155             }
156             history.create(cr, uid, res)
157         return id
158
159     def write(self, cr, uid, ids, vals, context=None):
160
161         """ @param cr: the current row, from the database cursor,
162             @param uid: the current user’s ID for security checks, """
163
164         result = super(Wiki, self).write(cr, uid, ids, vals, context)
165         history = self.pool.get('wiki.wiki.history')
166         if vals.get('text_area'):
167             for id in ids:
168                 res = {
169                     'minor_edit': vals.get('minor_edit', True), 
170                     'text_area': vals.get('text_area', ''), 
171                     'write_uid': uid, 
172                     'wiki_id': id, 
173                     'summary': vals.get('summary', '')
174                 }
175                 history.create(cr, uid, res)
176         return result
177
178 Wiki()
179
180
181 class History(osv.osv):
182     """ Wiki History """
183
184     _name = "wiki.wiki.history"
185     _description = "Wiki History"
186     _rec_name = "date_time"
187     _order = 'id DESC'
188
189     _columns = {
190               'create_date': fields.datetime("Date", select=True), 
191               'text_area': fields.text("Text area"), 
192               'minor_edit': fields.boolean('This is a major edit ?', select=True), 
193               'summary': fields.char('Summary', size=256, select=True), 
194               'write_uid': fields.many2one('res.users', "Modify By", select=True), 
195               'wiki_id': fields.many2one('wiki.wiki', 'Wiki Id', select=True)
196             }
197
198     _defaults = {
199         'write_uid': lambda obj, cr, uid, context: uid, 
200     }
201
202     def getDiff(self, cr, uid, v1, v2, context={}):
203
204         """ @param cr: the current row, from the database cursor,
205             @param uid: the current user’s ID for security checks, """
206
207         history_pool = self.pool.get('wiki.wiki.history')
208         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
209         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
210         line1 = text1.splitlines(1)
211         line2 = text2.splitlines(1)
212         diff = difflib.HtmlDiff()
213         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
214
215 History()
216
217 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: