# -*- coding: utf-8 -*- from openerp.http import request as r def get_products(type=None, location_ids=[]): product_obj = r.env['product.template'] domain = [ ('standard_price', '>=', 0), ('active', '=', True) ] if type == 'purchase': domain += [('purchase_ok', '=', True)] else: if product_obj.fields_get('hr_expense_ok'): domain += [('hr_expense_ok', '=', True)] else: return [] in_locations = {} if len(location_ids) > 0: 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.name, 'displayName': p.display_name, 'ean13': p.ean13, 'image': p.image_medium, 'standardPrice': p.standard_price, 'variantCount': p.product_variant_count, 'quantity': 1, 'price': p.standard_price, 'minimumPrice': 0, 'maximumPrice': 0, 'variants': [{ 'id': v.id, 'name': v.name, 'displayName': v.display_name, 'ean13': v.ean13, 'image': v.image_medium, 'standardPrice': v.standard_price, 'quantity': 1, 'price': v.standard_price, 'minimumPrice': 0, 'maximumPrice': 0, 'locations': in_locations.get(v.id, []) } for v in p.product_variant_ids if v.active] } for p in product_obj.search(domain) ] def create_product(values): p = r.env['product.template'].create({ 'name': values.get('name'), 'standard_price': float(values.get('price')), 'ean13': values.get('ean13') }) return { 'id': p.id, 'name': p.name, 'displayName': p.display_name, 'ean13': p.ean13, 'imageMedium': p.image_medium, 'standardPrice': p.standard_price, 'variantCount': p.product_variant_count, 'price': p.list_price, 'minimumPrice': p.minimum_price, 'maximumPrice': p.maximum_price, 'variants': [{ 'id': v.id, 'name': v.name, 'displayName': v.display_name, 'ean13': v.ean13, 'imageMedium': v.image_medium, 'standardPrice': p.standard_price, 'price': v.standard_price, 'minimumPrice': p.minimum_price, 'maximumPrice': p.maximum_price } for v in p.product_variant_ids if v.active] }