[MERGE] lp881356
[odoo/odoo.git] / addons / wiki / wiki.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22
23 from osv import fields, osv
24 from tools.translate import _
25 import difflib
26 import tools
27
28 class wiki_wiki(osv.osv):
29     """ wiki """
30     _name = "wiki.wiki"
31
32 wiki_wiki()
33
34 class wiki_group(osv.osv):
35     """ Wiki Groups """
36
37     _name = "wiki.groups"
38     _description = "Wiki Groups"
39     _order = 'name'
40
41     _columns = {
42        'name':fields.char('Wiki Group', size=256, select=True, required=True),
43        'page_ids':fields.one2many('wiki.wiki', 'group_id', 'Pages'),
44        'notes':fields.text("Description"),
45        'create_date':fields.datetime("Created Date", select=True),
46        'template': fields.text('Wiki Template'),
47        'section': fields.boolean("Make Section ?"),
48        'method':fields.selection([('list', 'List'), ('page', 'Home Page'), \
49                                    ('tree', 'Tree')], 'Display Method', help="Define the default behaviour of the menu created on this group"),
50        'home':fields.many2one('wiki.wiki', 'Home Page', help="Required to select home page if display method is Home Page"),
51        'menu_id': fields.many2one('ir.ui.menu', "Menu", readonly=True),
52     }
53
54     _defaults = {
55         'method': lambda *a: 'page',
56     }
57
58     def open_wiki_page(self, cr, uid, ids, context=None):
59
60         """ Opens Wiki Page of Group
61         @param cr: the current row, from the database cursor,
62         @param uid: the current user’s ID for security checks,
63         @param ids: List of open wiki group’s IDs
64         @return: dictionay of open wiki window on give group id
65         """
66         if type(ids) in (int,long,):
67             ids = [ids]
68         group_id = False
69         if ids:
70             group_id = ids[0]
71         if not group_id:
72             return {}
73         value = {
74             'name': 'Wiki Page',
75             'view_type': 'form',
76             'view_mode': 'form,tree',
77             'res_model': 'wiki.wiki',
78             'view_id': False,
79             'type': 'ir.actions.act_window',
80             'nodestroy': True,
81         }
82         group = self.browse(cr, uid, group_id, context=context)
83         value['domain'] = "[('group_id','=',%d)]" % (group.id)
84         if group.method == 'page':
85             value['res_id'] = group.home.id
86         elif group.method == 'list':
87             value['view_type'] = 'form'
88             value['view_mode'] = 'tree,form'
89         elif group.method == 'tree':
90             view_id = self.pool.get('ir.ui.view').search(cr, uid, [('name', '=', 'wiki.wiki.tree.children')])
91             value['view_id'] = view_id
92             value['domain'] = [('group_id', '=', group.id), ('parent_id', '=', False)]
93             value['view_type'] = 'tree'
94
95         return value
96 wiki_group()
97
98
99 class wiki_wiki2(osv.osv):
100     """ Wiki Page """
101
102     _inherit = "wiki.wiki"
103     _description = "Wiki Page"
104     _order = 'section,create_date desc'
105
106     _columns = {
107         'name': fields.char('Title', size=256, select=True, required=True),
108         'write_uid': fields.many2one('res.users', "Last Contributor", select=True),
109         'text_area': fields.text("Content"),
110         'create_uid': fields.many2one('res.users', 'Author', select=True, readonly=True),
111         'create_date': fields.datetime("Created on", select=True, readonly=True),
112         'write_date': fields.datetime("Modification Date", select=True, readonly=True),
113         'tags': fields.char('Keywords', size=1024, select=True),
114         'history_id': fields.one2many('wiki.wiki.history', 'wiki_id', 'History Lines'),
115         'minor_edit': fields.boolean('Minor edit', select=True),
116         'summary': fields.char('Summary', size=256),
117         'section': fields.char('Section', size=32, help="Use page section code like 1.2.1", select=True),
118         'group_id': fields.many2one('wiki.groups', 'Wiki Group', select=1, ondelete='set null',
119             help="Topic, also called Wiki Group"),
120         'toc': fields.boolean('Table of Contents',
121             help="Indicates that this pages have a table of contents or not"),
122         'review': fields.boolean('Needs Review', select=True,
123             help="Indicates that this page should be reviewed, raising the attention of other contributors"),
124         'parent_id': fields.many2one('wiki.wiki', 'Parent Page', help="Allows you to link with the other page with in the current topic"),
125         'child_ids': fields.one2many('wiki.wiki', 'parent_id', 'Child Pages'),
126     }
127     _defaults = {
128         'toc': True,
129         'review': True,
130         'minor_edit': True,
131     }
132
133     def onchange_group_id(self, cr, uid, ids, group_id, content, 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 ids: List of wiki page’s IDs
138             @return: dictionay of open wiki page on give page section  """
139
140         if (not group_id) or content:
141             return {}
142         grp = self.pool.get('wiki.groups').browse(cr, uid, group_id, context=context)
143         section = '0'
144         for page in grp.page_ids:
145             if page.section: section = page.section
146         s = section.split('.')
147         template = grp.template
148         try:
149             s[-1] = str(int(s[-1])+1)
150         except:
151             pass
152         section = '.'.join(s)
153         return {
154             'value':{
155                 'text_area': template,
156                 'section': section
157             }
158         }
159
160     def copy_data(self, cr, uid, id, default=None, context=None):
161
162         """ @param cr: the current row, from the database cursor,
163             @param uid: the current user’s ID for security checks,
164             @param id: Give wiki page's ID """
165
166         return super(wiki_wiki2, self).copy_data(cr, uid, id, {'wiki_id': False}, context)
167
168     def create_history(self, cr, uid, ids, vals, context=None):
169         history_id = False
170         history = self.pool.get('wiki.wiki.history')
171         if vals.get('text_area'):
172             res = {
173                 'minor_edit': vals.get('minor_edit', True),
174                 'text_area': vals.get('text_area', ''),
175                 'write_uid': uid,
176                 'wiki_id': ids[0],
177                 'summary':vals.get('summary', '')
178             }
179             history_id = history.create(cr, uid, res)
180         return history_id
181
182     def create(self, cr, uid, vals, context=None):
183
184         """ @param cr: the current row, from the database cursor,
185             @param uid: the current user’s ID for security checks, """
186         wiki_id = super(wiki_wiki2, self).create(cr, uid,
187                              vals, context)
188         self.create_history(cr, uid, [wiki_id], vals, context)
189         return wiki_id
190
191     def write(self, cr, uid, ids, vals, context=None):
192
193         """ @param cr: the current row, from the database cursor,
194             @param uid: the current user’s ID for security checks, """
195         result = super(wiki_wiki2, self).write(cr, uid, ids, vals, context)
196         self.create_history(cr, uid, ids, vals, context)
197         return result
198
199 wiki_wiki2()
200
201
202 class wiki_history(osv.osv):
203     """ Wiki History """
204
205     _name = "wiki.wiki.history"
206     _description = "Wiki History"
207     _rec_name = "summary"
208     _order = 'id DESC'
209
210     _columns = {
211           'create_date': fields.datetime("Date", select=True),
212           'text_area': fields.text("Text area"),
213           'minor_edit': fields.boolean('This is a major edit ?', select=True),
214           'summary': fields.char('Summary', size=256, select=True),
215           'write_uid': fields.many2one('res.users', "Modify By", select=True),
216           'wiki_id': fields.many2one('wiki.wiki', 'Wiki Id', select=True)
217     }
218
219     _defaults = {
220         'write_uid': lambda obj, cr, uid, context: uid,
221     }
222
223     def getDiff(self, cr, uid, v1, v2, context=None):
224
225         """ @param cr: the current row, from the database cursor,
226             @param uid: the current user’s ID for security checks, """
227
228         history_pool = self.pool.get('wiki.wiki.history')
229         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
230         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
231         line1 = line2 = ''
232         if text1:
233             line1 = tools.ustr(text1.splitlines(1))
234         if text2:
235             line2=tools.ustr(text2.splitlines(1))
236         if (not line1 and not line2) or (line1 == line2):
237             raise osv.except_osv(_('Warning !'), _('There are no changes in revisions'))
238         diff = difflib.HtmlDiff()
239         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
240
241 wiki_history()
242
243 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: