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

How do I fix KeyError Using Vals?

By
Ademola Oke
on 8/18/13, 11:20 AM 4,382 views

I have this code:

import time
from openerp.osv import fields, osv
import openerp.addons.decimal_precision as dp
from openerp.tools.translate import _    
from openerp.tools import hashlib       
from datetime import datetime          
from operator import itemgetter      
from openerp import SUPERUSER_ID
from openerp import pooler, tools
from dateutil.relativedelta import relativedelta

class account_bank_payment(osv.osv):
    _name = "account.bank.payment"
    _description = 'PayDirect Tax Payments'
    _inherit = 'account.bank.statement.line'
    _columns = {        
        'name': fields.char('Payment Reference', size=255, required=True),
        'date': fields.date('Payment Date', required=True),
        'amount': fields.float('Amount', digits_compute=dp.get_precision('Account'), required=True),                                                 
        'ref': fields.char('Description', size=255, required=True),    
        'taxpayer_name': fields.char('Tax Payer', size=255, required=True),
        'head_subhead': fields.char('Head/Sub-head', size=255, required=True), 
        'instr_number': fields.char('Print Paper Number', size=20), 
        'system_print_number': fields.char('System Print Number', size=20), 
        'bank_name': fields.char('Bank Name', size=255, required=True),     
        'payment_type': fields.char('Payment Type', size=255, required=True),
        'payment_logid': fields.integer('Payment Log Id'),                 
        'terminal_id': fields.char('Terminal Id', size=255),                 
        'channel_name': fields.char('Channel Name', size=255),
        'location': fields.char('Channel Name', size=255),                  
        'institution_id': fields.char('Institution Id', size=255),         
        'institution_name': fields.char('Institution', size=255),
        'branch_name': fields.char('Branch', size=255),   
        'receipt_no': fields.char('Receipt No', size=255),  
        'collections_account': fields.char('Collections Account', size=255),                                                          
        'bank_code': fields.char('Bank Code', size=255),   
        'is_reversal': fields.boolean('Is Reversal', help="Indicates whether the transactions is a reversal or not."),                                                          
        'deposit_slip_number': fields.char('Deposit Slip Number', size=255),              
        'period_covered': fields.char('Period Covered', size=255),  
        'period_covered_start': fields.date('Period Covered Start'),                                      
        'period_covered_end': fields.date('Period Covered End'),
        'val_date': fields.date('Value Date'),  
        'cust_reference': fields.char('Customer Reference', size=255),
        'alternate_cust_reference': fields.char('Alternate Customer Reference', size=255),
        'payment_status': fields.integer('Payment Status'),
        'settlement_date': fields.date('Settlement Date'),
        'fee_name': fields.char('Fee Name', size=255),
        'othercustomer_info': fields.char('Other Customer Info', size=255),
        'collections_account': fields.char('Collections Account', size=255),
        'third_party_code': fields.char('Thirty Party Code', size=255),
        'customer_address': fields.char('Customer Address', size=255),
        'customer_phone_number': fields.char('Customer Phone Number', size=255),
        'depositor_name': fields.char('Depositor Name', size=255),
        'payment_currency': fields.char('Payment Currency', size=255),
        'product_group_code': fields.char('Product Group Code', size=255),
        'transaction_date': fields.date('Transaction Date'),
        'item_amount': fields.float('Item Amount', digits_compute=dp.get_precision('Account')),
        'lead_bank_code': fields.char('Lead Bank Code', size=255),
        'lead_bank_cbn_code': fields.char('Lead Bank CBN Code', size=255),
        'lead_bank_name': fields.char('Lead Bank Name', size=255),
        'ip': fields.char('IP', size=255), 
        #dummy fields for date range
        'payment_date_from':fields.function(lambda *a,**k:{}, method=True, type='date',string="Payment date from"),
        'payment_date_to':fields.function(lambda *a,**k:{}, method=True, type='date',string="Payment date to"),           
        #'status': fields.selection([('ready', 'Ready'), ('printed', 'Printed'), ('issued', 'Issued'), ('request', 'Void Request')], 'State'),   
        'state': fields.selection([('ready', 'Ready'), ('printed', 'Printed'), ('issued', 'Issued'), ('request', 'Void Request'), ('voided', 'Voided')], 'Status'),    
        'wht_schedule_id': fields.one2many('collections.wht.schedule', 'payment_reference', 'WHT Schedule', select=False, required=False, ondelete='cascade'),       
        'partner_id': fields.many2one('res.partner', 'Internal Payer Name'),
        #override the following here to remove required constraint    
        'statement_id': fields.many2one('account.bank.statement', 'Statement',
            select=False, required=False, ondelete='cascade'),
        'type': fields.selection([ 
            ('taxpayer','Tax Payer'),
            ('supplier','Supplier'),
            ('customer','Customer'),
            ('general','General')
            ], 'Type', required=False),
        'account_id': fields.many2one('account.account','Account',
            required=False),          
    }
    _defaults = {
        #'status': 'ready', 
        'state': 'ready',
        'period_covered_start' : lambda *a : None, 
        'period_covered_end' : lambda *a : None,
    }
    _sql_constraints = [
#         ('name_unique', 'unique(name)', 'The payment reference must be unique per payment!'),
#         ('payment_logid_unique', 'unique(payment_logid)', 'The payment log id must be unique per payment!'),
#         ('terminal_id', 'unique(terminal_id)', 'The terminal id must be unique per payment!'), 
         ('instr_number', 'unique(instr_number)', 'The print paper number must be unique for each payment!'),
         ('system_print_number', 'unique(system_print_number)', 'The system print number must be unique for each payment!'),
    ]
    _order = "date desc"

    def unlink(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
#         if context.get('block_statement_line_delete', False):
#             raise osv.except_osv(_('Warning!'), _('Delete operation not allowed. \
#             Please go to the associated bank statement in order to delete and/or modify bank statement line.'))
        return super(account_bank_payment, self).unlink(cr, uid, ids, context=context)

    def set_ready(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'ready'}, context=context)
        return {}

    def set_printed(self, cr, uid, ids, context):
        for payment in self.browse(cr, uid, ids, context): 
            if not payment.instr_number:
                raise osv.except_osv(_('Warning!'), _('You must enter the print paper number before setting the status to printed.'))
            else:
                self.write(cr, uid, ids, {'state': 'printed'}, context=context)
        return {}

    def set_issued(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'issued'}, context=context)
        return {} 

    def set_request(self, cr, uid, ids, context):
        for payment in self.browse(cr, uid, ids, context): 
            if not payment.note:
                raise osv.except_osv(_('Warning!'), _('You must add a note to this payment before requesting it to be voided.'))
            else:
                self.write(cr, uid, ids, {'state': 'request'}, context=context)
        return {}   

    def set_voided(self, cr, uid, ids, context):
        self.write(cr, uid, ids, {'state': 'voided'}, context=context)
        return {} 

    def onchange_papernumber(self, cr, uid, ids, instr_number, context=None):
        if instr_number:
          hash = hashlib.sha1()
          hash.update(instr_number)
          self.write(cr, uid, ids, {'system_print_number': '89' + hash.hexdigest()[:12].upper()}, context=context)               
        return {}  

    def create(self, cr, uid, vals, context=None):         
#         if vals['period_covered_start'] and vals['period_covered_end']: 
#             if vals['period_covered_end'] < vals['period_covered_start']:
#                 raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.')) 
#         result = super(account_bank_payment,self).create(cr, uid, vals, context=context)             
        return result  

    def write(self, cr, uid, ids, vals, context=None):
      if uid != 15 and vals['head_subhead']:   
          raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))
#         if vals['period_covered_start'] and vals['period_covered_end']: 
#             if vals['period_covered_end'] < vals['period_covered_start']:
#                 raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))  
#         result = super(account_bank_payment,self).write(cr, uid, ids, vals, context=context)             
      return result  

    def onchange_period_covered(self, cr, uid, ids, period_covered_start, period_covered_end, context=None):
        if period_covered_start and period_covered_end: 
            if period_covered_end < period_covered_start:
                raise osv.except_osv(_('Warning!'), _('End date cannot be earlier than start date.'))               
        return {}

    def onchange_head(self, cr, uid, ids, context=None):
      if uid != 15:   
          raise osv.except_osv(_('Error!'), _('You are not allowed to edit this field.'))               
      return {}  

    def onchange_description(self, cr, uid, ids, context=None):
      if uid != 15:   
          raise osv.except_osv(_('Error!'), _('You are not allowed to edit this field.'))               
      return {}

account_bank_payment()


class paydirect_payment(osv.osv):
    _name = "payments"
    _description = 'PayDirect Raw Payment Records'
    _columns = {        
        'paymentreference': fields.char('Payment Reference', size=255),
        'paymentdate': fields.date('Payment Date'),
        'itemamount': fields.float('Amount', digits_compute=dp.get_precision('Account')),                                                 
        'itemname': fields.char('Description', size=255),    
        'customername': fields.char('Customer Name', size=255),
        'itemcode': fields.char('Head/Sub-head', size=255),  
        'bankname': fields.char('Bank Name', size=255),     
        'payment_type': fields.char('Payment Type', size=255),
        'paymentlogid': fields.integer('Payment Log Id'),                 
        'terminalid': fields.char('Terminal Id', size=255),                 
        'channelname': fields.char('Channel Name', size=255),
        'location': fields.char('Channel Name', size=255),                  
        'institutionid': fields.char('Institution Id', size=255),         
        'institutionname': fields.char('Institution', size=255),
        'branchname': fields.char('Branch', size=255),   
        'receiptno': fields.char('Receipt No', size=255),  
        'collectionsaccount': fields.char('Collections Account', size=255),                                                          
        'bankcode': fields.char('Bank Code', size=255),   
        'isreversal': fields.boolean('Is Reversal', help="Indicates whether the transactions is a reversal or not."),                                                          
        'depositslipnumber': fields.char('Deposit Slip Number', size=255),                
        'custreference': fields.char('Customer Reference', size=255),
        'alternatecustreference': fields.char('Alternate Customer Reference', size=255),
        'paymentstatus': fields.integer('Payment Status'),
        'settlementdate': fields.date('Settlement Date'),
        'feename': fields.char('Fee Name', size=255),
        'othercustomerinfo': fields.char('Other Customer Info', size=255),
        'collectionsaccount': fields.char('Collections Account', size=255),
        'thirdpartycode': fields.char('Thirty Party Code', size=255),
        'customeraddress': fields.char('Customer Address', size=255),
        'customerphonenumber': fields.char('Customer Phone Number', size=255),
        'depositorname': fields.char('Depositor Name', size=255),
        'paymentcurrency': fields.char('Payment Currency', size=255),
        'productgroupcode': fields.char('Product Group Code', size=255),
        'transactiondate': fields.date('Transaction Date'),
        'itemamount': fields.float('Item Amount', digits_compute=dp.get_precision('Account')),
        'leadbankcode': fields.char('Lead Bank Code', size=255),
        'leadbankcbncode': fields.char('Lead Bank CBN Code', size=255),
        'leadbankname': fields.char('Lead Bank Name', size=255),
        'ip': fields.char('IP', size=255),          
    }
    _order = "paymentdate desc"

paydirect_payment()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

When I try to edit a record and save, I get this error:

    Client Traceback (most recent call last):
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\http.py", line 204, in dispatch
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\controllers\main.py", line 1125, in call_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\controllers\main.py", line 1117, in _call_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 42, in proxy
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 30, in proxy_method
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 103, in send


Server Traceback (most recent call last):
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\web\session.py", line 89, in send
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\netsvc.py", line 292, in dispatch_rpc
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\service\web_services.py", line 626, in dispatch
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 188, in execute_kw
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 131, in wrapper
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 197, in execute
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\.\openerp\osv\osv.py", line 185, in execute_cr
  File "C:\Program Files (x86)\OpenERP 7.0\Server\server\openerp\addons\account_collections\account_bank_payment.py", line 169, in write
KeyError: 'head_subhead'

Can someone help identify my error?

Thanks in advance.

0
nazarii
On 8/18/13, 11:35 AM

You simply need to change if uid != 15 and vals['head_subhead']: to if uid != 15 and vals.get('head_subhead'):. Explanations are here.

Thank you. That definitely solved the problem. I copied that syntax from an existing module so I was surprised that it didn't work. Any ideas on why their syntax worked and mine didn't?

Bam Technologies Limited, Ademola Oke
on 8/18/13, 3:55 PM

I can't see reason why do we need to update value of this field each time we change the other one. In my opinion get() must be used everywhere in the code unless field value is required to populate each time in the vals.

nazarii
on 8/18/13, 4:54 PM

@Faiyer: The reason why you get key error is that when python tries to get vals['head_subhead'] it doesn't find it from your dictionary so it throws keyerror exception, with vals.get('head_subhead') if key is not found it returns none if it doesn't find head_subhead not an exception. I would assume that in that other code they caught the keyerror exception somewhere so it didn't crash but you don't catch it anywhere so it crashes.

Laura Jarvenpaa
on 8/19/13, 3:32 AM

@Laura: Thanks. @nazarii: Can you explain more detail in your comment?

Bam Technologies Limited, Ademola Oke
on 8/20/13, 2:27 PM

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

0 follower(s)

Stats

Asked: 8/18/13, 11:20 AM
Seen: 4382 times
Last updated: 3/16/15, 8:10 AM