Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Assigning value to a variable using a old api method to column type field declaration?

By
inian
on 5/23/16, 6:40 AM 291 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'])]

0
inian
On 5/24/16, 3:59 AM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/23/16, 6:40 AM
Seen: 291 times
Last updated: 5/24/16, 3:59 AM