我有以下代码,它是新旧API的混合体。如果满足if条件,我需要在我的方法中将状态变量更改为false。我是新手打开erp和旧的api方法。
#!/usr/bin/python
来自openerp import api的来自openerp.osv导入字段,osv 来自openerp导入模型
课堂出勤(osv.osv):_name = "attendance.analysis"
_description = "attendance analysis"
EID= fields.integer('Employee ID', required=True),
action= fields.selection([('sign_in', 'Sign In'), ('sign_out', 'Sign Out')], 'Employee Action'),
date= fields.datetime('Employee Date', required=True, select=1),
s2=fields.char("s2", required=True),
state= fields.boolean("state?", default=False)
@api.constrains('s2')
def constrains_action(self):
if self.s2=='0':
self.action='sign_in'
@api.constrains('s2')
def constraint_action(self):
if self.s2 == '1':
self.action = 'sign_out'
@api.constrains('date')
def constrains_date(self):
self.env.cr.execute('DELETE FROM attendance_analysis USING attendance_analysis ua2 WHERE attendance_analysis.date = ua2.date AND attendance_analysis.id < ua2.id')
def _altern_si_so(self, cr, uid, ids, context=None):
""" Alternance sign_in/sign_out check.
Previous (if exists) must be of opposite action.
Next (if exists) must be of opposite action.
"""
for att in self.browse(cr, uid, ids, context=context):
# search and browse for first previous and first next records
prev_att_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '<', att.date),
('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date DESC')
next_add_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '>', att.date),
('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date ASC')
results = self.search(cr, uid, [('state','=',att.state)], limit=1, order='date DESC')
result = self.browse(cr, uid, results, context=context)
prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
next_atts = self.browse(cr, uid, next_add_ids, context=context)
# check for alternance, return False if at least one condition is not satisfied
if prev_atts and prev_atts[0].action == att.action:
# How to assign state == True to change the field value here
print (result,'hellolllllllllllllll')
return False
if next_atts and next_atts[0].action == att.action: # next exists and is same action
return False
if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
return False
return True
_constraints = [
(_altern_si_so, 'Error ! Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]
答案 0 :(得分:0)
我找到了解决方案。我们需要通过返回self.write方法传递布尔变量来改变它的值
def _altern_si_so(self, cr, uid, ids, context=None):
""" Alternance sign_in/sign_out check.
Previous (if exists) must be of opposite action.
Next (if exists) must be of opposite action.
"""
for att in self.browse(cr, uid, ids, context=context):
state = False
# search and browse for first previous and first next records
prev_att_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '<', att.date),
('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date DESC')
next_add_ids = self.search(cr, uid, [('EID', '=', att.EID), ('date', '>', att.date),
('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='date ASC')
results = self.search(cr, uid, [('state','=',att.state)], limit=1, order='date DESC')
result = self.browse(cr, uid, results, context=context)
prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
next_atts = self.browse(cr, uid, next_add_ids, context=context)
# check for alternance, return False if at least one condition is not satisfied
if prev_atts and prev_atts[0].action == att.action:
return self.write(cr, uid, ids, {'state': True})
# How to assign state == True to change the field value here
if next_atts and next_atts[0].action == att.action:# next exists and is same action
return self.write(cr, uid, ids, {'state': True})
if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
return self.write(cr, uid, ids, {'state': True})
return True