1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # Copyright (c) 2004-2006 TINY SPRL. (http://axelor.com) All Rights Reserved.
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
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.
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.
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.
27 ##############################################################################
29 from osv import fields, osv
31 #from core.Wiki2Html import Wiki2Html
32 from StringIO import StringIO
39 'name':fields.char('Title',size=128),
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),
63 def __init__(self, cr, pool):
64 super(Wiki, self).__init__(cr, pool)
67 def read(self, cr, uid, cids, fields=None, context=None, load='_classic_read'):
70 if type(id) == type(1):
72 elif type(id) == type(u''):
75 result = super(Wiki, self).read(cr, uid, ids, fields, None, load='_classic_read')
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)
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']}]]
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)
99 class History(osv.osv):
100 _name="wiki.wiki.history"
101 _description="Wiki History"
102 _rec_name="date_time"
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)
114 'hist_write_date': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
115 'modify_by': lambda obj,cr,uid,context: uid,
118 def getDiff(self, cr, uid, v1, v2, context={}):
121 history_pool = self.pool.get('wiki.wiki.history')
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']
126 line1 = text1.splitlines(1)
127 line2 = text2.splitlines(1)
129 diff = difflib.HtmlDiff()
131 return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
135 from StringIO import StringIO
136 from HTMLParser import HTMLParser
138 class IndexLine(osv.osv):
139 _name="wiki.index.line"
140 _description="Index Lines"
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),
148 'active': lambda *a: True,
151 def __init__(self, cr , pool):
152 super(IndexLine, self).__init__(cr, pool)
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)
159 self.Index(cr, uid, context)
164 def Index(self, cr, uid, ids, context={}):
166 entry_pool = self.pool.get('wiki.index.line')
167 eids = entry_pool.search(cr, uid, [])
169 model_pool = self.pool.get('ir.model')
170 field_pool = self.pool.get('ir.model.fields')
172 ids = model_pool.search(cr, uid, [('model','!=','wiki.index.line')])
173 models = model_pool.read(cr, uid, ids, ['id', 'model'])
178 if str(mod['model']).startswith('workflow'):
181 if str(mod['model']).startswith('ir'):
184 if str(mod['model']).startswith('wiki.index.line'):
187 if str(mod['model']).startswith('res.currency'):
190 if str(mod['model']).startswith('wiki.wiki'):
193 if str(mod['model']).startswith('report'):
196 if str(mod['model']).startswith('account_analytic_analysis.summary.user'):
199 if str(mod['model']).startswith('hr_timesheet_invoice.factor'):
203 fids = field_pool.search(cr, 1, [('model_id','=',mod['id'])])
204 fdata = field_pool.read(cr, uid, fids, ['name','relation', 'ttype'])
211 res_pool = self.pool.get(mod['model'])
216 res_ids = res_pool.search(cr, 1, [])
217 res_datas = res_pool.read(cr, 1, res_ids)
222 print 'Indexing : ', mod['model'], ' Auto Status : '
225 for res_data in res_datas:
227 exist_ids = entry_pool.search(cr, uid, [('res_id','=',res_data['id']),('model','=',mod['model'])])
228 if exist_ids.__len__() > 0:
232 final_res += ' '.join(map(str,res_data.values()))
234 entry_pool.create(cr, uid, {
235 'model':mod['model'],
236 'res_id':res_data['id'],
243 def createIndex(self, cr, uid, ids, context={}):
244 return self.Index(cr, uid, ids, context)
246 def getResult(self, cr, uid, key, reSearch=True, context = {}):
248 if context and context.get('read_all') and key:
250 ids = self.search(cr, uid, [('name','ilike',key)])
251 res = self.read(cr, uid, ids, ['model', 'res_id'])
253 raise osv.except_osv('Search Error !!', e.faultString)
257 model = rs.get('model')
258 id = rs.get('res_id')
259 if trs.has_key(model):
268 for mod in res.keys():
270 obj_pool = self.pool.get(mod)
276 name = obj_pool.name_get(cr, 1, [id])[0][1]
277 obj_pool.read(cr, uid, [id])
283 rec_url = '/form/view?model=' + mod + "&id=" + str(id)
284 edit_url = '/form/edit?model=' + mod + "&id=" + str(id)
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'
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
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}]