From aafe2e35e7d4bdd05d0df519cd2df201f67bcf72 Mon Sep 17 00:00:00 2001 From: Adam Heinz Date: Mon, 18 Apr 2022 17:20:53 +0000 Subject: [PATCH] [IMP] sale: simplify API of create_product_variant method ### Current behavior before PR: The `product.template` model has a nice function `create_product_variant` that unfortunately expects its input to be in JSON. ### Desired behavior after PR is merged: JSON decoding is moved into the controller, such that the function can be called as expected. -- I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr closes odoo/odoo#88989 Signed-off-by: Victor Feyens (vfe) --- addons/sale/controllers/variant.py | 4 +++- addons/sale/models/product_template.py | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/addons/sale/controllers/variant.py b/addons/sale/controllers/variant.py index 694d8561040..06958dea7e0 100644 --- a/addons/sale/controllers/variant.py +++ b/addons/sale/controllers/variant.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +import json + from odoo import http from odoo.http import request @@ -29,7 +31,7 @@ class VariantController(http.Controller): @http.route(['/sale/create_product_variant'], type='json', auth="user", methods=['POST']) def create_product_variant(self, product_template_id, product_template_attribute_value_ids, **kwargs): - return request.env['product.template'].browse(int(product_template_id)).create_product_variant(product_template_attribute_value_ids) + return request.env['product.template'].browse(int(product_template_id)).create_product_variant(json.loads(product_template_attribute_value_ids)) def _get_pricelist(self, pricelist_id, pricelist_fallback=False): return request.env['product.pricelist'].browse(int(pricelist_id or 0)) diff --git a/addons/sale/models/product_template.py b/addons/sale/models/product_template.py index 2bfc5c9c543..5719cb237da 100644 --- a/addons/sale/models/product_template.py +++ b/addons/sale/models/product_template.py @@ -115,14 +115,14 @@ class ProductTemplate(models.Model): :param product_template_attribute_value_ids: the combination for which to get or create variant - :type product_template_attribute_value_ids: json encoded list of id + :type product_template_attribute_value_ids: list of id of `product.template.attribute.value` :return: id of the product variant matching the combination or 0 :rtype: int """ combination = self.env['product.template.attribute.value'] \ - .browse(json.loads(product_template_attribute_value_ids)) + .browse(product_template_attribute_value_ids) return self._create_product_variant(combination, log_warning=True).id or 0