Skip to Content
Menu
This question has been flagged
1 Reply
3591 Views

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'])]

Avatar
Discard
Author Best Answer

I found the solution we need to give a write command for doing so


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

Avatar
Discard
Related Posts Replies Views Activity
4
Apr 24
170719
0
Dec 23
602
5
Nov 24
217120
1
Dec 22
1692
2
Nov 22
1678