1
# -*- encoding: utf-8 -*-
2
###############################################################################
4
# product_custom_attributes for OpenERP #
5
# Copyright (C) 2011 Akretion Benoît GUILLOT <benoit.guillot@akretion.com> #
7
# This program is free software: you can redistribute it and/or modify #
8
# it under the terms of the GNU Affero General Public License as #
9
# published by the Free Software Foundation, either version 3 of the #
10
# License, or (at your option) any later version. #
12
# This program is distributed in the hope that it will be useful, #
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15
# GNU Affero General Public License for more details. #
17
# You should have received a copy of the GNU Affero General Public License #
18
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
20
###############################################################################
22
from osv import osv, fields
25
class attribute_option(osv.osv):
26
_name = "attribute.option"
27
_description = "Attribute Option"
31
'name': fields.char('Name', size=128, required=True),
32
'attribute_id': fields.many2one('product.attribute', 'Product Attribute', required=True),
33
'sequence': fields.integer('Sequence'),
36
class product_attribute(osv.osv):
38
_name = "product.attribute"
39
_description = "Product Attribute"
42
_inherits = {'ir.model.fields': 'field_id'}
45
'field_id': fields.many2one('ir.model.fields', 'Ir Model Fields', required=True, ondelete="cascade"),
46
'attribute_type': fields.selection([('char','Char'),('text','Text'),('select','Select'),('multiselect','Multiselect'),('boolean','Boolean'),('integer','Integer'),('date','Date'),('datetime','Datetime'),('binary','Binary'),('float','Float')],'Type', required=True),
47
'serialized': fields.boolean('Field serialized', help="If serialized, the field will be stocked in the serialized field : attribute_custom_tmpl or attribute_custom_variant depending on the field based_on"),
48
'based_on': fields.selection([('product_template','Product Template'),('product_product','Product Variant')],'Based on', required=True),
49
'option_ids': fields.one2many('attribute.option', 'attribute_id', 'Attribute Option'),
52
def create(self, cr, uid, vals, context=None):
53
if vals.get('based_on') == 'product_template':
54
vals['model_id'] = self.pool.get('ir.model').search(cr, uid, [('model', '=', 'product.template')],context=context)[0]
55
serial_name = 'attribute_custom_tmpl'
57
vals['model_id'] = self.pool.get('ir.model').search(cr, uid, [('model', '=', 'product.product')], context=context)[0]
58
serial_name = 'attribute_custom_variant'
59
if vals.get('serialized'):
60
vals['serialization_field_id'] = self.pool.get('ir.model.fields').search(cr, uid, [('name', '=', serial_name)], context=context)[0]
61
if vals['attribute_type'] == 'select':
62
vals['ttype'] = 'many2one'
63
vals['relation'] = 'attribute.option'
64
elif vals['attribute_type'] == 'multiselect':
65
vals['ttype'] = 'many2many'
66
vals['relation'] = 'attribute.option'
67
if not vals.get('serialized'):
68
raise osv.except_osv(_('Create Error'), _("The field serialized should be ticked for a multiselect field !"))
70
vals['ttype'] = vals['attribute_type']
71
vals['field_description'] = vals['name'][2:].capitalize()
72
vals['state'] = 'manual'
73
return super(product_attribute, self).create(cr, uid, vals, context)
75
class attribute_location(osv.osv):
77
_name = "attribute.location"
78
_description = "Attribute Location"
81
_inherits = {'product.attribute': 'attribute_id'}
84
'attribute_id': fields.many2one('product.attribute', 'Product Attribute', required=True, ondelete="cascade"),
85
'attribute_set_id': fields.many2one('attribute.set', 'Attribute Set', required=True),
86
'attribute_group_id': fields.many2one('attribute.group', 'Attribute Group', required=True),
87
'sequence': fields.integer('Sequence'),
90
class attribute_group(osv.osv):
92
_name= "attribute.group"
93
_description = "Attribute Group"
97
'name': fields.char('Name', size=128, required=True),
98
'attribute_set_id': fields.many2one('attribute.set', 'Attribute Set', required=True),
99
'attribute_ids': fields.one2many('attribute.location', 'attribute_group_id', 'Attributes'),
100
'sequence': fields.integer('Sequence'),
104
class attribute_set(osv.osv):
106
_name = "attribute.set"
107
_description = "Attribute Set"
110
'name': fields.char('Name', size=128, required=True),
111
'attribute_group_ids': fields.one2many('attribute.group', 'attribute_set_id', 'Attribute Groups'),