"""
Get the list of products for a user, optionally filtered by place.
"""
from datetime import datetime
# set the batch-size
batch_size = 100
# set some image parameter
name = 'image_128_front'
res_field = 'image_128'
res_model = 'product.template'
# get Odoo settings
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]
[docs]
def get_product_list(partner_id, place=None):
"""
Get the list of products for a given user (x_partner_id),
optionally filtered by a place (x_place),
including the last stock move and the 'image_128_front' attachment.
Parameters:
- partner_id: str
- place: str or None; if 'all' or None, returns all products for the user
Returns:
- list of product dicts
"""
custom_product_list = []
if partner_id:
offset = 0
# Prepare search domain
domain = [['x_partner_id', '=', partner_id]]
if place and place.lower() != 'all':
domain.append(['x_place', '=', place])
# Paginate through product records
while True:
batch = models.execute_kw(
db, uid, password,
'product.product', 'search_read',
[domain],
{
'fields': ['id', 'name', 'description', 'product_tmpl_id', 'x_place'],
'limit': batch_size,
'offset': offset
}
)
if not batch:
break
for product in batch:
product_id = product['id']
# Get the last stock move
stock_moves = models.execute_kw(
db, uid, password,
'stock.move', 'search_read',
[[('product_id', '=', product_id)]],
{
'fields': ['date'],
'limit': 1,
'order': 'date desc'
}
)
# get the date from the last stock move of the product
if stock_moves:
last_move_date = stock_moves[0]['date']
formatted_date = datetime.strptime(
last_move_date, '%Y-%m-%d %H:%M:%S'
).strftime('%d.%m.%Y')
product['last_move_date'] = formatted_date
else:
product['last_move_date'] = ''
# get the product template id, as the images are stored under the product template id
product_tmpl_id = product['product_tmpl_id'][0]
# Fetch only the 'image_128_front' attachment images for this product
image_attachment = models.execute_kw(
db, uid, password,
'ir.attachment', 'search_read',
[[['res_id', '=', product_tmpl_id],
['name', '=', name],
['res_field', '=', res_field],
['res_model', '=', res_model]]],
{'fields': ['datas'], 'limit': 1}
)
if image_attachment:
product['image_128'] = image_attachment[0]['datas']
else:
product['image_128'] = ''
custom_product_list.append(product)
offset += batch_size
return custom_product_list