Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
[IMP] Fix mutable default arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel Adriaans committed Feb 6, 2017
1 parent 100050d commit b4a6a60
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
24 changes: 18 additions & 6 deletions product_configurator/models/product.py
Expand Up @@ -109,7 +109,9 @@ def get_adjacent_steps(self, value_ids, active_step_line_id=None):
})
return adjacent_steps

def formatPrices(self, prices={}, dp='Product Price'):
def formatPrices(self, prices=None, dp='Product Price'):
if prices is None:
prices = {}
dp = None
prices['taxes'] = formatLang(
self.env, prices['taxes'], monetary=True, dp=dp)
Expand All @@ -122,7 +124,7 @@ def formatPrices(self, prices={}, dp='Product Price'):
return prices

@api.multi
def get_cfg_price(self, value_ids, custom_values={},
def get_cfg_price(self, value_ids, custom_values=None,
pricelist_id=None, formatLang=False):
""" Computes the price of the configured product based on the configuration
passed in via value_ids and custom_values
Expand All @@ -133,6 +135,8 @@ def get_cfg_price(self, value_ids, custom_values={},
:param formatLang: boolean for formatting price dictionary
:returns: dictionary of prices per attribute and total price"""
self.ensure_one()
if custom_values is None:
custom_values = {}
if not pricelist_id:
partner = self.env.user.partner_id
pricelist = partner.property_product_pricelist
Expand Down Expand Up @@ -194,7 +198,7 @@ def get_cfg_price(self, value_ids, custom_values={},
return prices

@api.multi
def search_variant(self, value_ids, custom_values={}):
def search_variant(self, value_ids, custom_values=None):
""" Searches product.variants with given value_ids and custom values
given in the custom_values dict
Expand All @@ -203,6 +207,8 @@ def search_variant(self, value_ids, custom_values={}):
:returns: product.product recordset of products matching domain
"""
if custom_values is None:
custom_values = {}
attr_obj = self.env['product.attribute']
for product_tmpl in self:
domain = [('product_tmpl_id', '=', product_tmpl.id)]
Expand Down Expand Up @@ -249,7 +255,7 @@ def get_config_image_obj(self, value_ids, size=None):
return img_obj

@api.multi
def get_variant_vals(self, value_ids, custom_values={}, **kwargs):
def get_variant_vals(self, value_ids, custom_values=None, **kwargs):
""" Hook to alter the values of the product variant before creation
:param value_ids: list of product.attribute.values ids
Expand All @@ -258,6 +264,8 @@ def get_variant_vals(self, value_ids, custom_values={}, **kwargs):
:returns: dictionary of values to pass to product.create() method
"""
self.ensure_one()
if custom_values is None:
custom_values = {}

image = self.get_config_image_obj(value_ids).image
all_images = tools.image_get_resized_images(
Expand Down Expand Up @@ -294,7 +302,7 @@ def get_variant_vals(self, value_ids, custom_values={}, **kwargs):
return vals

@api.multi
def create_variant(self, value_ids, custom_values={}):
def create_variant(self, value_ids, custom_values=None):
""" Creates a product.variant with the attributes passed via value_ids
and custom_values
Expand All @@ -304,6 +312,8 @@ def create_variant(self, value_ids, custom_values={}):
:returns: product.product recordset of products matching domain
"""
if custom_values is None:
custom_values = {}
valid = self.validate_configuration(value_ids, custom_values)
if not valid:
raise ValidationError(_('Invalid Configuration'))
Expand Down Expand Up @@ -345,7 +355,7 @@ def value_available(self, attr_val_id, value_ids):
return True

@api.multi
def validate_configuration(self, value_ids, custom_vals={}, final=True):
def validate_configuration(self, value_ids, custom_vals=None, final=True):
""" Verifies if the configuration values passed via value_ids and custom_vals
are valid
Expand All @@ -359,6 +369,8 @@ def validate_configuration(self, value_ids, custom_vals={}, final=True):
"""
# TODO: Raise ConfigurationError with reason
# Check if required values are missing for final configuration
if custom_vals is None:
custom_vals = {}
if final:
for line in self.attribute_line_ids:
common_vals = set(value_ids) & set(line.value_ids.ids)
Expand Down
6 changes: 5 additions & 1 deletion product_configurator/models/product_config.py
Expand Up @@ -344,7 +344,7 @@ def action_confirm(self):
return valid

@api.multi
def update_config(self, attr_val_dict={}, custom_val_dict={}):
def update_config(self, attr_val_dict=None, custom_val_dict=None):
"""Update the session object with the given value_ids and custom values.
Use this method instead of write in order to prevent incompatible
Expand All @@ -368,6 +368,10 @@ def update_config(self, attr_val_dict={}, custom_val_dict={}):
}
"""
if attr_val_dict is None:
attr_val_dict = {}
if custom_val_dict is None:
custom_val_dict = {}
update_vals = {}

value_ids = self.value_ids.ids
Expand Down
5 changes: 4 additions & 1 deletion product_configurator_mrp/models/product.py
Expand Up @@ -7,8 +7,11 @@ class ProductTemplate(models.Model):
_inherit = 'product.template'

@api.multi
def create_variant(self, value_ids, custom_values={}):
def create_variant(self, value_ids, custom_values=None):
"""Add bill of matrials to the configured variant."""
if custom_values is None:
custom_values = {}

variant = super(ProductTemplate, self).create_variant(
value_ids, custom_values=custom_values
)
Expand Down
21 changes: 17 additions & 4 deletions website_product_configurator/controllers/main.py
Expand Up @@ -140,13 +140,16 @@ def value_onchange(self, product_tmpl, config_step=None, cfg_vals=[]):

# TODO: Use the same variable name all over cfg_val, cfg_step, no mixup
# TODO: Rename cfg_vars to cfg_env everywhere, possibly turn into object
def config_vars(self, product_tmpl, active_step=None, data={}):
def config_vars(self, product_tmpl, active_step=None, data=None):
""" Proccess configuration step variables from the product.template
:param product_tmpl: product.template object being configured
:param active_step: current product.config.step.line object
:returns: dict of config related variables
"""
if data is None:
data = {}

attr_lines = product_tmpl.attribute_line_ids
cfg_lines = product_tmpl.config_line_ids
config_steps = product_tmpl.config_step_line_ids
Expand Down Expand Up @@ -416,11 +419,18 @@ def config_parse(self, product_tmpl, post, config_step=None,
values['errors'][field_name] = 'invalid'
return values

def config_redirect(self, product_tmpl, config_step, post={},
value_ids=[], custom_vals={}):
def config_redirect(self, product_tmpl, config_step, post=None,
value_ids=None, custom_vals=None):
"""
Redirect user to a certain url depending on the configuration state
"""
if post is None:
post = {}
if value_ids is None:
value_ids = []
if custom_vals is None:
custom_vals = {}

cfg_steps = product_tmpl.config_step_line_ids

product_tmpl_url = '/configurator/%s' % slug(product_tmpl)
Expand Down Expand Up @@ -560,7 +570,7 @@ def image_update(self, product_tmpl, cfg_vals,
json_config.get('attr_vals', {}).values())
return self.get_config_image(product_tmpl, value_ids, size)

def configure_product(self, product_tmpl, value_ids, custom_vals={}):
def configure_product(self, product_tmpl, value_ids, custom_vals=None):
"""Used for searching a variant with the values passed in cfg_vals
and returning a redirect to it. In case a product is not found with
the given valid configuration a new variant is generated with the
Expand All @@ -573,6 +583,9 @@ def configure_product(self, product_tmpl, value_ids, custom_vals={}):
"""
# TODO: Implement a search and create method that can be extended
# easily
if custom_vals is None:
custom_vals = {}

product = product_tmpl.search_variant(value_ids, custom_vals)
if product:
if len(product) > 1:
Expand Down

0 comments on commit b4a6a60

Please sign in to comment.