~angelmoya/product-extra-addons/nan_product_pack

« back to all changes in this revision

Viewing changes to product_custom_attributes/product_attribute.py

  • Committer: Benoit Guillot
  • Date: 2012-08-03 14:33:51 UTC
  • Revision ID: benoit.guillot@akretion.com.br-20120803143351-bft9u8x3cqlduuy3
[ADD] module product_custom_attributes: manage the product attributes and their options

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
###############################################################################
 
3
#                                                                             #
 
4
#   product_custom_attributes for OpenERP                                      #
 
5
#   Copyright (C) 2011 Akretion Benoît GUILLOT <benoit.guillot@akretion.com>  #
 
6
#                                                                             #
 
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.                           #
 
11
#                                                                             #
 
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.                       #
 
16
#                                                                             #
 
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/>.     #
 
19
#                                                                             #
 
20
###############################################################################
 
21
 
 
22
from osv import osv, fields
 
23
import netsvc
 
24
 
 
25
class attribute_option(osv.osv):
 
26
    _name = "attribute.option"
 
27
    _description = "Attribute Option"
 
28
    _order="sequence"
 
29
 
 
30
    _columns = {
 
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'),
 
34
    }
 
35
 
 
36
class product_attribute(osv.osv):
 
37
 
 
38
    _name = "product.attribute"
 
39
    _description = "Product Attribute"
 
40
    _order="sequence"
 
41
 
 
42
    _inherits = {'ir.model.fields': 'field_id'}
 
43
 
 
44
    _columns = {
 
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'),
 
50
    }
 
51
 
 
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'
 
56
        else:
 
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 !"))
 
69
        else:
 
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)
 
74
 
 
75
class attribute_location(osv.osv):
 
76
 
 
77
    _name = "attribute.location"
 
78
    _description = "Attribute Location"
 
79
    _order="sequence"
 
80
 
 
81
    _inherits = {'product.attribute': 'attribute_id'}
 
82
 
 
83
    _columns = {
 
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'),
 
88
    }
 
89
 
 
90
class attribute_group(osv.osv):
 
91
 
 
92
    _name= "attribute.group"
 
93
    _description = "Attribute Group"
 
94
    _order="sequence"
 
95
 
 
96
    _columns = {
 
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'),
 
101
    }
 
102
 
 
103
 
 
104
class attribute_set(osv.osv):
 
105
 
 
106
    _name = "attribute.set"
 
107
    _description = "Attribute Set"
 
108
 
 
109
    _columns = {
 
110
        'name': fields.char('Name', size=128, required=True),
 
111
        'attribute_group_ids': fields.one2many('attribute.group', 'attribute_set_id', 'Attribute Groups'),
 
112
    }