from odoo import models class ProductTemplate(models.Model): _inherit = 'product.template' def api_single_response(self, product_template, with_detail=False): base_url = self.env['ir.config_parameter'].get_param('web.base.url') product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id') product_pricelist_default_discount_id = int(product_pricelist_default_discount_id) data = { 'id': product_template.id, 'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '', 'code': product_template.default_code or '', 'name': product_template.name, 'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default_discount_id), 'variant_total': len(product_template.product_variant_ids), 'stock_total': product_template.qty_stock_vendor, 'weight': product_template.weight, 'manufacture': self.api_manufacture(product_template), 'categories': self.api_categories(product_template), } if with_detail: data_with_detail = { 'image': base_url + 'api/image/product.template/image_512/' + str(product_template.id) if product_template.image_512 else '', 'display_name': product_template.display_name, 'variants': [], 'description': product_template.website_description or '', 'solr_flag': product_template.solr_flag, 'product_rating': product_template.product_rating, } for variant in product_template.product_variant_ids: data_with_detail['variants'].append({ 'id': variant.id, 'code': variant.default_code or '', 'name': variant.display_name, 'price': self.env['product.pricelist'].compute_price(product_pricelist_default_discount_id, variant.id), 'stock': variant.qty_stock_vendor, 'weight': variant.weight, 'attributes': [x.name for x in variant.product_template_attribute_value_ids], 'solr_flag': product_template.solr_flag, 'product_rating': product_template.product_rating, }) data.update(data_with_detail) return data def api_manufacture(self, product_template): base_url = self.env['ir.config_parameter'].get_param('web.base.url') if product_template.x_manufacture: manufacture = product_template.x_manufacture return { 'id': manufacture.id, 'name': manufacture.x_name, 'image_promotion_1': base_url + 'api/image/x_manufactures/image_promotion_1/' + str(manufacture.id) if manufacture.image_promotion_1 else '', 'image_promotion_2': base_url + 'api/image/x_manufactures/image_promotion_2/' + str(manufacture.id) if manufacture.image_promotion_2 else '', } return {} def api_categories(self, product_template): if product_template.public_categ_ids: categories = [] for category in product_template.public_categ_ids: categories.append({ 'id': category.id, 'name': category.name }) return categories return []