Skip to content

Commit

Permalink
[FIX] the relation to the attribute set should be on the product.temp…
Browse files Browse the repository at this point in the history
…late, not the product.product.

The rationale for this is that a set can contain attributes stored on the product or on the template.
Thus, we have to know to which set a template is related to be able to read the list of attributes of the template within this set.
  • Loading branch information
guewen committed Sep 30, 2013
1 parent 4eb0038 commit 31ddb56
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion product_custom_attributes/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

{
'name': 'product_custom_attributes',
'version': '0.1',
'version': '0.2',
'category': 'Generic Modules/Others',
'license': 'AGPL-3',
'description': """This module adds the possibility to easily create custom fields on products.
Expand Down
35 changes: 35 additions & 0 deletions product_custom_attributes/migrations/7.0.0.2/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2013 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging

logger = logging.getLogger('upgrade')


def migrate(cr, version):
logger.info("Migrating product_custom_attributes from version %s", version)
cr.execute("UPDATE product_template pt "
"SET attribute_set_id = (SELECT pp.attribute_set_id_copy "
" FROM product_product pp WHERE "
" pp.product_tmpl_id = pt.id "
" LIMIT 1)"
"WHERE pt.attribute_set_id IS NULL")
cr.execute('ALTER TABLE product_product DROP COLUMN attribute_set_id')
36 changes: 36 additions & 0 deletions product_custom_attributes/migrations/7.0.0.2/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Guewen Baconnier
# Copyright 2013 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import logging

logger = logging.getLogger('upgrade')


def migrate(cr, version):
logger.info("Migrating product_custom_attributes from version %s", version)
cr.execute("SELECT count(pp.attribute_set_id), pp.attribute_set_id "
"FROM product_product pp "
"INNER JOIN product_template pt ON pp.product_tmpl_id = pt.id "
"GROUP BY pp.attribute_set_id "
"HAVING count(pp.id) > 1")
if cr.rowcount > 0:
raise Exception('Impossible to migrate: all the variants should have '
'the same attribute set.')
8 changes: 7 additions & 1 deletion product_custom_attributes/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
from tools.translate import _
from lxml import etree

class product_template(Model):
_inherit = "product.template"

_columns = {
'attribute_set_id': fields.many2one('attribute.set', 'Attribute Set'),
}


class product_product(Model):
_inherit = "product.product"
Expand All @@ -43,7 +50,6 @@ def _attr_grp_ids(self, cr, uid, ids, field_names, arg=None, context=None):
return res

_columns = {
'attribute_set_id': fields.many2one('attribute.set', 'Attribute Set'),
'attribute_group_ids': fields.function(_attr_grp_ids, type='one2many',
relation='attribute.group', string='Groups')
}
Expand Down

0 comments on commit 31ddb56

Please sign in to comment.