Source code for odoo_xmlrpc_twisted.functions.create_dummy_product

"""
Create a minimal (dummy) product with an auto-assigned RFID from a sequence.
Returns the new product ID (product.product).
"""


import os
import configparser


# ------- begin of the initialisation -------

# call the function "get_settings_odoo" to get the parameters
# for the logging of the model on the "odoo Webservice API"
try:
    from .get_settings_odoo import get_settings_odoo
except ImportError:
    from get_settings_odoo import get_settings_odoo
result   = get_settings_odoo()
db       = result[0]
uid      = result[1]
password = result[3]
models   = result[4]


# get all the parameters from the file "parameter_inventory.ini"

# Name and full path of the "ini" file
filename  = "parameter_inventory.ini"
path_name = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath  = os.path.join(config_path, filename)

config = configparser.ConfigParser()
config.read(fullpath)

# Value for the product category id
categ_id = config.get('create_product', 'categ_id')

# Value for the product type
product_type = config.get('create_product', 'type')

# Value for product is active
active = int(config.get('create_product', 'active'))

# Value for product can be sold
sale_ok = int(config.get('create_product', 'sale_ok'))

# Value for product can be purchased
purchase_ok = int(config.get('create_product', 'purchase_ok'))

# Value for the list price of the product
list_price = int(config.get('create_product', 'list_price'))

# Value for the initial name of the product
init_name = config.get('create_product', 'init_name')

# Value for the initial place of the product
x_init_place = config.get('create_product', 'x_init_place')


# Sequence settings
SEQ_CODE    = 'rfid.tag'     # must stay consistent in Odoo database
SEQ_NAME    = 'RFID Tag Identifier'
SEQ_PREFIX  = 'RFID'
SEQ_PADDING = 16              # -> RFID0000000000000001

# ------- end of the initialisation ------- 



def _ensure_sequence(models, db, uid, password):
    """
    Create the ir.sequence if missing.
    """
    seq_ids = models.execute_kw(
        db, uid, password,
        'ir.sequence', 'search',
        [[('code', '=', SEQ_CODE)]],
        {'limit': 1}
    )
    if seq_ids:
        return seq_ids[0]

    return models.execute_kw(
        db, uid, password,
        'ir.sequence', 'create',
        [{
            'name': SEQ_NAME,
            'code': SEQ_CODE,
            'prefix': SEQ_PREFIX,
            'padding': SEQ_PADDING,
            'implementation': 'standard',
        }]
    )



def _next_rfid(models, db, uid, password) -> str:
    """
    Get next formatted RFID from ir.sequence (atomic, concurrency-safe).
    """
    return models.execute_kw(
        db, uid, password,
        'ir.sequence', 'next_by_code', [SEQ_CODE])



[docs] def create_dummy_product(partner_id): """ Create a dummy product with only x_rfid_id and x_partner_id. The RFID value is auto-generated via ir.sequence. Returns the new product ID. """ if partner_id != '': # Ensure sequence, then get the next RFID value _ensure_sequence(models, db, uid, password) rfid_value = _next_rfid(models, db, uid, password) # create the new dummy product record in the odoo tables "product_product" and "product_template" product_id = models.execute_kw(db, uid, password, 'product.product', 'create', [{'active': active, 'categ_id': categ_id, 'list_price': list_price, 'name': init_name, 'sale_ok': sale_ok, 'purchase_ok': purchase_ok, 'type': product_type, 'x_rfid_id': rfid_value, 'x_partner_id': partner_id,}]) return product_id else: result = '' return(result)