# -*- coding: utf-8 -*- from docutils.nodes import entry from openerp.http import request as r _MODEL = 'product.template' def get_products(image_type='small', location_ids=None): domain = [ ('sale_ok', '=', True), ('list_price', '>', 0), ('active', '=', True) ] in_locations = {} if location_ids: for q in r.env['stock.quant'].search([('location_id', 'in', location_ids)], order='product_id'): entry = in_locations.get(q.product_id.id, None) if entry: if q.location_id.id not in map(lambda x: x.get('id'), entry): entry.append({ 'id': q.location_id.id, 'name': q.location_id.display_name }) continue in_locations[q.product_id.id] = [ { 'id': q.location_id.id, 'name': q.location_id.display_name } ] return [ { 'id': p.id, 'name': p.display_name, 'ean13': p.ean13, 'defaultCode': p.default_code, 'image': p.image_small if image_type == 'small' else p.image_medium, 'listPrice': p.list_price, 'variantCount': p.product_variant_count, 'quantity': 1, 'price': p.list_price, 'discount': 0, 'categoryId': p.categ_id.id or None, 'variants': [{ 'id': v.id, 'name': v.display_name, 'ean13': v.ean13, 'defaultCode': p.default_code, 'image': v.image_small if image_type == 'small' else v.image_medium, 'listPrice': v.list_price, 'standardPrice': v.standard_price, 'quantity': 1, 'price': v.list_price, 'discount': 0, 'pricelistId': v.pricelist_id.id or None, 'locations': in_locations.get(v.id, []), 'categoryId': v.categ_id.id or None, 'tmplId': p.id or None, 'tmplCategoryId': p.categ_id.id or None } for v in p.product_variant_ids] } for p in r.env[_MODEL].search(domain) ] def create_product(**kw): product = r.env[_MODEL].create({ 'name': kw.get('name'), 'listPrice': float(kw.get('price')), 'ean13': kw.get('ean13') }) return { 'id': product.id, 'name': product.display_name, 'ean13': product.ean13, 'image': product.image_small, 'listPrice': product.list_price, 'variantCount': product.product_variant_count, 'quantity': 1, 'price': product.list_price, 'discount': 0, 'variants': [{ 'id': variant.id, 'name': variant.display_name, 'ean13': variant.ean13, 'image': variant.image_small, 'listPrice': variant.list_price, 'quantity': 1, 'price': variant.list_price, 'discount': 0, } for variant in product.product_variant_ids if variant.active] }