gpt4 book ai didi

python - Odoo 10 : Catch the UserError and show it to webpage

转载 作者:行者123 更新时间:2023-12-03 08:46:18 27 4
gpt4 key购买 nike

我有一个 Controller ,它在models.py中调用 action_approve 函数,最后返回一个网页:

class Holiday(http.Controller):
@http.route('/Approve', auth='public',type='http', methods=['GET'], website=True)
def index(self,resid):

record = http.request.env['hr.holidays'].search([('id', '=', resid)])
print str(record)
result=record.action_approve()
return http.request.render('holiday.approve_page')

这是 action_approve 函数,具有 UserError 语句:
@api.multi
def action_approve(self):
# if double_validation: this method is the first approval approval
# if not double_validation: this method calls action_validate() below
print "holiday calleds"
if not self.env.user.has_group('hr_holidays.group_hr_holidays_manager'):
raise UserError(_('Only Leads can approve leave requests.'))
return False
manager = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)

notification=Notification()
notification.notifyEmployee(self,'Approved')

for holiday in self:
if holiday.state != 'confirm':
raise UserError(_('Leave request must be confirmed ("To Approve") in order to approve it.'))
return False
if holiday.double_validation:
return holiday.write({'state': 'validate1', 'manager_id': manager.id if manager else False})
else:
holiday.action_validate()

我想捕获此 UserError 消息,并将其在网页上显示给用户。我怎样才能做到这一点?

最佳答案

此代码有效:

try:
record.action_approve()

except UserError,e:
return http.request.render('holiday.approve_page', {'errorMsg': e[0]})

关于python - Odoo 10 : Catch the UserError and show it to webpage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53121020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com