I have following code that is a mixture of both new and old api. I need to change the state variable to false in my method if it satisfies a if condition. I am new to open erp and old api methods.
#!/usr/bin/python
from openerp import api
from openerp.osv import fields, osv
class attendance(osv.osv):
_name = "attendance.analysis"
_description = "attendance analysis"
_columns = {
'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
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'])]