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