hr.contract/方法
提前声明:本人是odoo萌新,python也只有基础,欢迎大神指点,有所不足之处还请见谅 此为本萌新学习归纳所用,毕竟有输出才有动力,废话不多说,咱们开始吧:
@api.constrains('employee_id', 'state', 'kanban_state', 'date_start', 'date_end')
def _check_current_contract(self):
""" Two contracts in state [incoming | open | close] cannot overlap """
for contract in self.filtered(lambda c: (c.state not in ['draft', 'cancel'] or c.state == 'draft' and c.kanban_state == 'done') and c.employee_id):
domain = [
('id', '!=', contract.id),
('employee_id', '=', contract.employee_id.id),
'|',
('state', 'in', ['open', 'close']),
'&',
('state', '=', 'draft'),
('kanban_state', '=', 'done') # replaces incoming
]
if not contract.date_end:
start_domain = []
end_domain = ['|', ('date_end', '>=', contract.date_start), ('date_end', '=', False)]
else:
start_domain = [('date_start', '<=', contract.date_end)]
end_domain = ['|', ('date_end', '>', contract.date_start), ('date_end', '=', False)]
domain = expression.AND([domain, start_domain, end_domain])
if self.search_count(domain):
raise ValidationError(_('A
我们继续阅读,便会发现如上的这一大段方法,遇到这种情况,首先不要慌,我们逐步开始阅读其中的逻辑.业务逻辑以后再学习,我们现在先对代码的实现有所认知,知道能干什么,在以后做开发的时候,也能轻车熟路的用套路(粘贴复制)
- @api.constrains(‘xxxx’)(用于评估和检查,当参数字段的值变化时便开始检查,如果检查不通过,则字段值不会发生变化,在前端页面也会抛出一个异常)
- 在修饰器的方法内,我们首先看到的就是for方法
for contract in self.filtered(lambda c: (c.state not in ['draft', 'cancel'] or c.state == 'draft' and c.kanban_state == 'done') and c.employee_id):
套了很多,我们逐步瓦解
- self.filtered()根据里面的domain过滤,只保留()体内给予的值
lambda c: (c.state not in ['draft', 'cancel'] or c.state == 'draft' and c.kanban_state == 'done') and c.employee_id
接下来是一段匿名函数,lambda,不太清楚大家是否懂这个(这是python的基础),就给一些小白解释一下吧
lamdba(固定写法) c(方法参数) :?(默认冒号后面的值都会返回)
也就是说,将c(self)传入进此匿名函数内,将:?后面所得的值尽数返回,
业务逻辑上的理解是这样的:
(状态不是草稿和取消,或者状态是草稿并且看板状态=完成)?且有员工
就是只保留如上限制下的数据,然后再将其遍历.我们接着往下看:
domain = [
('id', '!=', contract.id),
('employee_id', '=', contract.employee_id.id),
'|',
('state', 'in', ['open', 'close']),
'&',
('state', '=', 'draft'),
('kanban_state', '=', 'done') # replaces incoming
]
|