improve the module : wiki
[odoo/odoo.git] / addons / wiki / wiki.py
1 # -*- encoding: 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 time
31 #from core.Wiki2Html import Wiki2Html
32 from StringIO import StringIO
33
34 class Tag(osv.osv):
35     _name="wiki.wiki.tag"
36     _description="Wiki"
37
38     _columns={
39        'name':fields.char('Title',size=128),
40     }
41 Tag()
42
43 class Wiki(osv.osv):
44     _name="wiki.wiki"
45     _description="Wiki"
46     _order = 'model_id'
47     _columns={
48        'name':fields.char('Title', size=128, select=True, required=True),
49        'write_uid':fields.many2one('res.users',"Last Modify By"),
50        'text_area':fields.text("Content", select=True),
51        'create_uid':fields.many2one('res.users','Authour', select=True),
52        'create_date':fields.datetime("Created on", select=True),
53        'write_date':fields.datetime("Last modified", select=True),
54        'tags':fields.char('Tags', size=1024), # many2many("wiki.wiki.tag","wiki_tag_many_id","wiki_id","tag_id","Tags", select=True),
55        'history_id':fields.one2many('wiki.wiki.history','history_wiki_id','History Lines'),
56        'path':fields.char('Page Path',size=128),
57        'model_id': fields.many2one('ir.model', 'Model id', select=True, ondelete='cascade'),
58        'res_id': fields.integer("Record Id"),
59        'minor_edit':fields.boolean('Thisd is a minor edit', select=True),
60        'summary':fields.char('Summary',size=256, select=True),
61     }
62
63     def __init__(self, cr, pool):
64         super(Wiki, self).__init__(cr, pool)
65         self.oldmodel = None
66
67     def read(self, cr, uid, cids, fields=None, context=None, load='_classic_read'):
68         ids = []
69         for id in cids:
70             if type(id) == type(1):
71                 ids.append(id)
72             elif type(id) == type(u''):
73                 ids.append(10)
74                 
75         result = super(Wiki, self).read(cr, uid, ids, fields, None, load='_classic_read')
76         return result
77
78     def create(self, cr, uid, vals, context=None):
79         if not vals.has_key('minor_edit'):
80             return super(Wiki,self).create(cr, uid, vals, context)
81         vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'summary':vals['summary']}]]
82         return super(Wiki,self).create(cr, uid, vals, context)
83
84     def write(self, cr, uid, ids, vals, context=None):
85 #        wiki_data=self.read(cr,uid,ids,['minor_edit','summary'])[0]
86         if vals.get('text_area'):
87 #            vals['html'] = self.Wiki2Html(vals['text_area'])
88             if vals.has_key('minor_edit') and vals.has_key('summary'):
89                 vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':vals['summary']}]]
90             elif vals.has_key('minor_edit'):
91                 vals['history_id']=[[0,0,{'minor_edit':vals['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':wiki_data['summary']}]]
92             elif vals.has_key('summary'):
93                 vals['history_id']=[[0,0,{'minor_edit':wiki_data['summary'],'text_area':vals['text_area'],'modify_by':uid,'summary':vals['summary']}]]
94             else:
95                 vals['history_id']=[[0,0,{'minor_edit':wiki_data['minor_edit'],'text_area':vals['text_area'],'modify_by':uid,'summary':wiki_data['summary']}]]
96         return super(Wiki,self).write(cr, uid, ids, vals, context)
97 Wiki()
98
99 class History(osv.osv):
100     _name="wiki.wiki.history"
101     _description="Wiki History"
102     _rec_name="date_time"
103     _order = 'id DESC'
104     _columns={
105       'date_time':fields.datetime("Date",select=True),
106       'text_area':fields.text("Text area",select=True),
107       'minor_edit':fields.boolean('This is a major edit ?',select=True),
108       'summary':fields.char('Summary',size=256, select=True),
109       'modify_by':fields.many2one('res.users',"Modify By", select=True),
110       'hist_write_date':fields.datetime("Last modified", select=True),
111       'history_wiki_id':fields.many2one('wiki.wiki','Wiki Id', select=True)
112     }
113     _defaults = {
114         'hist_write_date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
115         'modify_by': lambda obj,cr,uid,context: uid,
116     }
117     
118     def getDiff(self, cr, uid, v1, v2, context={}):
119         import difflib
120         
121         history_pool = self.pool.get('wiki.wiki.history')
122         
123         text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area']
124         text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area']
125         
126         line1 = text1.splitlines(1)
127         line2 = text2.splitlines(1)
128         
129         diff = difflib.HtmlDiff()
130         
131         return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
132     
133 History()
134
135 from StringIO import StringIO
136 from HTMLParser import HTMLParser
137
138 class IndexLine(osv.osv):
139     _name="wiki.index.line"
140     _description="Index Lines"
141     _columns={
142       'name':fields.text('Content', select=True),
143       'active':fields.boolean('Active', select=True),
144       'model':fields.char('Resource',size=256, select=True),
145       'res_id':fields.integer('Resource Id', select=True),
146     }
147     _defaults = {
148         'active': lambda *a: True,
149     }
150     
151     def __init__(self, cr , pool):
152         super(IndexLine, self).__init__(cr, pool)
153      
154     def reIndex(self, cr, uid, ids, context={}):
155         password = self.pool.get('res.users').browse(cr, uid, uid).password
156         self.server.init(cr.dbname, uid, password, True)
157         self.server.open()
158         
159         self.Index(cr, uid, context)
160         
161         self.server.close()
162         return True
163     
164     def Index(self, cr, uid, ids, context={}):
165         
166         entry_pool = self.pool.get('wiki.index.line')
167         eids = entry_pool.search(cr, uid, [])
168         
169         model_pool = self.pool.get('ir.model')
170         field_pool = self.pool.get('ir.model.fields')
171         
172         ids = model_pool.search(cr, uid, [('model','!=','wiki.index.line')])
173         models = model_pool.read(cr, uid, ids, ['id', 'model'])
174         
175         for mod in models:
176             res_ids = None
177             
178             if str(mod['model']).startswith('workflow'):
179                 continue
180             
181             if str(mod['model']).startswith('ir'):
182                 continue
183             
184             if str(mod['model']).startswith('wiki.index.line'):
185                 continue
186             
187             if str(mod['model']).startswith('res.currency'):
188                 continue
189             
190             if str(mod['model']).startswith('wiki.wiki'):
191                 continue
192             
193             if str(mod['model']).startswith('report'):
194                 continue
195             
196             if str(mod['model']).startswith('account_analytic_analysis.summary.user'):
197                 continue
198             
199             if str(mod['model']).startswith('hr_timesheet_invoice.factor'):
200                 continue
201             
202             try:
203                 fids = field_pool.search(cr, 1, [('model_id','=',mod['id'])])
204                 fdata = field_pool.read(cr, uid, fids, ['name','relation', 'ttype'])
205             except Exception, e:
206                 continue
207         
208             res_pool = None
209             res_ids = None
210
211             res_pool = self.pool.get(mod['model'])
212             
213             res_datas = None
214             
215             try:
216                 res_ids = res_pool.search(cr, 1, [])
217                 res_datas = res_pool.read(cr, 1, res_ids)
218             except Exception, e:
219                 print e
220                 continue
221             
222             print 'Indexing : ', mod['model'], ' Auto Status : '
223             
224             if res_datas:
225                 for res_data in res_datas:
226                     
227                     exist_ids = entry_pool.search(cr, uid, [('res_id','=',res_data['id']),('model','=',mod['model'])])
228                     if exist_ids.__len__() > 0:
229                         continue
230
231                     final_res = ''
232                     final_res += ' '.join(map(str,res_data.values()))
233                     
234                     entry_pool.create(cr, uid, {
235                         'model':mod['model'],
236                         'res_id':res_data['id'],
237                         'name': final_res
238                     })
239                     cr.commit()
240
241         return True
242     
243     def createIndex(self, cr, uid, ids, context={}):
244         return self.Index(cr, uid, ids, context)
245     
246     def getResult(self, cr, uid, key, reSearch=True, context = {}):
247         
248         if context and context.get('read_all') and key:
249             try:
250                 ids = self.search(cr, uid, [('name','ilike',key)])
251                 res = self.read(cr, uid, ids, ['model', 'res_id'])
252             except Exception, e:
253                 raise osv.except_osv('Search Error !!', e.faultString)
254             
255             trs = {}
256             for rs in res:
257                 model = rs.get('model')
258                 id = rs.get('res_id')
259                 if trs.has_key(model):
260                     olds = trs[model]
261                     olds.append(id)
262                 else:
263                     trs[model] = [id]
264                     
265             res = trs
266             if res:
267                 buffer = ''
268                 for mod in res.keys():
269                     sbuff = ''
270                     obj_pool = self.pool.get(mod)
271                     header = False
272                     
273                     for id in res[mod]:
274                         name = None
275                         try:
276                             name = obj_pool.name_get(cr, 1, [id])[0][1]
277                             obj_pool.read(cr, uid, [id])
278                             header = True
279                         except Exception, e:
280                             header = False
281                             continue
282
283                         rec_url = '/form/view?model=' + mod + "&id=" + str(id)
284                         edit_url = '/form/edit?model=' + mod + "&id=" + str(id)
285                         sbuff+= '#Row\n'
286                         name = name.replace('/',' ')
287                         name = name.replace('*',' ')
288                         name = name.replace('_',' ')
289                         sbuff+= '| # | ' + name + ' | [' +  mod + '/' + str(id) + ' -  Wiki]' + ' | [' +  rec_url + ' - Browse ] | [' +  edit_url + ' - Edit ]\n'
290                     
291                     if header == True:
292                         mid = self.pool.get('ir.model').search(cr, uid, [('model','=',mod)])[0]
293                         mod_data = self.pool.get('ir.model').browse(cr, uid, mid).name
294                         sbuff = '\n==' + mod_data + '==\n' + sbuff
295                         
296                     buffer += sbuff
297                 html = self.pool.get('wiki.wiki').Wiki2Html(buffer)
298                 html = html.replace('$s', '<strike>')
299                 html = html.replace('$es', '</strike>')
300                 return [{'html':html}]
301
302         return None
303
304 IndexLine()