00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 import os, time, base64, datetime
00031 import copy
00032 
00033 from Koo import Rpc
00034 
00035 import Wizard
00036 from Koo.Printer import *
00037 
00038 from Koo.Common import Api
00039 from Koo.Common import Common
00040 from PyQt4.QtCore import *
00041 from PyQt4.QtGui import *
00042 
00043 class ExecuteReportThread(QThread):
00044         def __init__(self, name, data, context=None, parent=None):
00045                 QThread.__init__(self, parent)
00046                 if context is None:
00047                         context = {}
00048                 self.name = name
00049                 self.datas = data.copy()
00050                 self.context = context
00051                 self.status = ''
00052                 self.session = Rpc.session.copy()
00053 
00054         def run(self):
00055                 ids = self.datas['ids']
00056                 del self.datas['ids']
00057                 if not ids:
00058                         try:
00059                                 ids = self.session.call('/object', 'execute', self.datas['model'], 'search', [])
00060                         except Rpc.RpcException, e:
00061                                 self.emit( SIGNAL('error'), ( _('Error: %s') % unicode(e.type), e.message, e.data ) )
00062                                 return
00063                                 
00064                         if ids == []:
00065                                 self.emit( SIGNAL('warning'), _('Nothing to print.') )
00066                                 return 
00067                         self.datas['id'] = ids[0]
00068                 try:
00069                         ctx = self.session.context.copy()
00070                         ctx.update(self.context)
00071                         report_id = self.session.call('/report', 'report', self.name, ids, self.datas, ctx)
00072                         state = False
00073                         attempt = 0
00074                         while not state:
00075                                 val = self.session.call('/report', 'report_get', report_id)
00076                                 state = val['state']
00077                                 if not state:
00078                                         time.sleep(1)
00079                                         attempt += 1
00080                                 if attempt>200:
00081                                         self.emit( SIGNAL('warning'), _('Printing aborted. Delay too long.') )
00082                                         return False
00083                         Printer.printData(val)
00084                 except Rpc.RpcException, e:
00085                         self.emit( SIGNAL('error'), ( _('Error: %s') % unicode(e.type), e.message, e.data ) )
00086                 
00087 
00088 def executeReport(name, data, context=None):
00089         if context is None:
00090                 context = {}
00091         QApplication.setOverrideCursor( Qt.WaitCursor )
00092         datas = data.copy()
00093         ids = datas['ids']
00094         del datas['ids']
00095         try:
00096                 if not ids:
00097                         ids = Rpc.session.execute('/object', 'execute', datas['model'], 'search', [])
00098                         if ids == []:
00099                                 QApplication.restoreOverrideCursor()
00100                                 QMessageBox.information( None, _('Information'), _('Nothing to print!'))
00101                                 return False
00102                         datas['id'] = ids[0]
00103                 ctx = Rpc.session.context.copy()
00104                 ctx.update(context)
00105                 report_id = Rpc.session.execute('/report', 'report', name, ids, datas, ctx)
00106                 state = False
00107                 attempt = 0
00108                 while not state:
00109                         val = Rpc.session.execute('/report', 'report_get', report_id)
00110                         state = val['state']
00111                         if not state:
00112                                 time.sleep(1)
00113                                 attempt += 1
00114                         if attempt>200:
00115                                 QApplication.restoreOverrideCursor()
00116                                 QMessageBox.information( None, _('Error'), _('Printing aborted, too long delay !'))
00117                                 return False
00118                 Printer.printData(val, datas['model'], ids)
00119         except Rpc.RpcException, e:
00120                 QApplication.restoreOverrideCursor()
00121                 return False
00122         QApplication.restoreOverrideCursor()
00123         return True
00124 
00125 
00126 def execute(act_id, datas, type=None, context=None):
00127         if context is None:
00128                 context = {}
00129         ctx = Rpc.session.context.copy()
00130         ctx.update(context)
00131         if type==None:
00132                 res = Rpc.session.execute('/object', 'execute', 'ir.actions.actions', 'read', [act_id], ['type'], ctx)
00133                 if not len(res):
00134                         raise Exception, 'ActionNotFound'
00135                 type=res[0]['type']
00136         res = Rpc.session.execute('/object', 'execute', type, 'read', [act_id], False, ctx)[0]
00137         Api.instance.executeAction(res,datas,context)
00138 
00139 
00140 def executeAction(action, datas, context=None):
00141         if context is None:
00142                 context = {}
00143         if 'type' not in action:
00144                 return
00145         if action['type']=='ir.actions.act_window':
00146                 for key in ('res_id', 'res_model', 'view_type', 'view_mode',
00147                                 'limit', 'auto_refresh'):
00148                         datas[key] = action.get(key, datas.get(key, None))
00149 
00150                 if datas['limit'] is None or datas['limit'] == 0:
00151                         datas['limit'] = 80
00152 
00153                 view_ids=False
00154                 if action.get('views', []):
00155                         view_ids=[x[0] for x in action['views']]
00156                         datas['view_mode']=",".join([x[1] for x in action['views']])
00157                 elif action.get('view_id', False):
00158                         view_ids=[action['view_id'][0]]
00159 
00160 
00161                 if not action.get('domain', False):
00162                         action['domain']='[]'
00163 
00164                 ctx = context.copy()
00165                 ctx.update( {'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])} )
00166                 ctx.update(Rpc.session.evaluateExpression(action.get('context','{}'), ctx.copy()) )
00167 
00168                 a = ctx.copy()
00169                 a['time'] = time
00170                 a['datetime'] = datetime
00171                 domain = Rpc.session.evaluateExpression(action['domain'], a)
00172 
00173                 if datas.get('domain', False):
00174                         domain.append(datas['domain'])
00175 
00176                 target = action.get('target','current')
00177                 if not target:
00178                         target = 'current'
00179                 Api.instance.createWindow( view_ids, datas['res_model'], datas['res_id'], domain,
00180                         action['view_type'], datas.get('window',None), ctx,
00181                         datas['view_mode'], name=action.get('name', False), autoReload=datas['auto_refresh'], 
00182                         target=target )
00183 
00184                 
00185                 
00186 
00187         elif action['type']=='ir.actions.server':
00188                 ctx = context.copy()
00189                 ctx.update({'active_id': datas.get('id',False), 'active_ids': datas.get('ids',[])})
00190                 res = Rpc.session.execute('/object', 'execute', 'ir.actions.server', 'run', [action['id']], ctx)
00191                 if res:
00192                         self.executeAction( res, datas, context )
00193 
00194         elif action['type']=='ir.actions.wizard':
00195                 win=None
00196                 if 'window' in datas:
00197                         win=datas['window']
00198                         del datas['window']
00199                 Wizard.execute(action['wiz_name'], datas, parent=win, context=context)
00200 
00201         elif action['type']=='ir.actions.report.custom':
00202                 if 'window' in datas:
00203                         win=datas['window']
00204                         del datas['window']
00205                 datas['report_id'] = action['report_id']
00206                 Api.instance.executeReport('custom', datas, context)
00207 
00208         elif action['type']=='ir.actions.report.xml':
00209                 if 'window' in datas:
00210                         win=datas['window']
00211                         del datas['window']
00212                 Api.instance.executeReport(action['report_name'], datas, context)
00213         elif action['type']=='ir.actions.act_url':
00214                 Api.instance.createWebWindow( action.get('url'), action.get('name') )
00215 
00216 
00217 def executeKeyword(keyword, data=None, context=None):
00218         if data is None:
00219                 data = {}
00220         if context is None:
00221                 context = {}
00222         actions = None
00223         if 'id' in data:
00224                 try:
00225                         id = data.get('id', False) 
00226                         actions = Rpc.session.execute('/object', 'execute',
00227                                         'ir.values', 'get', 'action', keyword,
00228                                         [(data['model'], id)], False, Rpc.session.context)
00229                         actions = map(lambda x: x[2], actions)
00230                 except Rpc.RpcException, e:
00231                         return None
00232 
00233         if not actions:
00234                 return None
00235 
00236         keyact = {}
00237         for action in actions:
00238                 keyact[action['name']] = action
00239 
00240         res = Common.selection(_('Select your action'), keyact)
00241         if not res:
00242                 return None
00243         (name,action) = res
00244         Api.instance.executeAction(action, data, context=context)
00245         return (name, action)
00246