2010-02-10 09:54:34 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
2010-02-02 13:47:02 +00:00
from osv import fields , osv , orm
2010-02-08 10:34:14 +00:00
from tools . translate import _
2010-07-09 12:14:44 +00:00
from datetime import datetime
from datetime import timedelta
2010-08-24 11:20:57 +00:00
from tools . safe_eval import safe_eval
2011-02-09 09:18:48 +00:00
import pooler
2010-06-24 06:17:55 +00:00
import re
import time
import tools
2010-02-02 13:47:02 +00:00
2010-09-09 14:13:28 +00:00
def get_datetime ( date_field ) :
2011-12-27 09:27:00 +00:00
''' Return a datetime from a date string or a datetime string '''
#complete date time if date_field contains only a date
date_split = date_field . split ( ' ' )
if len ( date_split ) == 1 :
date_field = date_split [ 0 ] + " 00:00:00 "
2010-09-09 14:13:28 +00:00
return datetime . strptime ( date_field [ : 19 ] , ' % Y- % m- %d % H: % M: % S ' )
2010-02-02 13:47:02 +00:00
class base_action_rule ( osv . osv ) :
2010-03-25 13:43:25 +00:00
""" Base Action Rules """
2010-02-02 13:47:02 +00:00
_name = ' base.action.rule '
2010-03-25 13:43:25 +00:00
_description = ' Action Rules '
2011-02-09 09:18:48 +00:00
2010-11-19 13:48:01 +00:00
def _state_get ( self , cr , uid , context = None ) :
2010-04-26 13:45:17 +00:00
""" Get State
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param context : A standard dictionary for contextual values """
return self . state_get ( cr , uid , context = context )
2010-11-19 13:48:01 +00:00
def state_get ( self , cr , uid , context = None ) :
2010-04-26 13:45:17 +00:00
""" Get State
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param context : A standard dictionary for contextual values """
2010-05-10 13:07:29 +00:00
return [ ( ' ' , ' ' ) ]
2011-02-09 09:18:48 +00:00
2010-11-19 13:48:01 +00:00
def priority_get ( self , cr , uid , context = None ) :
2010-04-26 13:45:17 +00:00
""" Get Priority
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param context : A standard dictionary for contextual values """
2010-05-10 13:07:29 +00:00
return [ ( ' ' , ' ' ) ]
2010-03-25 13:43:25 +00:00
2010-02-02 13:47:02 +00:00
_columns = {
2011-02-09 09:18:48 +00:00
' name ' : fields . char ( ' Rule Name ' , size = 64 , required = True ) ,
' model_id ' : fields . many2one ( ' ir.model ' , ' Object ' , required = True ) ,
' create_date ' : fields . datetime ( ' Create Date ' , readonly = 1 ) ,
2010-07-06 11:46:25 +00:00
' active ' : fields . boolean ( ' Active ' , help = " If the active field is set to False, \
2011-02-09 09:18:48 +00:00
it will allow you to hide the rule without removing it . " ),
2010-05-10 13:07:29 +00:00
' sequence ' : fields . integer ( ' Sequence ' , help = " Gives the sequence order \
2011-02-09 09:18:48 +00:00
when displaying a list of rules . " ),
2010-04-26 13:45:17 +00:00
' trg_date_type ' : fields . selection ( [
2011-02-09 09:18:48 +00:00
( ' none ' , ' None ' ) ,
( ' create ' , ' Creation Date ' ) ,
( ' action_last ' , ' Last Action Date ' ) ,
( ' date ' , ' Date ' ) ,
( ' deadline ' , ' Deadline ' ) ,
] , ' Trigger Date ' , size = 16 ) ,
2010-05-10 13:07:29 +00:00
' trg_date_range ' : fields . integer ( ' Delay after trigger date ' , \
help = " Delay After Trigger Date, \
specifies you can put a negative number . If you need a delay before the \
2011-02-09 09:18:48 +00:00
trigger date , like sending a reminder 15 minutes before a meeting . " ),
2010-05-10 13:07:29 +00:00
' trg_date_range_type ' : fields . selection ( [ ( ' minutes ' , ' Minutes ' ) , ( ' hour ' , ' Hours ' ) , \
2011-02-09 09:18:48 +00:00
( ' day ' , ' Days ' ) , ( ' month ' , ' Months ' ) ] , ' Delay type ' ) ,
' trg_user_id ' : fields . many2one ( ' res.users ' , ' Responsible ' ) ,
' trg_partner_id ' : fields . many2one ( ' res.partner ' , ' Partner ' ) ,
' trg_partner_categ_id ' : fields . many2one ( ' res.partner.category ' , ' Partner Category ' ) ,
' trg_state_from ' : fields . selection ( _state_get , ' State ' , size = 16 ) ,
' trg_state_to ' : fields . selection ( _state_get , ' Button Pressed ' , size = 16 ) ,
' act_method ' : fields . char ( ' Call Object Method ' , size = 64 ) ,
' act_user_id ' : fields . many2one ( ' res.users ' , ' Set Responsible to ' ) ,
' act_state ' : fields . selection ( _state_get , ' Set State to ' , size = 16 ) ,
2010-06-24 12:55:54 +00:00
' act_email_cc ' : fields . char ( ' Add Watchers (Cc) ' , size = 250 , help = " \
2010-05-10 13:07:29 +00:00
These people will receive a copy of the future communication between partner \
2011-02-09 09:18:48 +00:00
and users by email " ),
2010-05-10 13:07:29 +00:00
' act_remind_partner ' : fields . boolean ( ' Remind Partner ' , help = " Check \
2011-02-09 09:18:48 +00:00
this if you want the rule to send a reminder by email to the partner . " ),
2010-06-24 12:55:54 +00:00
' act_remind_user ' : fields . boolean ( ' Remind Responsible ' , help = " Check \
2011-02-09 09:18:48 +00:00
this if you want the rule to send a reminder by email to the user . " ),
' act_reply_to ' : fields . char ( ' Reply-To ' , size = 64 ) ,
' act_remind_attach ' : fields . boolean ( ' Remind with Attachment ' , help = " Check this if you want that all documents attached to the object be attached to the reminder email sent. " ) ,
2010-06-24 12:55:54 +00:00
' act_mail_to_user ' : fields . boolean ( ' Mail to Responsible ' , help = " Check \
2011-02-09 09:18:48 +00:00
this if you want the rule to send an email to the responsible person . " ),
' act_mail_to_watchers ' : fields . boolean ( ' Mail to Watchers (CC) ' ,
2010-05-10 13:07:29 +00:00
help = " Check this if you want \
2011-02-09 09:18:48 +00:00
the rule to mark CC ( mail to any other person defined in actions ) . " ),
2010-06-24 12:55:54 +00:00
' act_mail_to_email ' : fields . char ( ' Mail to these Emails ' , size = 128 , \
2011-02-09 09:18:48 +00:00
help = " Email-id of the persons whom mail is to be sent " ) ,
' act_mail_body ' : fields . text ( ' Mail body ' , help = " Content of mail " ) ,
2010-07-12 22:38:54 +00:00
' regex_name ' : fields . char ( ' Regex on Resource Name ' , size = 128 , help = " Regular expression for matching name of the resource \
\ne . g . : ' urgent.* ' will search for records having name starting with the string ' urgent ' \
2011-02-09 09:18:48 +00:00
\nNote : This is case sensitive search . " ),
' server_action_id ' : fields . many2one ( ' ir.actions.server ' , ' Server Action ' , help = " Describes the action name. \n eg:on which object which action to be taken on basis of which condition " ) ,
' filter_id ' : fields . many2one ( ' ir.filters ' , ' Filter ' , required = False ) ,
2010-08-19 16:27:50 +00:00
' act_email_from ' : fields . char ( ' Email From ' , size = 64 , required = False ,
help = " Use a python expression to specify the right field on which one than we will use for the ' From ' field of the header " ) ,
2010-09-30 15:29:42 +00:00
' act_email_to ' : fields . char ( ' Email To ' , size = 64 , required = False ,
help = " Use a python expression to specify the right field on which one than we will use for the ' To ' field of the header " ) ,
2010-09-09 13:17:50 +00:00
' last_run ' : fields . datetime ( ' Last Run ' , readonly = 1 ) ,
2010-02-08 10:34:14 +00:00
}
2010-03-25 13:43:25 +00:00
2010-02-08 10:34:14 +00:00
_defaults = {
2011-02-09 09:18:48 +00:00
' active ' : lambda * a : True ,
' trg_date_type ' : lambda * a : ' none ' ,
' trg_date_range_type ' : lambda * a : ' day ' ,
' act_mail_to_user ' : lambda * a : 0 ,
' act_remind_partner ' : lambda * a : 0 ,
' act_remind_user ' : lambda * a : 0 ,
' act_mail_to_watchers ' : lambda * a : 0 ,
2010-02-02 13:47:02 +00:00
}
2011-02-09 09:18:48 +00:00
2010-05-11 08:59:48 +00:00
_order = ' sequence '
2011-02-09 09:18:48 +00:00
2010-07-21 10:01:42 +00:00
def onchange_model_id ( self , cr , uid , ids , name ) :
2010-07-26 09:20:14 +00:00
#This is not a good solution as it will affect the domain only on onchange
2010-07-21 10:01:42 +00:00
res = { ' domain ' : { ' filter_id ' : [ ] } }
if name :
model_name = self . pool . get ( ' ir.model ' ) . read ( cr , uid , [ name ] , [ ' model ' ] )
if model_name :
mod_name = model_name [ 0 ] [ ' model ' ]
res [ ' domain ' ] = { ' filter_id ' : [ ( ' model_id ' , ' = ' , mod_name ) ] }
else :
res [ ' value ' ] = { ' filter_id ' : False }
return res
2011-11-14 11:39:15 +00:00
def post_action ( self , cr , uid , ids , model , context = None ) :
2010-06-24 06:17:55 +00:00
# Searching for action rules
2010-07-06 13:48:05 +00:00
cr . execute ( " SELECT model.model, rule.id FROM base_action_rule rule \
2010-07-26 09:20:14 +00:00
LEFT JOIN ir_model model on ( model . id = rule . model_id ) \
2010-09-09 13:26:26 +00:00
WHERE active " )
2010-06-24 06:17:55 +00:00
res = cr . fetchall ( )
# Check if any rule matching with current object
for obj_name , rule_id in res :
if not ( model == obj_name ) :
2011-11-14 11:39:15 +00:00
continue # TODO add this condition in the WHERE clause above.
2010-06-24 06:17:55 +00:00
else :
obj = self . pool . get ( obj_name )
2010-09-09 13:27:21 +00:00
# If the rule doesn't involve a time condition, run it immediately
# Otherwise we let the scheduler run the action
if self . browse ( cr , uid , rule_id , context = context ) . trg_date_type == ' none ' :
self . _action ( cr , uid , [ rule_id ] , obj . browse ( cr , uid , ids , context = context ) , context = context )
2010-06-24 06:17:55 +00:00
return True
def _create ( self , old_create , model , context = None ) :
2011-11-14 11:39:15 +00:00
"""
Return a wrapper around ` old_create ` calling both ` old_create ` and
` post_action ` , in that order .
"""
def wrapper ( cr , uid , vals , context = context ) :
2011-12-22 14:10:18 +00:00
if context is None :
context = { }
2010-06-24 06:17:55 +00:00
new_id = old_create ( cr , uid , vals , context = context )
if not context . get ( ' action ' ) :
2011-11-14 11:39:15 +00:00
self . post_action ( cr , uid , [ new_id ] , model , context = context )
2010-06-24 06:17:55 +00:00
return new_id
2011-11-14 11:39:15 +00:00
return wrapper
2010-06-24 06:17:55 +00:00
def _write ( self , old_write , model , context = None ) :
2011-11-14 11:39:15 +00:00
"""
Return a wrapper around ` old_write ` calling both ` old_write ` and
` post_action ` , in that order .
"""
def wrapper ( cr , uid , ids , vals , context = context ) :
2011-12-22 14:10:18 +00:00
if context is None :
context = { }
2010-06-24 06:17:55 +00:00
if isinstance ( ids , ( str , int , long ) ) :
ids = [ ids ]
2011-11-14 11:39:15 +00:00
old_write ( cr , uid , ids , vals , context = context )
2010-06-24 06:17:55 +00:00
if not context . get ( ' action ' ) :
2011-11-14 11:39:15 +00:00
self . post_action ( cr , uid , ids , model , context = context )
return True
return wrapper
2010-06-24 06:17:55 +00:00
def _register_hook ( self , cr , uid , ids , context = None ) :
2011-11-14 11:39:15 +00:00
"""
Wrap every ` create ` and ` write ` methods of the models specified by
the rules ( given by ` ids ` ) .
"""
2010-06-24 06:17:55 +00:00
for action_rule in self . browse ( cr , uid , ids , context = context ) :
2010-07-26 09:20:14 +00:00
model = action_rule . model_id . model
2010-07-06 11:46:25 +00:00
obj_pool = self . pool . get ( model )
2010-08-26 14:20:22 +00:00
if not hasattr ( obj_pool , ' base_action_ruled ' ) :
obj_pool . create = self . _create ( obj_pool . create , model , context = context )
obj_pool . write = self . _write ( obj_pool . write , model , context = context )
obj_pool . base_action_ruled = True
2010-06-24 06:17:55 +00:00
2010-08-26 14:20:22 +00:00
return True
2011-11-14 11:39:15 +00:00
2010-06-24 06:17:55 +00:00
def create ( self , cr , uid , vals , context = None ) :
2010-11-19 13:48:01 +00:00
res_id = super ( base_action_rule , self ) . create ( cr , uid , vals , context = context )
2011-02-09 09:18:48 +00:00
self . _register_hook ( cr , uid , [ res_id ] , context = context )
2010-06-24 06:17:55 +00:00
return res_id
2011-02-09 09:18:48 +00:00
2010-06-24 06:17:55 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
2011-11-14 11:39:15 +00:00
super ( base_action_rule , self ) . write ( cr , uid , ids , vals , context = context )
2010-06-24 06:17:55 +00:00
self . _register_hook ( cr , uid , ids , context = context )
2011-11-14 11:39:15 +00:00
return True
2010-06-24 06:17:55 +00:00
def _check ( self , cr , uid , automatic = False , use_new_cursor = False , \
context = None ) :
"""
This Function is call by scheduler .
"""
2010-07-06 11:46:25 +00:00
rule_pool = self . pool . get ( ' base.action.rule ' )
2010-06-24 06:17:55 +00:00
rule_ids = rule_pool . search ( cr , uid , [ ] , context = context )
2010-09-09 13:17:50 +00:00
self . _register_hook ( cr , uid , rule_ids , context = context )
rules = self . browse ( cr , uid , rule_ids , context = context )
for rule in rules :
model = rule . model_id . model
model_pool = self . pool . get ( model )
last_run = False
if rule . last_run :
2010-09-09 14:13:28 +00:00
last_run = get_datetime ( rule . last_run )
2010-09-09 13:17:50 +00:00
now = datetime . now ( )
for obj_id in model_pool . search ( cr , uid , [ ] , context = context ) :
obj = model_pool . browse ( cr , uid , obj_id , context = context )
2010-09-09 13:26:50 +00:00
# Calculate when this action should next occur for this object
2010-09-09 13:17:50 +00:00
base = False
if rule . trg_date_type == ' create ' and hasattr ( obj , ' create_date ' ) :
2010-09-09 14:25:49 +00:00
base = obj . create_date
2010-09-09 14:27:08 +00:00
elif ( rule . trg_date_type == ' action_last '
and hasattr ( obj , ' create_date ' ) ) :
2010-09-09 13:17:50 +00:00
if hasattr ( obj , ' date_action_last ' ) and obj . date_action_last :
2010-09-09 14:25:49 +00:00
base = obj . date_action_last
2010-09-09 13:17:50 +00:00
else :
2010-09-09 14:25:49 +00:00
base = obj . create_date
2010-09-09 14:27:08 +00:00
elif ( rule . trg_date_type == ' deadline '
and hasattr ( obj , ' date_deadline ' )
and obj . date_deadline ) :
2010-09-09 14:25:49 +00:00
base = obj . date_deadline
2010-09-09 14:27:08 +00:00
elif ( rule . trg_date_type == ' date '
and hasattr ( obj , ' date ' )
and obj . date ) :
2010-09-09 14:25:49 +00:00
base = obj . date
2010-09-09 13:17:50 +00:00
if base :
fnct = {
2010-09-09 14:27:08 +00:00
' minutes ' : lambda interval : timedelta ( minutes = interval ) ,
' day ' : lambda interval : timedelta ( days = interval ) ,
' hour ' : lambda interval : timedelta ( hours = interval ) ,
' month ' : lambda interval : timedelta ( months = interval ) ,
2010-09-09 13:17:50 +00:00
}
2010-09-09 14:25:49 +00:00
base = get_datetime ( base )
2010-09-09 14:28:09 +00:00
delay = fnct [ rule . trg_date_range_type ] ( rule . trg_date_range )
action_date = base + delay
2010-09-09 14:28:41 +00:00
if ( not last_run or ( last_run < = action_date < now ) ) :
self . _action ( cr , uid , [ rule . id ] , [ obj ] , context = context )
2010-09-09 14:27:08 +00:00
rule_pool . write ( cr , uid , [ rule . id ] , { ' last_run ' : now } ,
context = context )
2010-06-24 06:17:55 +00:00
2010-02-15 14:42:24 +00:00
def format_body ( self , body ) :
2010-03-25 13:43:25 +00:00
""" Foramat Action rule ' s body
@param self : The object pointer """
2010-02-15 14:42:24 +00:00
return body and tools . ustr ( body ) or ' '
def format_mail ( self , obj , body ) :
data = {
2011-02-09 09:18:48 +00:00
' object_id ' : obj . id ,
' object_subject ' : hasattr ( obj , ' name ' ) and obj . name or False ,
' object_date ' : hasattr ( obj , ' date ' ) and obj . date or False ,
' object_description ' : hasattr ( obj , ' description ' ) and obj . description or False ,
' object_user ' : hasattr ( obj , ' user_id ' ) and ( obj . user_id and obj . user_id . name ) or ' / ' ,
2010-03-25 13:43:25 +00:00
' object_user_email ' : hasattr ( obj , ' user_id ' ) and ( obj . user_id and \
2011-08-03 10:06:02 +00:00
obj . user_id . user_email ) or ' / ' ,
' object_user_phone ' : hasattr ( obj , ' partner_address_id ' ) and ( obj . partner_address_id and \
obj . partner_address_id . phone ) or ' / ' ,
2011-02-09 09:18:48 +00:00
' partner ' : hasattr ( obj , ' partner_id ' ) and ( obj . partner_id and obj . partner_id . name ) or ' / ' ,
2010-03-25 13:43:25 +00:00
' partner_email ' : hasattr ( obj , ' partner_address_id ' ) and ( obj . partner_address_id and \
2011-02-09 09:18:48 +00:00
obj . partner_address_id . email ) or ' / ' ,
2010-02-15 14:42:24 +00:00
}
2010-02-17 13:40:45 +00:00
return self . format_body ( body % data )
2010-02-15 14:42:24 +00:00
2010-08-19 16:27:50 +00:00
def email_send ( self , cr , uid , obj , emails , body , emailfrom = None , context = None ) :
2010-03-25 13:43:25 +00:00
""" send email
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param email : pass the emails
@param emailfrom : Pass name the email From else False
@param context : A standard dictionary for contextual values """
2010-08-19 16:27:50 +00:00
if not emailfrom :
emailfrom = tools . config . get ( ' email_from ' , False )
if context is None :
context = { }
2011-08-22 17:16:59 +00:00
mail_message = self . pool . get ( ' mail.message ' )
2010-02-15 14:42:24 +00:00
body = self . format_mail ( obj , body )
if not emailfrom :
2011-08-03 10:06:02 +00:00
if hasattr ( obj , ' user_id ' ) and obj . user_id and obj . user_id . user_email :
2011-07-28 06:11:56 +00:00
emailfrom = obj . user_id . user_email
2010-03-25 13:43:25 +00:00
2010-02-15 14:42:24 +00:00
name = ' [ %d ] %s ' % ( obj . id , tools . ustr ( obj . name ) )
emailfrom = tools . ustr ( emailfrom )
2010-03-25 13:43:25 +00:00
reply_to = emailfrom
2010-02-15 14:42:24 +00:00
if not emailfrom :
2011-02-09 09:18:48 +00:00
raise osv . except_osv ( _ ( ' Error! ' ) ,
2010-02-15 14:42:24 +00:00
_ ( " No E-Mail ID Found for your Company address! " ) )
2011-08-22 17:16:59 +00:00
return mail_message . schedule_with_attach ( cr , uid , emailfrom , emails , name , body , model = ' base.action.rule ' , reply_to = reply_to , res_id = obj . id )
2010-02-15 14:42:24 +00:00
2010-03-25 13:43:25 +00:00
2010-08-24 11:20:57 +00:00
def do_check ( self , cr , uid , action , obj , context = None ) :
2010-03-25 13:43:25 +00:00
""" check Action
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param context : A standard dictionary for contextual values """
2010-08-24 11:20:57 +00:00
if context is None :
context = { }
2011-02-09 09:18:48 +00:00
ok = True
2010-07-10 10:55:48 +00:00
if action . filter_id :
2010-07-26 09:20:14 +00:00
if action . model_id . model == action . filter_id . model_id :
2010-07-10 10:55:48 +00:00
context . update ( eval ( action . filter_id . context ) )
obj_ids = obj . _table . search ( cr , uid , eval ( action . filter_id . domain ) , context = context )
if not obj . id in obj_ids :
ok = False
else :
2010-05-11 08:59:48 +00:00
ok = False
2010-08-24 11:20:57 +00:00
if getattr ( obj , ' user_id ' , False ) :
2010-02-15 14:42:24 +00:00
ok = ok and ( not action . trg_user_id . id or action . trg_user_id . id == obj . user_id . id )
2010-08-24 11:20:57 +00:00
if getattr ( obj , ' partner_id ' , False ) :
2010-03-25 13:43:25 +00:00
ok = ok and ( not action . trg_partner_id . id or action . trg_partner_id . id == obj . partner_id . id )
2010-02-15 14:42:24 +00:00
ok = ok and (
not action . trg_partner_categ_id . id or
(
obj . partner_id . id and
( action . trg_partner_categ_id . id in map ( lambda x : x . id , obj . partner_id . category_id or [ ] ) )
)
)
state_to = context . get ( ' state_to ' , False )
2010-08-24 11:20:57 +00:00
state = getattr ( obj , ' state ' , False )
if state :
ok = ok and ( not action . trg_state_from or action . trg_state_from == state )
2010-02-15 14:42:24 +00:00
if state_to :
ok = ok and ( not action . trg_state_to or action . trg_state_to == state_to )
2010-07-26 10:59:49 +00:00
elif action . trg_state_to :
ok = False
2010-02-15 14:42:24 +00:00
reg_name = action . regex_name
result_name = True
if reg_name :
ptrn = re . compile ( str ( reg_name ) )
_result = ptrn . search ( str ( obj . name ) )
if not _result :
result_name = False
regex_n = not reg_name or result_name
ok = ok and regex_n
return ok
2010-08-24 11:20:57 +00:00
def do_action ( self , cr , uid , action , model_obj , obj , context = None ) :
2010-03-25 13:43:25 +00:00
""" Do Action
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param action : pass action
@param model_obj : pass Model object
@param context : A standard dictionary for contextual values """
2010-08-24 11:20:57 +00:00
if context is None :
context = { }
2010-03-25 13:43:25 +00:00
2010-02-15 14:42:24 +00:00
if action . server_action_id :
2010-05-10 13:07:29 +00:00
context . update ( { ' active_id ' : obj . id , ' active_ids ' : [ obj . id ] } )
2010-02-15 14:42:24 +00:00
self . pool . get ( ' ir.actions.server ' ) . run ( cr , uid , [ action . server_action_id . id ] , context )
2010-03-25 13:43:25 +00:00
write = { }
2010-06-24 06:17:55 +00:00
2010-02-15 14:42:24 +00:00
if hasattr ( obj , ' user_id ' ) and action . act_user_id :
obj . user_id = action . act_user_id
write [ ' user_id ' ] = action . act_user_id . id
2010-03-25 13:43:25 +00:00
if hasattr ( obj , ' date_action_last ' ) :
2010-02-15 14:42:24 +00:00
write [ ' date_action_last ' ] = time . strftime ( ' % Y- % m- %d % H: % M: % S ' )
if hasattr ( obj , ' state ' ) and action . act_state :
obj . state = action . act_state
write [ ' state ' ] = action . act_state
2010-03-03 05:57:06 +00:00
if hasattr ( obj , ' categ_id ' ) and action . act_categ_id :
obj . categ_id = action . act_categ_id
write [ ' categ_id ' ] = action . act_categ_id . id
2010-02-15 14:42:24 +00:00
model_obj . write ( cr , uid , [ obj . id ] , write , context )
2010-03-25 13:43:25 +00:00
2010-02-15 14:42:24 +00:00
if hasattr ( model_obj , ' remind_user ' ) and action . act_remind_user :
model_obj . remind_user ( cr , uid , [ obj . id ] , context , attach = action . act_remind_attach )
if hasattr ( model_obj , ' remind_partner ' ) and action . act_remind_partner :
model_obj . remind_partner ( cr , uid , [ obj . id ] , context , attach = action . act_remind_attach )
if action . act_method :
getattr ( model_obj , ' act_method ' ) ( cr , uid , [ obj . id ] , action , context )
2010-08-24 11:20:57 +00:00
2010-02-15 14:42:24 +00:00
emails = [ ]
if hasattr ( obj , ' user_id ' ) and action . act_mail_to_user :
2011-07-28 06:11:56 +00:00
if obj . user_id :
emails . append ( obj . user_id . user_email )
2010-03-25 13:43:25 +00:00
2010-02-15 14:42:24 +00:00
if action . act_mail_to_watchers :
emails + = ( action . act_email_cc or ' ' ) . split ( ' , ' )
if action . act_mail_to_email :
emails + = ( action . act_mail_to_email or ' ' ) . split ( ' , ' )
2010-09-30 15:29:42 +00:00
locals_for_emails = {
' user ' : self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context = context ) ,
' obj ' : obj ,
}
if action . act_email_to :
emails . append ( safe_eval ( action . act_email_to , { } , locals_for_emails ) )
2010-02-15 14:42:24 +00:00
emails = filter ( None , emails )
if len ( emails ) and action . act_mail_body :
emails = list ( set ( emails ) )
2010-09-30 15:29:42 +00:00
email_from = safe_eval ( action . act_email_from , { } , locals_for_emails )
2010-09-08 11:36:56 +00:00
def to_email ( text ) :
return re . findall ( r ' ([^ ,<@]+@[^> ,]+) ' , text or ' ' )
emails = to_email ( ' , ' . join ( filter ( None , emails ) ) )
email_froms = to_email ( email_from )
if email_froms :
self . email_send ( cr , uid , obj , emails , action . act_mail_body , emailfrom = email_froms [ 0 ] )
2010-02-08 10:34:14 +00:00
return True
2010-02-15 14:42:24 +00:00
2010-08-24 11:20:57 +00:00
def _action ( self , cr , uid , ids , objects , scrit = None , context = None ) :
2010-03-25 13:43:25 +00:00
""" Do Action
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param ids : List of Basic Action Rule ’ s IDs ,
@param objects : pass objects
@param context : A standard dictionary for contextual values """
2010-08-24 11:20:57 +00:00
if context is None :
context = { }
2010-06-24 06:17:55 +00:00
context . update ( { ' action ' : True } )
2010-02-02 13:47:02 +00:00
if not scrit :
scrit = [ ]
2010-08-24 11:20:57 +00:00
2010-11-19 13:48:01 +00:00
for action in self . browse ( cr , uid , ids , context = context ) :
2010-02-15 14:42:24 +00:00
for obj in objects :
2011-04-22 11:37:06 +00:00
if self . do_check ( cr , uid , action , obj , context = context ) :
model_obj = self . pool . get ( action . model_id . model )
self . do_action ( cr , uid , action , model_obj , obj , context = context )
2010-04-26 13:45:17 +00:00
2010-06-24 06:17:55 +00:00
context . update ( { ' action ' : False } )
2010-02-02 13:47:02 +00:00
return True
2010-03-25 13:43:25 +00:00
2010-02-02 13:47:02 +00:00
def _check_mail ( self , cr , uid , ids , context = None ) :
2010-03-25 13:43:25 +00:00
""" Check Mail
@param self : The object pointer
@param cr : the current row , from the database cursor ,
@param uid : the current user ’ s ID for security checks ,
@param ids : List of Action Rule ’ s IDs
@param context : A standard dictionary for contextual values """
2010-02-15 14:42:24 +00:00
empty = orm . browse_null ( )
rule_obj = self . pool . get ( ' base.action.rule ' )
2010-11-19 13:48:01 +00:00
for rule in self . browse ( cr , uid , ids , context = context ) :
2010-02-02 13:47:02 +00:00
if rule . act_mail_body :
try :
2010-02-15 14:42:24 +00:00
rule_obj . format_mail ( empty , rule . act_mail_body )
2010-02-02 13:47:02 +00:00
except ( ValueError , KeyError , TypeError ) :
return False
return True
2010-03-25 13:43:25 +00:00
2010-02-02 13:47:02 +00:00
_constraints = [
2011-02-09 09:18:48 +00:00
( _check_mail , ' Error: The mail is not well formated ' , [ ' act_mail_body ' ] ) ,
2010-02-02 13:47:02 +00:00
]
2010-03-25 13:43:25 +00:00
2010-04-26 13:45:17 +00:00
base_action_rule ( )
2010-03-25 13:43:25 +00:00
2010-06-24 06:17:55 +00:00
class ir_cron ( osv . osv ) :
2011-06-24 10:24:19 +00:00
_inherit = ' ir.cron '
_init_done = False
2010-06-24 06:17:55 +00:00
def _poolJobs ( self , db_name , check = False ) :
2011-06-24 10:24:19 +00:00
if not self . _init_done :
self . _init_done = True
try :
db = pooler . get_db ( db_name )
except :
return False
cr = db . cursor ( )
try :
next = datetime . now ( ) . strftime ( ' % Y- % m- %d % H:00:00 ' )
# Putting nextcall always less than current time in order to call it every time
cr . execute ( ' UPDATE ir_cron set nextcall = \' %s \' where numbercall<>0 and active and model= \' base.action.rule \' ' % ( next ) )
finally :
cr . commit ( )
cr . close ( )
2010-06-24 06:17:55 +00:00
2010-07-06 16:03:42 +00:00
super ( ir_cron , self ) . _poolJobs ( db_name , check = check )
2010-06-24 06:17:55 +00:00
ir_cron ( )
2010-02-02 13:47:02 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: