2017-04-05 127 views
1

在wizzard視圖中,我有一個按鈕在'ir.action.act_url'中返回pdf報告。它工作正常。問題是,在pdf出現後,我想自動關閉wizzard窗口。要做到這一點,我可以返回close_window字典。Odoo8 - 需要一個接一個地執行多個操作的ir.actions.server的例子

另外這兩個'返回'工作正常。

我想執行兩個動作,一個接一個。我發現這可以使用具有多屬性的ir.action.server。

不幸的是我找不到一個例子。

close_window = {'type': 'ir.actions.act_window_close'} 

final_report = { 
    'type': 'ir.actions.act_url', 
    'url': '/web/binary/saveas?model=ir.attachment&field=datas& 
    filename_field=name&id=' + str(file.id), 
    'target': 'self', 
} 

return final_report 

回答

0

檢查ir_actions測試與Odoo出貨,他們也許能幫助您在正確的方向。

基地/測試/ test_ir_actions.py

def test_60_multi(self): 
    cr, uid = self.cr, self.uid 

    # Data: 2 server actions that will be nested 
    act1_id = self.ir_actions_server.create(cr, uid, { 
     'name': 'Subaction1', 
     'sequence': 1, 
     'model_id': self.res_partner_model_id, 
     'state': 'code', 
     'code': 'action = {"type": "ir.actions.act_window"}', 
    }) 
    act2_id = self.ir_actions_server.create(cr, uid, { 
     'name': 'Subaction2', 
     'sequence': 2, 
     'model_id': self.res_partner_model_id, 
     'state': 'object_create', 
     'use_create': 'copy_current', 
    }) 
    act3_id = self.ir_actions_server.create(cr, uid, { 
     'name': 'Subaction3', 
     'sequence': 3, 
     'model_id': self.res_partner_model_id, 
     'state': 'code', 
     'code': 'action = {"type": "ir.actions.act_url"}', 
    }) 
    self.ir_actions_server.write(cr, uid, [self.act_id], { 
     'state': 'multi', 
     'child_ids': [(6, 0, [act1_id, act2_id, act3_id])], 
    }) 

    # Do: run the action 
    res = self.ir_actions_server.run(cr, uid, [self.act_id], context=self.context) 

    # Test: new partner created 
    pids = self.res_partner.search(cr, uid, [('name', 'ilike', 'TestingPartner (copy)')]) # currently res_partner overrides default['name'] whatever its value 
    self.assertEqual(len(pids), 1, 'ir_actions_server: TODO') 
    # Test: action returned 
    self.assertEqual(res.get('type'), 'ir.actions.act_url') 

    # Test loops 
    with self.assertRaises(except_orm): 
     self.ir_actions_server.write(cr, uid, [self.act_id], { 
      'child_ids': [(6, 0, [self.act_id])] 
     }) 

沒有完成它自己,但它看起來像你必須指定,這是一個multi行動,並指定要執行的child_ids行動。

也請記住,根據文檔(https://www.odoo.com/documentation/8.0/reference/actions.html):

執行多個動作一個接一個。要執行的動作是 ,通過child_ids m2m定義。如果子行爲本身返回 動作,最後一個將被返回給客戶端作爲多的 自己的下一步行動

0

根據@dgeorgiev我寫代碼進一步。我設法創建服務器操作,並單獨這個操作能夠返回PDF文件或關閉窗口。但我無法將這兩個回報結合起來。見下:

# first server action returning pdf 
ir_actions_server = self.env['ir.actions.server'] 
act1_id = ir_actions_server.create({ 
    'type': 'ir.actions.server', 
    'name': 'divided_package_labels', 
    'sequence': 1, 
    'model_id': self.id, 
    'state': 'code', 
    'code': 'action = {"type": "ir.actions.act_url", "url": "/web/binary/saveas?model=ir.attachment&field=datas&filename_field=name&id= %s", "target": "new"}' % str(file.id), 
}) 
# second server action closing window 
act2_id = ir_actions_server.create({ 
    'type': 'ir.actions.server', 
    'name': 'Close_sale.package.wizard', 
    'sequence': 2, 
    'model_id': self.id, 
    'state': 'code', 
    'code': 'action = {"type": "ir.actions.act_window_close"}' 
}) 
# server action for combining two previously described 
act_id = ir_actions_server.create({ 
    'type': 'ir.actions.server', 
    'name': 'TestAction', 
    'condition': 'True', 
    'model_id': self.id, 
    'state': 'multi', 
    # 'child_ids': [(6, 0, [act1_id.id])] # return pdf 
    # 'child_ids': [(6, 0, [act2_id.id])] # close window 
    'child_ids': [(6, 0, [act1_id.id, act2_id.id])] # close window, no pdf file 

}) 

print act_id, act1_id, act2_id 
print "act_id.child_ids", act_id.child_ids 
# shows that the relations are properly made 

# run 
return act_id.run() 

可能有一個小錯誤,但我找不到它。

相關問題