Odoo is the world's easiest all-in-one management software. It includes hundreds of business apps:
CRM | e-Commerce | Accounting | Inventory | PoS | Project management | MRP | etc.
I have programmed the following module to automatically assign an EAN13 code to every new product (see code below).
The module works fine, both when you manually add products and when you import ONE product from CSV.
However, when trying to import a set of products (even a small number like 10) you get the following message:
Unknown error during import: <class 'openerp.exceptions.ValidationError'>: ('ValidateError', u'Field(s) `ean13` failed against a constraint: EAN13 code not valid. Use "Internal reference" field instead') in row number 5
Resolve other errors first
Row number varies from execution to execution. So it seems that the same code is being tried to be used twice (maybe CSV loads rows in paralel leading to a race condition or to the sequence to assign the same value).
What is wrong with the customized EAN13 method below? How should it be programmed to support CSV import too? Is there any way to make that code/transaction atomic?
EAN13 validation code:
# -*- coding: utf-8 -*-
from openerp import models, fields, api
# Class EanAuto generates automatically EAN codes for new created products
# This ensures that every new product has its own unique EAN code
# Sequence is defined in XML file, and it is originally intended to use internal EAN13 codes
# although it can be used for any other codes.
# It redefines create method adding additional required steps to retrieve a new EAN code
# to the product.
# If product is already provided with an EAN code it will be respected.
# If EAN code is used it will look for the next free value, so no duplicates are generated
# during creation through this routine.
# EAN duplicates are not checked elsewhere, at least not by this module.
class EanAuto ( models.Model ):
_inherit = 'product.template'
def create(self, vals):
# Tests if EAN13 has been provided, if so do nothing, otherwise look for free EAN13 code
if not vals.get('ean13'):
# Gets next value for custom EAN13 working out checksum digit
seq_id = self.env['ir.sequence'].search([('code','=','ean.code')])
ean_seq = seq_id._next()
ean_check_digit = 10 - (( sum(map(int, ean_seq[0::2])) + 3*sum(map(int, ean_seq[1::2])) ) % 10 )
ean_code = str(ean_seq) + str(ean_check_digit)
# Checks that there is no such EAN in use
prod_count = self.env['product.product'].search_count([('ean13','=',ean_code)])
if prod_count == 0:
# EAN is not in use, so proceed
# Creates the product using standard create method
new_product_template = super(EanAuto, self).create(vals)
# TEST TEST try to retrieve product_id of the new generated product
Sequence is defined as follows:
# cat sequence.xml
<?xml version="1.0" encoding="utf-8"?>
<record id="ean_code_sequence" model="ir.sequence.type">
<field name="name">EAN Code Sequence</field>
<record id="automatic_ean" model="ir.sequence">
<field name="name">Automatic EAN13</field>
About This Community
This platform 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.Register
Odoo Training Center
Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.Test it now
|Asked: 12/6/15, 1:53 PM|
|Seen: 1033 times|
|Last updated: 12/8/15, 11:27 AM|