如何使Odoo方法仅在特定视图中工作?



>我创建了一个在请求休假时引发验证错误的方法,但我需要这个方法在休假分配请求中不起作用并通过所有验证。 我应该更改 Python 代码以在条件时传递还是应该在 XML 上工作。

蒂亚密码

@api.constrains('state', 'date_from', 'holiday_status_id',)
def _check_hire_date(self):
if self.holiday_type == 'category':
print "hhhhhhhhhhhhhhhhhh"
return
from_dt = fields.Datetime.from_string(self.date_from)
to_dt = fields.Datetime.from_string(self.date_to)
if self.holiday_status_id.is_pass:
return
if self.employee_id.joining_date:
jo_dt = fields.Datetime.from_string(self.employee_id.joining_date)
else:
raise ValidationError("you must define joining date")
if self.date_from or self.date_to:
now = datetime.datetime.now()
now_time_delta = now - to_dt
hire_time_delta = from_dt - jo_dt
diff_time_delta = to_dt - from_dt
if hire_time_delta.days < 180:
raise ValidationError("don’t allowed to take this vacancy before 6 months from hired date")
if self.holiday_status_id.lev_type == 'Casual' and self.number_of_days_temp > 2:
print ('innnnnnnnnnnnn',diff_time_delta.days)
raise ValidationError("don’t allowed to take vacancy more than 2 days")
if self.holiday_status_id.lev_type in ['Sick', 'Casual'] and now_time_delta.days > 3 :
raise ValidationError("don’t allowed to make this vacancy more than 3 days from back to work")
if self.holiday_status_id.lev_type in ['Sick', 'Paternity'] and not self.attachment:
raise ValidationError('You cannot send the leave request without attaching a document.')
if self.holiday_status_id.lev_type == 'Maternity' and hire_time_delta.days < 300 :
raise ValidationError("don’t allowed to take this vacancy before 10 months from hired date")
if self.holiday_status_id.lev_type == 'Pilgrimage' and hire_time_delta.days < 1800 :
raise ValidationError("don’t allowed to take this vacancy before 5 Years from hired date")

我认为最简单的方法是使用context,在您的视图操作中传递一个特殊的key

<!-- in your act_window definition -->
<field name="context">{'check_hire_date': 1}</field>

然后在方法中,如果上下文中存在此键,则开始验证(如果未通过它(:

@api.constrains('state', 'date_from', 'holiday_status_id',)
def _check_hire_date(self):
if not self.env.context.get('check_hire_date', False):
return 
# the rest of your code 

我希望这对你有帮助

最新更新