openerp学习笔记 计划动作、计划执行(维护计划)
示例代码:
data/scheduler.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data
noupdate="0">
<record forcecreate="True"
id="ir_cron_dispatch_service_card_scheduler_action"
model="ir.cron">
<field
name="name">自动更新服务卡过期状态</field>
<field eval="True" name="active"
/>
<field name="user_id"
ref="base.user_root"/>
<field
name="interval_number">12</field>
<field
name="interval_type">hours</field>
<field
name="numbercall">-1</field>
<field eval="True"
name="doall"/>
<field eval="'dispatch.service_card'"
name="model"/>
<field eval="'process_change_state'"
name="function"/>
<field eval="'()'"
name="args"/>
<field
name="priority">0</field>
</record>
</data>
</openerp>
service_card.py
#自动更新服务卡过期状态,每天早上自动执行一次
def
process_change_state(self, cr, uid, ids=None,
context=None):
service_card_ids=self.search(cr,uid,[("state","=",'1')],
context=context)
if
service_card_ids:
DATE_FORMAT =
"%Y-%m-%d"
try:
for rec in self.browse(cr, uid, service_card_ids,
context=context):
if rec.remaining_times == 0:
#已用完
self.write(cr, uid, rec.id,
{'state':'2'})
elif rec.due_date < datetime.datetime.strftime(datetime.date.today(),
DATE_FORMAT):
#已过期
self.write(cr, uid, rec.id,
{'state':'3'})
except
Exception:
_logger.exception("更新服务卡过期状态出现异常!(dispatch.service_card.process_change_state)")
return False
backup_data.xml
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data
noupdate="1">
<record forcecreate="True"
id="ir_cron_dispatch_auto_backups_scheduler_action"
model="ir.cron">
<field
name="name">自动备份数据库</field>
<field eval="True" name="active"
/>
<field name="user_id"
ref="base.user_root"/>
<field
name="interval_number">8</field>
<field
name="interval_type">hours</field>
<field
name="numbercall">-1</field>
<field eval="False"
name="doall"/>
<field eval="'db.backup'"
name="model"/>
<field eval="'schedule_backup'"
name="function"/>
<field eval="'()'"
name="args"/>
<field
name="priority">0</field>
</record>
</data>
</openerp>