我有一个控制器,该控制器在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')
这是具有 UserError 语句的 action_approve 函数:
@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 消息并将其在网页上显示给用户。我该怎么办?
答案 0 :(得分:0)
此代码有效:
try:
record.action_approve()
except UserError,e:
return http.request.render('holiday.approve_page', {'errorMsg': e[0]})