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