[merge]
[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 from tools.translate import _
31 import difflib
32
33 class Wiki(osv.osv):
34     """ wiki """
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', 'Home Page'),
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 Wiki2(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 Contributor", select=True),
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, select=True),
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('Section', size=32, help="Use page section code like 1.2.1", select=True),
99         'group_id': fields.many2one('wiki.groups', 'Wiki Group', select=1, ondelete='set null', 
100             help="Topic, also called Wiki Group"),
101         'toc': fields.boolean('Table of Contents', 
102             help="Indicates that this pages is a table of contents (linking to other pages)"),
103         'review': fields.boolean('Needs Review', select=True, 
104             help="Indicates that this page should be reviewed, raising the attention of other contributors"),
105         'parent_id': fields.many2one('wiki.wiki', 'Parent Page'),
106         'child_ids': fields.one2many('wiki.wiki', 'parent_id', 'Child Pages'),
107     }
108
109     def onchange_group_id(self, cr, uid, ids, group_id, content, context={}):
110
111         """ @param cr: the current row, from the database cursor,
112             @param uid: the current user’s ID for security checks,
113             @param ids: List of wiki page’s IDs
114             @return: dictionay of open wiki page on give page section  """
115
116         if (not group_id) or content:
117             return {}
118         grp = self.pool.get('wiki.groups').browse(cr, uid, group_id)
119         section = '0'
120         for page in grp.page_ids:
121             if page.section: section = page.section
122         s = section.split('.')
123         template = grp.template
124         try:
125             s[-1] = str(int(s[-1])+1)
126         except:
127             pass
128         section = '.'.join(s)
129         return {
130             'value':{
131                 'text_area': template,
132                 'section': section
133             }
134         }
135
136     def copy_data(self, cr, uid, id, default=None, context=None):
137
138         """ @param cr: the current row, from the database cursor,
139             @param uid: the current user’s ID for security checks,
140             @param id: Give wiki page's ID """
141
142         return super(Wiki, self).copy_data(cr, uid, id, {'wiki_id': False}, context)
143
144     def create(self, cr, uid, vals, context=None):
145
146         """ @param cr: the current row, from the database cursor,
147             @param uid: the current user’s ID for security checks, """
148
149         id = super(Wiki, self).create(cr, uid, vals, context)
150         history = self.pool.get('wiki.wiki.history')
151         if vals.get('text_area'):
152             res = {
153                 'minor_edit': vals.get('minor_edit', True),
154                 'text_area': vals.get('text_area', ''),
155                 'write_uid': uid,
156                 'wiki_id': id,
157                 'summary':vals.get('summary', '')
158             }
159             history.create(cr, uid, res)
160         return id
161
162     def write(self, cr, uid, ids, vals, context=None):
163
164         """ @param cr: the current row, from the database cursor,
165             @param uid: the current user’s ID for security checks, """
166
167         result = super(Wiki, self).write(cr, uid, ids, vals, context)
168         history = self.pool.get('wiki.wiki.history')
169         if vals.get('text_area'):
170             for id in ids:
171                 res = {
172                     'minor_edit': vals.get('minor_edit', True),
173                     'text_area': vals.get('text_area', ''),
174                     'write_uid': uid,
175                     'wiki_id': id,
176                     'summary': vals.get('summary', '')
177                 }
178                 history.create(cr, uid, res)
179         return result
180
181 Wiki2()
182
183
184 class History(osv.osv):
185     """ Wiki History """
186
187     _name = "wiki.wiki.history"
188     _description = "Wiki History"
189     _rec_name = "date_time"
190     _order = 'id DESC'
191
192     _columns = {
193               'create_date': fields.datetime("Date", select=True),
194               'text_area': fields.text("Text area"),
195               'minor_edit': fields.boolean('This is a major edit ?', select=True),
196               'summary': fields.char('Summary', size=256, select=True),
197               'write_uid': fields.many2one('res.users', "Modify By", select=True),
198               'wiki_id': fields.many2one('wiki.wiki', 'Wiki Id', select=True)
199             }
200
201     _defaults = {
202         'write_uid': lambda obj, cr, uid, context: uid,
203     }
204
205     def getDiff(self, cr, uid, v1, v2, context={}):
206
207         """ @param cr: the current row, from the database cursor,
208             @param uid: the current user’s ID for security checks, """
209
210         history_pool = self.pool.get('wiki.wiki.history')
211         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
212         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
213         line1 = text1.splitlines(1)
214         line2 = text2.splitlines(1)
215         diff = difflib.HtmlDiff()
216         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
217
218 History()
219
220 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: