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

[MIG] migrate default_val feature for product attribute to v10.0, pro… #120

Open
wants to merge 12 commits into
base: 10.0
Choose a base branch
from
33 changes: 33 additions & 0 deletions product_configurator_default_val/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

==================================
Product Configurator Default Value
==================================

This module provides the scope to allow default value for attribute line.

Usage
=====

To use this module:

* Go to Sales > Configurable Products > Configurable Templates
* Create or Select a configurable template
* In the Variants tab, enter the attribute and its values
* Enter a default attribute value
* Save the configurable template
* Run the configuration wizard in the back or on the website
* The default attribute value is selected by default

Credits
=======

* Paul Catinean <[email protected]>
* Sandip Mangukiya <[email protected]>

Contributors
------------

* Ursa Information Systems <[email protected]>
7 changes: 7 additions & 0 deletions product_configurator_default_val/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import (
models,
)
23 changes: 23 additions & 0 deletions product_configurator_default_val/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Product Configurator Default Value",
"summary": "To allow default value for attribute line",
"version": "10.0.1.0.0",
"license": "AGPL-3",
"author": "Ursa Information Systems",
"category": "Sales",
"maintainer": "Ursa Information Systems",
"website": "http://www.ursainfosystems.com",
"depends": [
"product_configurator",
],
"qweb": [
],
"data": [
"views/product_view.xml",
],
"application": False,
}
9 changes: 9 additions & 0 deletions product_configurator_default_val/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import (
product_config,
product_attribute,
product
)
33 changes: 33 additions & 0 deletions product_configurator_default_val/models/product.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, models, _
from odoo.exceptions import ValidationError


class ProductTemplate(models.Model):
_inherit = 'product.template'

@api.multi
@api.constrains('attribute_line_ids')
def _check_default_values(self):
"""Validate default values set on the product template"""
default_val_ids = self.attribute_line_ids.filtered(
lambda l: l.default_val).mapped('default_val').ids

# TODO: Remove if cond when PR with raise error on github is merged
if not self.validate_configuration(default_val_ids, final=False):
raise ValidationError(
_('Default values provided generate an invalid configuration')
)

@api.multi
@api.constrains('config_line_ids')
def _check_default_value_domains(self):
try:
self._check_default_values()
except ValidationError:
raise ValidationError(
_('Restrictions added make the current default values '
'generate an invalid configuration')
)
31 changes: 31 additions & 0 deletions product_configurator_default_val/models/product_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ProductAttributeLine(models.Model):
_inherit = 'product.attribute.line'

@api.onchange('value_ids')
def onchange_values(self):
if self.default_val and self.default_val not in self.value_ids:
self.default_val = None

default_val = fields.Many2one(
comodel_name='product.attribute.value',
string='Default Value'
)

@api.multi
@api.constrains('value_ids', 'default_val')
def _check_default_values(self):
for line in self.filtered(lambda l: l.default_val):
if line.default_val not in line.value_ids:
raise ValidationError(
_("Default values for each attribute line must exist in "
"the attribute values (%s: %s)" % (
line.attribute_id.name, line.default_val.name)
)
)
31 changes: 31 additions & 0 deletions product_configurator_default_val/models/product_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, models, _
from odoo.exceptions import ValidationError


class ProductConfigSession(models.Model):
_inherit = 'product.config.session'

@api.model
def create(self, vals):
product_tmpl = self.env['product.template'].browse(
vals.get('product_tmpl_id')).exists()
if product_tmpl:
default_val_ids = product_tmpl.attribute_line_ids.filtered(
lambda l: l.default_val).mapped('default_val').ids
value_ids = vals.get('value_ids')
if value_ids:
default_val_ids += value_ids[0][2]
valid_conf = product_tmpl.validate_configuration(
default_val_ids, final=False)
# TODO: Remove if cond when PR with raise error on github is merged
if not valid_conf:
raise ValidationError(
_('Default values provided generate an invalid '
'configuration')
)
vals.update({'value_ids': [(6, 0, default_val_ids)]})
return super(ProductConfigSession, self).create(vals)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions product_configurator_default_val/views/product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="product_template_form_view" model="ir.ui.view">
<field name="name">product.configurator.product.template.form</field>
<field name="model">product.template</field>
<field name="inherit_id"
ref="product_configurator.product_template_form_view"/>
<field name="arch" type="xml">
<!-- TODO: Implement a method to hide this field for non-configurable product templates -->
<xpath expr="//field[@name='attribute_line_ids']/tree/field[@name='required']"
position="before">
<field name="default_val"
domain="[('id', 'in', value_ids and value_ids[0][2])]"
context="{'show_attribute': False}"
options="{'no_create': True, 'no_create_edit': True}"
invisible="not context.get('default_config_ok', False)"/>
</xpath>
</field>
</record>

</odoo>
29 changes: 29 additions & 0 deletions product_configurator_refactor/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=============================
Product Configurator Refactor
=============================

This module refactors the product configurator module to add the
product.attribute.value.line object to store additional fields specific to the
product-attribute-value.

Usage
=====

To use this module:

* Go to Sales > Configurable Products

Credits
=======

* Paul Catinean <[email protected]>
* Sandip Mangukiya <[email protected]>

Contributors
------------

* Ursa Information Systems <[email protected]>
7 changes: 7 additions & 0 deletions product_configurator_refactor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import (
models,
)
26 changes: 26 additions & 0 deletions product_configurator_refactor/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Product Configurator Refactor",
"summary": "To refactor product configurator",
"version": "10.0.1.0.0",
"license": "AGPL-3",
"author": "Ursa Information Systems",
"category": "Sales",
"maintainer": "Ursa Information Systems",
"website": "http://www.ursainfosystems.com",
"depends": [
"product_configurator_default_val"
],
"qweb": [
],
"data": [
"security/ir.model.access.csv",
"views/product_attribute_value_line_view.xml",
"views/product_attribute_views.xml",
"views/product_view.xml",
],
"application": False,
}
8 changes: 8 additions & 0 deletions product_configurator_refactor/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import (
product_attribute_value_line,
product_attribute,
)
45 changes: 45 additions & 0 deletions product_configurator_refactor/models/product_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ProductAttributeLine(models.Model):
_inherit = 'product.attribute.line'

@api.multi
def _get_attribute_values(self):
for line in self:
attribute_value_ids = line.value_idss.mapped('attrib_value_id')
if attribute_value_ids:
line.value_ids = [(6, 0, attribute_value_ids.ids)]

value_idss = fields.One2many('product.attribute.value.line',
'attribute_line_id', 'Values', copy=True)
value_ids = fields.Many2many(compute='_get_attribute_values',
comodel_name='product.attribute.value',
string='Attribute Values')
default_val = fields.Many2one('product.attribute.value',
compute='_is_default_value',
string="Default Value", store=True)

@api.depends('value_idss.is_default')
def _is_default_value(self):
default_lines = [default for default in
self.value_idss.mapped('is_default') if default]
if default_lines:
if len(default_lines) == 1:
for value in self.value_idss:
if value.is_default:
self.default_val = value.attrib_value_id
else:
raise ValidationError(_(
"Please select one, first selected option will be valid!"))
else:
self.default_val = False

@api.multi
@api.constrains('value_ids', 'default_val')
def _check_default_values(self):
return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2012 - TODAY, Ursa Information Systems
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError


class ProductAttributeValueLine(models.Model):
_name = 'product.attribute.value.line'

related_attribute_id = fields.Many2one(
related='attribute_line_id.attribute_id', string='Product Attribute')
attribute_line_id = fields.Many2one('product.attribute.line',
'Product Attribute')
attrib_value_id = fields.Many2one('product.attribute.value',
'Attribute Value')
is_default = fields.Boolean("Is Default")
2 changes: 2 additions & 0 deletions product_configurator_refactor/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
product_configurator_refactor,Attribute Value Line,model_product_attribute_value_line,product_configurator.group_product_configurator,1,1,1,1
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<!-- Product Attribute Value Line -->
<record id="product_attribute_value_line_form_view" model="ir.ui.view">
<field name="name">product.attribute.value.line.form</field>
<field name="model">product.attribute.value.line</field>
<field name="arch" type="xml">
<form string="Product Attribute Value Line">
<sheet>
<group>
<group>
<field name="attribute_line_id" invisible="1"/>
<field name="related_attribute_id" readonly="1"/>
<field name="attrib_value_id"
domain="[('attribute_id','=',related_attribute_id)]"/>
</group>
<group>
<field name="is_default"/>
</group>
</group>
</sheet>
</form>
</field>
</record>

<record id="product_attribute_value_line_tree_view" model="ir.ui.view">
<field name="name">product.attribute.value.line.tree</field>
<field name="model">product.attribute.value.line</field>
<field name="arch" type="xml">
<form string="Product Attribute Value Line">
<tree name="name">
<field name="attribute_line_id"/>
<field name="attrib_value_id"/>
<field name="is_default"/>
</tree>
</form>
</field>
</record>

</odoo>
17 changes: 17 additions & 0 deletions product_configurator_refactor/views/product_attribute_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<record id="product_attribute_line_form" model="ir.ui.view">
<field name="name">product.attribute.line.form</field>
<field name="model">product.attribute.line</field>
<field name="inherit_id" ref="product.product_attribute_line_form"/>
<field name="mode">primary</field>
<field name="priority" eval="8"/>
<field name="arch" type="xml">
<field name="value_ids" position="before">
<field name="value_idss"/>
</field>
</field>
</record>

</odoo>
Loading