2008-07-23 15:01:27 +00:00
# -*- encoding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
#
2008-11-22 00:09:35 +00:00
# OpenERP, Open Source Management Solution
2008-11-03 18:27:16 +00:00
# Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
# $Id$
2008-06-16 11:00:21 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
##############################################################################
from osv import fields , osv
2007-09-07 14:02:45 +00:00
import tools
2008-06-15 00:25:47 +00:00
import time
2008-07-23 07:33:17 +00:00
from tools . config import config
2008-07-23 08:16:45 +00:00
import netsvc
2008-08-21 10:36:17 +00:00
import re
2006-12-07 13:41:40 +00:00
class actions ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.actions '
_table = ' ir_actions '
_columns = {
' name ' : fields . char ( ' Action Name ' , required = True , size = 64 ) ,
' type ' : fields . char ( ' Action Type ' , required = True , size = 32 ) ,
' usage ' : fields . char ( ' Action Usage ' , size = 32 ) ,
}
_defaults = {
' usage ' : lambda * a : False ,
}
2006-12-07 13:41:40 +00:00
actions ( )
class report_custom ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.report.custom '
_table = ' ir_act_report_custom '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Report Name ' , size = 64 , required = True , translate = True ) ,
' type ' : fields . char ( ' Report Type ' , size = 32 , required = True ) ,
2008-08-21 13:49:55 +00:00
' model ' : fields . char ( ' Object ' , size = 64 , required = True ) ,
2008-07-22 14:24:36 +00:00
' report_id ' : fields . integer ( ' Report Ref. ' , required = True ) ,
' usage ' : fields . char ( ' Action Usage ' , size = 32 ) ,
' multi ' : fields . boolean ( ' On multiple doc. ' , help = " If set to true, the action will not be displayed on the right toolbar of a form views. " )
}
_defaults = {
' multi ' : lambda * a : False ,
' type ' : lambda * a : ' ir.actions.report.custom ' ,
}
2006-12-07 13:41:40 +00:00
report_custom ( )
class report_xml ( osv . osv ) :
2007-09-07 14:02:45 +00:00
2008-07-22 14:24:36 +00:00
def _report_content ( self , cursor , user , ids , name , arg , context = None ) :
res = { }
for report in self . browse ( cursor , user , ids , context = context ) :
data = report [ name + ' _data ' ]
if not data and report [ name [ : - 8 ] ] :
try :
fp = tools . file_open ( report [ name [ : - 8 ] ] , mode = ' rb ' )
data = fp . read ( )
except :
data = False
res [ report . id ] = data
return res
2007-09-07 14:02:45 +00:00
2008-07-22 14:24:36 +00:00
def _report_content_inv ( self , cursor , user , id , name , value , arg , context = None ) :
self . write ( cursor , user , id , { name + ' _data ' : value } , context = context )
2007-09-07 14:02:45 +00:00
2008-07-22 14:24:36 +00:00
def _report_sxw ( self , cursor , user , ids , name , arg , context = None ) :
res = { }
for report in self . browse ( cursor , user , ids , context = context ) :
if report . report_rml :
res [ report . id ] = report . report_rml . replace ( ' .rml ' , ' .sxw ' )
else :
res [ report . id ] = False
return res
2007-09-07 14:02:45 +00:00
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.report.xml '
_table = ' ir_act_report_xml '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True , translate = True ) ,
' type ' : fields . char ( ' Report Type ' , size = 32 , required = True ) ,
2008-08-21 13:49:55 +00:00
' model ' : fields . char ( ' Object ' , size = 64 , required = True ) ,
2008-07-22 14:24:36 +00:00
' report_name ' : fields . char ( ' Internal Name ' , size = 64 , required = True ) ,
' report_xsl ' : fields . char ( ' XSL path ' , size = 256 ) ,
' report_xml ' : fields . char ( ' XML path ' , size = 256 ) ,
' report_rml ' : fields . char ( ' RML path ' , size = 256 ,
help = " The .rml path of the file or NULL if the content is in report_rml_content " ) ,
' report_sxw ' : fields . function ( _report_sxw , method = True , type = ' char ' ,
string = ' SXW path ' ) ,
' report_sxw_content_data ' : fields . binary ( ' SXW content ' ) ,
' report_rml_content_data ' : fields . binary ( ' RML content ' ) ,
' report_sxw_content ' : fields . function ( _report_content ,
fnct_inv = _report_content_inv , method = True ,
type = ' binary ' , string = ' SXW content ' , ) ,
' report_rml_content ' : fields . function ( _report_content ,
fnct_inv = _report_content_inv , method = True ,
type = ' binary ' , string = ' RML content ' ) ,
' auto ' : fields . boolean ( ' Automatic XSL:RML ' , required = True ) ,
' usage ' : fields . char ( ' Action Usage ' , size = 32 ) ,
' header ' : fields . boolean ( ' Add RML header ' ,
help = " Add or not the coporate RML header " ) ,
' multi ' : fields . boolean ( ' On multiple doc. ' ,
help = " If set to true, the action will not be displayed on the right toolbar of a form views. " ) ,
' report_type ' : fields . selection ( [
( ' pdf ' , ' pdf ' ) ,
( ' html ' , ' html ' ) ,
( ' raw ' , ' raw ' ) ,
( ' sxw ' , ' sxw ' ) ,
] , string = ' Type ' , required = True ) ,
2008-09-04 00:22:48 +00:00
' groups_id ' : fields . many2many ( ' res.groups ' , ' res_groups_report_rel ' , ' uid ' , ' gid ' , ' Groups ' ) ,
' attachment ' : fields . char ( ' Save As Attachment Prefix ' , size = 32 , help = ' This is the prefix of the file name the print will be saved as attachement. Keep empty to not save the printed reports ' )
2008-07-22 14:24:36 +00:00
}
_defaults = {
' type ' : lambda * a : ' ir.actions.report.xml ' ,
' multi ' : lambda * a : False ,
' auto ' : lambda * a : True ,
' header ' : lambda * a : True ,
' report_sxw_content ' : lambda * a : False ,
' report_type ' : lambda * a : ' pdf ' ,
2008-09-04 00:22:48 +00:00
' attachment ' : lambda * a : False ,
2008-07-22 14:24:36 +00:00
}
2007-07-31 14:08:58 +00:00
2006-12-07 13:41:40 +00:00
report_xml ( )
class act_window ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.act_window '
_table = ' ir_act_window '
_sequence = ' ir_actions_id_seq '
2007-05-15 13:27:39 +00:00
2008-09-18 14:02:05 +00:00
# def search(self, cr, uid, args, offset=0, limit=2000, order=None,
# context=None, count=False):
# if context is None:
# context = {}
# ids = osv.orm.orm.search(self, cr, uid, args, offset, limit, order,
# context=context)
# if uid==1:
# return ids
# user_groups = self.pool.get('res.users').read(cr, uid, [uid])[0]['groups_id']
# result = []
# for act in self.browse(cr, uid, ids):
# if not len(act.groups_id):
# result.append(act.id)
# continue
# for g in act.groups_id:
# if g.id in user_groups:
# result.append(act.id)
# break
# return result
2008-07-22 14:24:36 +00:00
def _views_get_fnc ( self , cr , uid , ids , name , arg , context = { } ) :
res = { }
for act in self . browse ( cr , uid , ids ) :
res [ act . id ] = [ ( view . view_id . id , view . view_mode ) for view in act . view_ids ]
2008-11-17 16:56:01 +00:00
modes = act . view_mode . split ( ' , ' )
if len ( modes ) > len ( act . view_ids ) :
2008-07-22 14:24:36 +00:00
find = False
2008-09-09 13:22:10 +00:00
if act . view_id :
2008-07-22 14:24:36 +00:00
res [ act . id ] . append ( ( act . view_id . id , act . view_id . type ) )
2008-11-17 16:56:01 +00:00
for t in modes [ len ( act . view_ids ) : ] :
2008-07-22 14:24:36 +00:00
if act . view_id and ( t == act . view_id . type ) and not find :
find = True
continue
res [ act . id ] . append ( ( False , t ) )
return res
2007-05-15 13:27:39 +00:00
2008-07-22 14:24:36 +00:00
_columns = {
' name ' : fields . char ( ' Action Name ' , size = 64 , translate = True ) ,
' type ' : fields . char ( ' Action Type ' , size = 32 , required = True ) ,
' view_id ' : fields . many2one ( ' ir.ui.view ' , ' View Ref. ' , ondelete = ' cascade ' ) ,
' domain ' : fields . char ( ' Domain Value ' , size = 250 ) ,
' context ' : fields . char ( ' Context Value ' , size = 250 ) ,
2008-08-21 13:49:55 +00:00
' res_model ' : fields . char ( ' Object ' , size = 64 ) ,
' src_model ' : fields . char ( ' Source Object ' , size = 64 ) ,
2008-07-22 14:24:36 +00:00
' target ' : fields . selection ( [ ( ' current ' , ' Current Window ' ) , ( ' new ' , ' New Window ' ) ] , ' Target Window ' ) ,
' view_type ' : fields . selection ( ( ( ' tree ' , ' Tree ' ) , ( ' form ' , ' Form ' ) ) , string = ' Type of view ' ) ,
' view_mode ' : fields . char ( ' Mode of view ' , size = 250 ) ,
' usage ' : fields . char ( ' Action Usage ' , size = 32 ) ,
' view_ids ' : fields . one2many ( ' ir.actions.act_window.view ' , ' act_window_id ' , ' Views ' ) ,
' views ' : fields . function ( _views_get_fnc , method = True , type = ' binary ' , string = ' Views ' ) ,
' limit ' : fields . integer ( ' Limit ' , help = ' Default limit for the list view ' ) ,
' auto_refresh ' : fields . integer ( ' Auto-Refresh ' ,
help = ' Add an auto-refresh on the view ' ) ,
2008-09-18 14:02:05 +00:00
' groups_id ' : fields . many2many ( ' res.groups ' , ' ir_act_window_group_rel ' ,
' act_id ' , ' gid ' , ' Groups ' ) ,
2008-07-22 14:24:36 +00:00
}
_defaults = {
' type ' : lambda * a : ' ir.actions.act_window ' ,
' view_type ' : lambda * a : ' form ' ,
' view_mode ' : lambda * a : ' tree,form ' ,
' context ' : lambda * a : ' {} ' ,
' limit ' : lambda * a : 80 ,
' target ' : lambda * a : ' current ' ,
' auto_refresh ' : lambda * a : 0 ,
}
2006-12-07 13:41:40 +00:00
act_window ( )
2007-05-15 13:27:39 +00:00
class act_window_view ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.act_window.view '
_table = ' ir_act_window_view '
_rec_name = ' view_id '
_columns = {
' sequence ' : fields . integer ( ' Sequence ' ) ,
' view_id ' : fields . many2one ( ' ir.ui.view ' , ' View ' ) ,
' view_mode ' : fields . selection ( (
( ' tree ' , ' Tree ' ) ,
( ' form ' , ' Form ' ) ,
( ' graph ' , ' Graph ' ) ,
2008-10-10 12:30:23 +00:00
( ' calendar ' , ' Calendar ' ) ,
( ' gantt ' , ' Gantt ' ) ) , string = ' Type of view ' , required = True ) ,
2008-07-22 14:24:36 +00:00
' act_window_id ' : fields . many2one ( ' ir.actions.act_window ' , ' Action ' , ondelete = ' cascade ' ) ,
' multi ' : fields . boolean ( ' On multiple doc. ' ,
help = " If set to true, the action will not be displayed on the right toolbar of a form views. " ) ,
}
_defaults = {
' multi ' : lambda * a : False ,
}
_order = ' sequence '
2007-05-15 13:27:39 +00:00
act_window_view ( )
2006-12-07 13:41:40 +00:00
class act_wizard ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.wizard '
_table = ' ir_act_wizard '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Wizard info ' , size = 64 , required = True , translate = True ) ,
' type ' : fields . char ( ' Action type ' , size = 32 , required = True ) ,
' wiz_name ' : fields . char ( ' Wizard name ' , size = 64 , required = True ) ,
' multi ' : fields . boolean ( ' Action on multiple doc. ' , help = " If set to true, the wizard will not be displayed on the right toolbar of a form views. " ) ,
2008-10-08 14:12:08 +00:00
' groups_id ' : fields . many2many ( ' res.groups ' , ' res_groups_wizard_rel ' , ' uid ' , ' gid ' , ' Groups ' ) ,
' model ' : fields . char ( ' Object ' , size = 64 ) ,
2008-07-22 14:24:36 +00:00
}
_defaults = {
' type ' : lambda * a : ' ir.actions.wizard ' ,
' multi ' : lambda * a : False ,
}
2006-12-07 13:41:40 +00:00
act_wizard ( )
2007-10-25 18:20:12 +00:00
class act_url ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.url '
_table = ' ir_act_url '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Action Name ' , size = 64 , translate = True ) ,
' type ' : fields . char ( ' Action Type ' , size = 32 , required = True ) ,
' url ' : fields . text ( ' Action Url ' , required = True ) ,
' target ' : fields . selection ( (
( ' new ' , ' New Window ' ) ,
( ' self ' , ' This Window ' ) ) ,
' Action Target ' , required = True
)
}
_defaults = {
' type ' : lambda * a : ' ir.actions.act_url ' ,
' target ' : lambda * a : ' new '
}
2007-10-25 18:20:12 +00:00
act_url ( )
2008-08-14 06:00:47 +00:00
def model_get ( self , cr , uid , context = { } ) :
wkf_pool = self . pool . get ( ' workflow ' )
ids = wkf_pool . search ( cr , uid , [ ] )
osvs = wkf_pool . read ( cr , uid , ids , [ ' osv ' ] )
2008-09-18 14:02:05 +00:00
2008-08-14 06:00:47 +00:00
res = [ ]
mpool = self . pool . get ( ' ir.model ' )
for osv in osvs :
model = osv . get ( ' osv ' )
id = mpool . search ( cr , uid , [ ( ' model ' , ' = ' , model ) ] )
name = mpool . read ( cr , uid , id ) [ 0 ] [ ' name ' ]
res . append ( ( model , name ) )
2008-09-18 14:02:05 +00:00
2008-08-14 06:00:47 +00:00
return res
2008-08-19 11:23:03 +00:00
2008-08-20 10:21:43 +00:00
class ir_model_fields ( osv . osv ) :
_inherit = ' ir.model.fields '
2008-10-28 23:15:42 +00:00
_rec_name = ' field_description '
2008-08-20 10:21:43 +00:00
_columns = {
2008-12-01 15:13:51 +00:00
' complete_name ' : fields . char ( ' Complete Name ' , size = 64 , select = 1 ) ,
2008-08-20 10:21:43 +00:00
}
2008-12-12 11:48:54 +00:00
def name_search ( self , cr , uid , name , args = None , operator = ' ilike ' , context = None , limit = 800 ) :
2008-08-20 10:21:43 +00:00
def get_fields ( cr , uid , field , rel ) :
result = [ ]
mobj = self . pool . get ( ' ir.model ' )
id = mobj . search ( cr , uid , [ ( ' model ' , ' = ' , rel ) ] )
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
obj = self . pool . get ( ' ir.model.fields ' )
ids = obj . search ( cr , uid , [ ( ' model_id ' , ' in ' , id ) ] )
records = obj . read ( cr , uid , ids )
for record in records :
id = record [ ' id ' ]
fld = field + ' / ' + record [ ' name ' ]
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
result . append ( ( id , fld ) )
return result
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
if not args :
args = [ ]
if not context :
context = { }
return super ( ir_model_fields , self ) . name_search ( cr , uid , name , args , operator , context , limit )
2008-09-18 14:02:05 +00:00
2008-08-20 10:32:48 +00:00
if context . get ( ' key ' ) != ' server_action ' :
return super ( ir_model_fields , self ) . name_search ( cr , uid , name , args , operator , context , limit )
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
result = [ ]
obj = self . pool . get ( ' ir.model.fields ' )
ids = obj . search ( cr , uid , args )
records = obj . read ( cr , uid , ids )
for record in records :
id = record [ ' id ' ]
field = record [ ' name ' ]
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
if record [ ' ttype ' ] == ' many2one ' :
rel = record [ ' relation ' ]
res = get_fields ( cr , uid , field , record [ ' relation ' ] )
for rs in res :
result . append ( rs )
result . append ( ( id , field ) )
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
for rs in result :
obj . write ( cr , uid , [ rs [ 0 ] ] , { ' complete_name ' : rs [ 1 ] } )
2008-09-18 14:02:05 +00:00
2008-08-20 10:24:11 +00:00
iids = [ ]
for rs in result :
iids . append ( rs [ 0 ] )
2008-09-18 14:02:05 +00:00
2008-08-20 10:24:11 +00:00
result = super ( ir_model_fields , self ) . name_search ( cr , uid , name , [ ( ' complete_name ' , ' ilike ' , name ) , ( ' id ' , ' in ' , iids ) ] , operator , context , limit )
2008-09-18 14:02:05 +00:00
2008-08-20 10:21:43 +00:00
return result
ir_model_fields ( )
2008-08-20 11:15:20 +00:00
2008-08-25 10:55:15 +00:00
class server_object_lines ( osv . osv ) :
_name = ' ir.server.object.lines '
_sequence = ' ir_actions_id_seq '
_columns = {
' server_id ' : fields . many2one ( ' ir.actions.server ' , ' Object Mapping ' ) ,
' col1 ' : fields . many2one ( ' ir.model.fields ' , ' Destination ' , required = True ) ,
' value ' : fields . text ( ' Value ' , required = True ) ,
' type ' : fields . selection ( [
( ' value ' , ' Value ' ) ,
( ' equation ' , ' Formula ' )
] , ' Type ' , required = True , size = 32 , change_default = True ) ,
}
_defaults = {
' type ' : lambda * a : ' equation ' ,
}
server_object_lines ( )
2008-08-19 12:30:13 +00:00
##
2008-06-15 00:25:47 +00:00
# Actions that are run on the server side
#
class actions_server ( osv . osv ) :
2008-12-12 06:48:19 +00:00
def _select_signals ( self , cr , uid , context = { } ) :
cr . execute ( " select distinct t.signal as key, t.signal || ' - [ ' || w.osv || ' ] ' as val from wkf w, wkf_activity a, wkf_transition t " \
" where w.id = a.wkf_id " \
" and t.act_from = a.wkf_id " \
" or t.act_to = a.wkf_id " )
return cr . fetchall ( )
def on_trigger_obj_id ( self , cr , uid , ids , context = { } ) :
cr . execute ( " select distinct t.signal as key, t.signal as val from wkf w, wkf_activity a, wkf_transition t " \
" where w.id = a.wkf_id " \
" and t.act_from = a.wkf_id " \
" or t.act_to = a.wkf_id " \
" and w.osv = %s " , ( ' account.invoice ' ) )
data = cr . fetchall ( )
return { " values " : { ' trigger_name ' : data } }
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.server '
_table = ' ir_act_server '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Action Name ' , required = True , size = 64 ) ,
' state ' : fields . selection ( [
2008-12-04 23:58:43 +00:00
( ' client_action ' , ' Client Action ' ) ,
2008-07-22 14:24:36 +00:00
( ' dummy ' , ' Dummy ' ) ,
( ' trigger ' , ' Trigger ' ) ,
( ' email ' , ' Email ' ) ,
( ' sms ' , ' SMS ' ) ,
( ' object_create ' , ' Create Object ' ) ,
( ' object_write ' , ' Write Object ' ) ,
( ' other ' , ' Others Actions ' ) ,
2008-12-04 23:58:43 +00:00
] , ' Action State ' , required = True , size = 32 ) ,
2008-07-22 14:24:36 +00:00
' code ' : fields . text ( ' Python Code ' ) ,
' sequence ' : fields . integer ( ' Sequence ' ) ,
2008-08-21 13:49:55 +00:00
' model_id ' : fields . many2one ( ' ir.model ' , ' Object ' , required = True ) ,
2008-12-04 23:58:43 +00:00
' action_id ' : fields . many2one ( ' ir.actions.actions ' , ' Client Action ' ) ,
2008-12-12 06:48:19 +00:00
' trigger_name ' : fields . selection ( _select_signals , string = ' Trigger Name ' , size = 128 ) ,
2008-12-12 11:48:54 +00:00
' wkf_model_id ' : fields . many2one ( ' ir.model ' , ' Workflow on ' ) ,
2008-12-12 06:48:19 +00:00
' trigger_obj_id ' : fields . many2one ( ' ir.model.fields ' , ' Trigger On ' ) ,
2008-12-11 11:32:08 +00:00
' email ' : fields . many2one ( ' ir.model.fields ' , ' Contact ' ) ,
2008-07-22 14:24:36 +00:00
' message ' : fields . text ( ' Message ' , translate = True ) ,
2008-12-11 11:32:08 +00:00
' mobile ' : fields . many2one ( ' ir.model.fields ' , ' Contact ' ) ,
2008-09-10 12:55:55 +00:00
' sms ' : fields . char ( ' SMS ' , size = 160 , translate = True ) ,
2008-12-12 11:48:54 +00:00
' child_ids ' : fields . many2many ( ' ir.actions.server ' , ' rel_server_actions ' , ' server_id ' , ' action_id ' , ' Others Actions ' ) ,
2008-07-22 14:24:36 +00:00
' usage ' : fields . char ( ' Action Usage ' , size = 32 ) ,
' type ' : fields . char ( ' Report Type ' , size = 32 , required = True ) ,
2008-12-12 05:07:38 +00:00
' srcmodel_id ' : fields . many2one ( ' ir.model ' , ' Model ' , help = " In which object you want to create / write the object if its empty refer to the Object field " ) ,
2008-08-25 10:55:15 +00:00
' fields_lines ' : fields . one2many ( ' ir.server.object.lines ' , ' server_id ' , ' Fields Mapping ' ) ,
2008-12-12 05:07:38 +00:00
' record_id ' : fields . many2one ( ' ir.model.fields ' , ' Record Id ' , help = " privide the field name from where the record id refers, if its empty it will refer to the active id of the object " )
2008-07-22 14:24:36 +00:00
}
_defaults = {
' state ' : lambda * a : ' dummy ' ,
' type ' : lambda * a : ' ir.actions.server ' ,
' sequence ' : lambda * a : 0 ,
' code ' : lambda * a : """ # You can use the following variables
2008-06-15 00:25:47 +00:00
# - object
# - object2
# - time
# - cr
# - uid
# - ids
# If you plan to return an action, assign: action = {...}
2008-09-01 11:48:01 +00:00
""" ,
2008-07-22 14:24:36 +00:00
}
2008-09-18 14:02:05 +00:00
2008-12-11 11:32:08 +00:00
def get_email ( self , cr , uid , action , context ) :
2008-08-21 09:04:40 +00:00
obj_pool = self . pool . get ( action . model_id . model )
id = context . get ( ' active_id ' )
obj = obj_pool . browse ( cr , uid , id )
2008-09-18 14:02:05 +00:00
2008-08-21 10:36:17 +00:00
fields = None
2008-09-18 14:02:05 +00:00
2008-12-11 11:32:08 +00:00
if ' / ' in action . email . complete_name :
fields = action . email . complete_name . split ( ' / ' )
elif ' . ' in action . email . complete_name :
fields = action . email . complete_name . split ( ' . ' )
for field in fields :
try :
obj = getattr ( obj , field )
except Exception , e :
logger . notifyChannel ( ' Workflow ' , netsvc . LOG_ERROR , ' Failed to parse : %s ' % ( match . group ( ) ) )
return obj
def get_mobile ( self , cr , uid , action , context ) :
obj_pool = self . pool . get ( action . model_id . model )
id = context . get ( ' active_id ' )
obj = obj_pool . browse ( cr , uid , id )
fields = None
if ' / ' in action . mobile . complete_name :
fields = action . mobile . complete_name . split ( ' / ' )
elif ' . ' in action . mobile . complete_name :
fields = action . mobile . complete_name . split ( ' . ' )
2008-09-18 14:02:05 +00:00
2008-08-21 09:04:40 +00:00
for field in fields :
2008-08-21 10:36:17 +00:00
try :
obj = getattr ( obj , field )
except Exception , e :
logger . notifyChannel ( ' Workflow ' , netsvc . LOG_ERROR , ' Failed to parse : %s ' % ( match . group ( ) ) )
2008-09-18 14:02:05 +00:00
2008-08-21 09:04:40 +00:00
return obj
2008-08-14 06:00:47 +00:00
2008-08-25 13:11:10 +00:00
def merge_message ( self , cr , uid , keystr , action , context ) :
2008-08-21 10:36:17 +00:00
logger = netsvc . Logger ( )
def merge ( match ) :
obj_pool = self . pool . get ( action . model_id . model )
id = context . get ( ' active_id ' )
obj = obj_pool . browse ( cr , uid , id )
2008-12-11 11:32:08 +00:00
exp = str ( match . group ( ) [ 2 : - 2 ] ) . strip ( )
return eval ( exp , { ' object ' : obj , ' context ' : context , ' time ' : time } )
2008-09-18 14:02:05 +00:00
2008-11-26 23:52:20 +00:00
com = re . compile ( ' ( \ [ \ [.+? \ ] \ ]) ' )
2008-08-25 13:11:10 +00:00
message = com . sub ( merge , keystr )
2008-08-21 10:36:17 +00:00
return message
2008-09-18 14:02:05 +00:00
2008-07-22 14:24:36 +00:00
# Context should contains:
# ids : original ids
# id : current id of the object
# OUT:
# False : Finnished correctly
# ACTION_ID : Action to launch
def run ( self , cr , uid , ids , context = { } ) :
2008-07-23 16:33:28 +00:00
logger = netsvc . Logger ( )
2008-07-22 14:24:36 +00:00
for action in self . browse ( cr , uid , ids , context ) :
2008-12-12 06:48:19 +00:00
2008-12-04 23:58:43 +00:00
if action . state == ' client_action ' :
if not action . action_id :
raise osv . except_osv ( _ ( ' Error ' ) , _ ( " Please specify an action to launch ! " ) )
return self . pool . get ( action . action_id . type ) . read ( cr , uid , action . action_id . id , context = context )
2008-12-12 06:48:19 +00:00
2008-07-22 14:24:36 +00:00
if action . state == ' python ' :
localdict = {
' self ' : self . pool . get ( action . model_id . model ) ,
' context ' : context ,
' time ' : time ,
' ids ' : ids ,
' cr ' : cr ,
' uid ' : uid
}
exec action . code in localdict
if ' action ' in localdict :
return localdict [ ' action ' ]
2008-09-18 14:02:05 +00:00
2008-07-23 16:33:28 +00:00
if action . state == ' email ' :
2008-07-29 07:28:04 +00:00
user = config [ ' email_from ' ]
2008-07-23 16:33:28 +00:00
subject = action . name
2008-12-11 11:32:08 +00:00
address = self . get_email ( cr , uid , action , context )
2008-10-22 11:25:13 +00:00
if not address :
raise osv . except_osv ( _ ( ' Error ' ) , _ ( " Please specify the Partner Email address ! " ) )
2008-12-11 11:32:08 +00:00
if not user :
raise osv . except_osv ( _ ( ' Error ' ) , _ ( " Please specify server option --smtp-from ! " ) )
2008-10-22 11:25:13 +00:00
body = self . merge_message ( cr , uid , str ( action . message ) , action , context )
2008-12-11 13:34:57 +00:00
if tools . email_send ( user , [ address ] , subject , body , debug = False ) == True :
2008-08-20 11:15:20 +00:00
logger . notifyChannel ( ' email ' , netsvc . LOG_INFO , ' Email successfully send to : %s ' % ( address ) )
2008-07-23 16:33:28 +00:00
else :
2008-08-20 11:15:20 +00:00
logger . notifyChannel ( ' email ' , netsvc . LOG_ERROR , ' Failed to send email to : %s ' % ( address ) )
2008-08-14 08:40:46 +00:00
2008-08-14 06:00:47 +00:00
if action . state == ' trigger ' :
2008-08-14 08:40:46 +00:00
wf_service = netsvc . LocalService ( " workflow " )
2008-12-12 11:48:54 +00:00
model = action . wkf_model_id . model
2008-12-12 06:48:19 +00:00
obj_pool = self . pool . get ( action . model_id . model )
res_id = self . pool . get ( action . model_id . model ) . read ( cr , uid , [ context . get ( ' active_id ' ) ] , [ action . trigger_obj_id . name ] )
id = res_id [ 0 ] [ action . trigger_obj_id . name ]
2008-08-14 08:40:46 +00:00
wf_service . trg_validate ( uid , model , int ( id ) , action . trigger_name , cr )
2008-09-18 14:02:05 +00:00
2008-08-06 09:01:54 +00:00
if action . state == ' sms ' :
2008-09-10 07:16:19 +00:00
#TODO: set the user and password from the system
# for the sms gateway user / password
api_id = ' '
text = action . sms
2008-12-11 11:32:08 +00:00
to = self . get_mobile ( cr , uid , action , context )
2008-09-10 07:16:19 +00:00
#TODO: Apply message mearge with the field
2008-08-06 09:01:54 +00:00
if tools . sms_send ( user , password , api_id , text , to ) == True :
logger . notifyChannel ( ' sms ' , netsvc . LOG_INFO , ' SMS successfully send to : %s ' % ( action . address ) )
else :
logger . notifyChannel ( ' sms ' , netsvc . LOG_ERROR , ' Failed to send SMS to : %s ' % ( action . address ) )
2008-12-12 06:48:19 +00:00
2008-08-14 06:00:47 +00:00
if action . state == ' other ' :
2008-12-12 11:48:54 +00:00
res = None
2008-08-14 08:40:46 +00:00
for act in action . child_ids :
2008-12-12 11:48:54 +00:00
result = self . run ( cr , uid , [ act . id ] , context )
if result :
res = result
return res
2008-10-23 05:19:53 +00:00
2008-08-25 13:11:10 +00:00
if action . state == ' object_write ' :
res = { }
for exp in action . fields_lines :
euq = exp . value
if exp . type == ' equation ' :
2008-11-26 23:52:20 +00:00
obj_pool = self . pool . get ( action . model_id . model )
obj = obj_pool . browse ( cr , uid , context [ ' active_id ' ] , context = context )
expr = eval ( euq , { ' context ' : context , ' object ' : obj } )
2008-08-25 13:11:10 +00:00
else :
expr = exp . value
res [ exp . col1 . name ] = expr
2008-09-18 14:02:05 +00:00
2008-12-12 05:07:38 +00:00
if not action . record_id :
if not action . srcmodel_id :
obj_pool = self . pool . get ( action . model_id . model )
obj_pool . write ( cr , uid , [ context . get ( ' active_id ' ) ] , res )
else :
obj_pool = self . pool . get ( action . srcmodel_id . model )
obj_pool . write ( cr , uid , [ context . get ( ' active_id ' ) ] , res )
2008-12-11 15:01:47 +00:00
else :
obj_pool = self . pool . get ( action . srcmodel_id . model )
id = self . pool . get ( action . model_id . model ) . read ( cr , uid , [ context . get ( ' active_id ' ) ] , [ action . record_id . name ] )
obj_pool . write ( cr , uid , [ int ( id [ 0 ] [ action . record_id . name ] ) ] , res )
2008-12-12 11:48:54 +00:00
2008-08-25 13:11:10 +00:00
if action . state == ' object_create ' :
2008-08-26 08:51:10 +00:00
res = { }
for exp in action . fields_lines :
euq = exp . value
if exp . type == ' equation ' :
2008-11-26 23:52:20 +00:00
obj_pool = self . pool . get ( action . model_id . model )
obj = obj_pool . browse ( cr , uid , context [ ' active_id ' ] , context = context )
expr = eval ( euq , { ' context ' : context , ' object ' : obj } )
2008-08-26 08:51:10 +00:00
else :
expr = exp . value
res [ exp . col1 . name ] = expr
2008-09-18 14:02:05 +00:00
2008-09-01 11:48:01 +00:00
obj_pool = None
2008-12-11 15:01:47 +00:00
res_id = False
obj_pool = self . pool . get ( action . srcmodel_id . model )
res_id = obj_pool . create ( cr , uid , res )
self . pool . get ( action . model_id . model ) . write ( cr , uid , [ context . get ( ' active_id ' ) ] , { action . record_id . name : res_id } )
2008-09-18 14:02:05 +00:00
2008-07-22 14:24:36 +00:00
return False
2008-06-15 00:25:47 +00:00
actions_server ( )
2008-06-15 14:59:23 +00:00
class act_window_close ( osv . osv ) :
2008-07-22 14:24:36 +00:00
_name = ' ir.actions.act_window_close '
_table = ' ir_actions '
_sequence = ' ir_actions_id_seq '
_columns = {
' name ' : fields . char ( ' Action Name ' , size = 64 , translate = True ) ,
' type ' : fields . char ( ' Action Type ' , size = 32 , required = True ) ,
}
_defaults = {
' type ' : lambda * a : ' ir.actions.act_window_close ' ,
}
2008-06-15 14:59:23 +00:00
act_window_close ( )
2008-09-29 08:49:06 +00:00
# This model use to register action services.
# if action type is 'configure', it will be start on configuration wizard.
# if action type is 'service',
# - if start_type= 'at once', it will be start at one time on start date
# - if start_type='auto', it will be start on auto starting from start date, and stop on stop date
# - if start_type="manual", it will start and stop on manually
class ir_actions_todo ( osv . osv ) :
2008-11-22 00:09:35 +00:00
_name = ' ir.actions.todo '
2008-09-29 08:49:06 +00:00
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True , select = True ) ,
2008-11-30 15:18:23 +00:00
' note ' : fields . text ( ' Text ' , translate = True ) ,
2008-11-22 00:09:35 +00:00
' start_date ' : fields . datetime ( ' Start Date ' ) ,
' end_date ' : fields . datetime ( ' End Date ' ) ,
2008-09-29 08:49:06 +00:00
' action_id ' : fields . many2one ( ' ir.actions.act_window ' , ' Action ' , select = True , required = True , ondelete = ' cascade ' ) ,
' sequence ' : fields . integer ( ' Sequence ' ) ,
2008-11-22 00:09:35 +00:00
' active ' : fields . boolean ( ' Active ' ) ,
' type ' : fields . selection ( [ ( ' configure ' , ' Configure ' ) , ( ' service ' , ' Service ' ) , ( ' other ' , ' Other ' ) ] , string = ' Type ' , required = True ) ,
' start_on ' : fields . selection ( [ ( ' at_once ' , ' At Once ' ) , ( ' auto ' , ' Auto ' ) , ( ' manual ' , ' Manual ' ) ] , string = ' Start On ' ) ,
' groups_id ' : fields . many2many ( ' res.groups ' , ' res_groups_act_todo_rel ' , ' act_todo_id ' , ' group_id ' , ' Groups ' ) ,
' users_id ' : fields . many2many ( ' res.users ' , ' res_users_act_todo_rel ' , ' act_todo_id ' , ' user_id ' , ' Users ' ) ,
2008-09-29 08:49:06 +00:00
' state ' : fields . selection ( [ ( ' open ' , ' Not Started ' ) , ( ' done ' , ' Done ' ) , ( ' skip ' , ' Skipped ' ) , ( ' cancel ' , ' Cancel ' ) ] , string = ' State ' , required = True )
}
_defaults = {
' state ' : lambda * a : ' open ' ,
' sequence ' : lambda * a : 10 ,
2008-11-22 00:09:35 +00:00
' active ' : lambda * a : True ,
' type ' : lambda * a : ' configure '
2008-09-29 08:49:06 +00:00
}
_order = " sequence "
ir_actions_todo ( )
# This model to use run all configuration actions
class ir_actions_configuration_wizard ( osv . osv_memory ) :
_name = ' ir.actions.configuration.wizard '
def next_configuration_action ( self , cr , uid , context = { } ) :
item_obj = self . pool . get ( ' ir.actions.todo ' )
item_ids = item_obj . search ( cr , uid , [ ( ' type ' , ' = ' , ' configure ' ) , ( ' state ' , ' = ' , ' open ' ) , ( ' active ' , ' = ' , True ) ] , limit = 1 , context = context )
if item_ids and len ( item_ids ) :
item = item_obj . browse ( cr , uid , item_ids [ 0 ] , context = context )
return item
return False
def _get_action_name ( self , cr , uid , context = { } ) :
next_action = self . next_configuration_action ( cr , uid , context = context )
if next_action :
return next_action . note
else :
return " Your database is now fully configured. \n \n Click ' Continue ' and enjoy your OpenERP experience... "
return False
def _get_action ( self , cr , uid , context = { } ) :
next_action = self . next_configuration_action ( cr , uid , context = context )
if next_action :
return next_action . id
return False
def _progress_get ( self , cr , uid , context = { } ) :
total = self . pool . get ( ' ir.actions.todo ' ) . search_count ( cr , uid , [ ] , context )
todo = self . pool . get ( ' ir.actions.todo ' ) . search_count ( cr , uid , [ ( ' type ' , ' = ' , ' configure ' ) , ( ' active ' , ' = ' , True ) , ( ' state ' , ' <> ' , ' open ' ) ] , context )
return max ( 5.0 , round ( todo * 100 / total ) )
_columns = {
' name ' : fields . text ( ' Next Wizard ' , readonly = True ) ,
' progress ' : fields . float ( ' Configuration Progress ' , readonly = True ) ,
' item_id ' : fields . many2one ( ' ir.actions.todo ' , ' Next Configuration Wizard ' , invisible = True , readonly = True ) ,
}
_defaults = {
' progress ' : _progress_get ,
' item_id ' : _get_action ,
' name ' : _get_action_name ,
}
2008-11-27 12:17:01 +00:00
def button_next ( self , cr , uid , ids , context = None ) :
user_action = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid )
act_obj = self . pool . get ( user_action . menu_id . type )
action_ids = act_obj . search ( cr , uid , [ ( ' name ' , ' = ' , user_action . menu_id . name ) ] )
action_open = act_obj . browse ( cr , uid , action_ids ) [ 0 ]
if context . get ( ' menu ' , False ) :
return {
' view_type ' : action_open . view_type ,
' view_id ' : action_open . view_id and [ action_open . view_id . id ] or False ,
' res_model ' : action_open . res_model ,
' type ' : action_open . type ,
' domain ' : action_open . domain
}
return { ' type ' : ' ir.actions.act_window_close ' }
2008-09-29 08:49:06 +00:00
def button_skip ( self , cr , uid , ids , context = None ) :
item_obj = self . pool . get ( ' ir.actions.todo ' )
item_id = self . read ( cr , uid , ids ) [ 0 ] [ ' item_id ' ]
if item_id :
item = item_obj . browse ( cr , uid , item_id , context = context )
item_obj . write ( cr , uid , item . id , {
' state ' : ' skip ' ,
} , context = context )
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.actions.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2008-11-27 12:17:01 +00:00
return self . button_next ( cr , uid , ids , context )
2008-09-29 08:49:06 +00:00
def button_continue ( self , cr , uid , ids , context = None ) :
item_obj = self . pool . get ( ' ir.actions.todo ' )
item_id = self . read ( cr , uid , ids ) [ 0 ] [ ' item_id ' ]
if item_id :
item = item_obj . browse ( cr , uid , item_id , context = context )
item_obj . write ( cr , uid , item . id , {
' state ' : ' done ' ,
} , context = context )
return {
' view_mode ' : item . action_id . view_mode ,
' view_type ' : item . action_id . view_type ,
' view_id ' : item . action_id . view_id and [ item . action_id . view_id . id ] or False ,
' res_model ' : item . action_id . res_model ,
' type ' : item . action_id . type ,
' target ' : item . action_id . target ,
}
2008-11-27 12:17:01 +00:00
return self . button_next ( cr , uid , ids , context )
2008-09-29 08:49:06 +00:00
ir_actions_configuration_wizard ( )
2008-10-08 14:12:08 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: