[MERGE] base: raise an exception if the format of the bank account is wrong
[odoo/odoo.git] / openerp / addons / base / res / ir_property.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 from tools.misc import attrgetter
24 import time
25
26 # -------------------------------------------------------------------------
27 # Properties
28 # -------------------------------------------------------------------------
29
30 class ir_property(osv.osv):
31     _name = 'ir.property'
32
33     def _models_field_get(self, cr, uid, field_key, field_value, context=None):
34         get = attrgetter(field_key, field_value)
35         obj = self.pool.get('ir.model.fields')
36         ids = obj.search(cr, uid, [('view_load','=',1)], context=context)
37         res = set()
38         for o in obj.browse(cr, uid, ids, context=context):
39             res.add(get(o))
40         return list(res)
41
42     def _models_get(self, cr, uid, context=None):
43         return self._models_field_get(cr, uid, 'model', 'model_id.name', context)
44
45     def _models_get2(self, cr, uid, context=None):
46         return self._models_field_get(cr, uid, 'relation', 'relation', context)
47
48
49     _columns = {
50         'name': fields.char('Name', size=128, select=1),
51
52         'res_id': fields.reference('Resource', selection=_models_get, size=128,
53                                    help="If not set, acts as a default value for new resources", select=1),
54         'company_id': fields.many2one('res.company', 'Company', select=1),
55         'fields_id': fields.many2one('ir.model.fields', 'Field', ondelete='cascade', required=True, select=1),
56
57         'value_float' : fields.float('Value'),
58         'value_integer' : fields.integer('Value'),
59         'value_text' : fields.text('Value'), # will contain (char, text)
60         'value_binary' : fields.binary('Value'),
61         'value_reference': fields.reference('Value', selection=_models_get2, size=128),
62         'value_datetime' : fields.datetime('Value'),
63
64         'type' : fields.selection([('char', 'Char'),
65                                    ('float', 'Float'),
66                                    ('boolean', 'Boolean'),
67                                    ('integer', 'Integer'),
68                                    ('text', 'Text'),
69                                    ('binary', 'Binary'),
70                                    ('many2one', 'Many2One'),
71                                    ('date', 'Date'),
72                                    ('datetime', 'DateTime'),
73                                   ],
74                                   'Type',
75                                   required=True,
76                                   select=1),
77     }
78
79     _defaults = {
80         'type': 'many2one',
81     }
82
83     def _update_values(self, cr, uid, ids, values):
84         value = values.pop('value', None)
85         if not value:
86             return values
87
88         prop = None
89         type_ = values.get('type')
90         if not type_:
91             if ids:
92                 prop = self.browse(cr, uid, ids[0])
93                 type_ = prop.type
94             else:
95                 type_ = self._defaults['type']
96
97         type2field = {
98             'char': 'value_text',
99             'float': 'value_float',
100             'boolean' : 'value_integer',
101             'integer': 'value_integer',
102             'text': 'value_text',
103             'binary': 'value_binary',
104             'many2one': 'value_reference',
105             'date' : 'value_datetime',
106             'datetime' : 'value_datetime',
107         }
108         field = type2field.get(type_)
109         if not field:
110             raise osv.except_osv('Error', 'Invalid type')
111
112         if field == 'value_reference':
113             if isinstance(value, osv.orm.browse_record):
114                 value = '%s,%d' % (value._name, value.id)
115             elif isinstance(value, (int, long)):
116                 field_id = values.get('fields_id')
117                 if not field_id:
118                     if not prop:
119                         raise ValueError()
120                     field_id = prop.fields_id
121                 else:
122                     field_id = self.pool.get('ir.model.fields').browse(cr, uid, field_id)
123
124                 value = '%s,%d' % (field_id.relation, value)
125
126         values[field] = value
127         return values
128
129
130     def write(self, cr, uid, ids, values, context=None):
131         return super(ir_property, self).write(cr, uid, ids, self._update_values(cr, uid, ids, values), context=context)
132
133     def create(self, cr, uid, values, context=None):
134         return super(ir_property, self).create(cr, uid, self._update_values(cr, uid, None, values), context=context)
135
136     def get_by_record(self, cr, uid, record, context=None):
137         if record.type in ('char', 'text'):
138             return record.value_text
139         elif record.type == 'float':
140             return record.value_float
141         elif record.type == 'boolean':
142             return bool(record.value_integer)
143         elif record.type == 'integer':
144             return record.value_integer
145         elif record.type == 'binary':
146             return record.value_binary
147         elif record.type == 'many2one':
148             return record.value_reference
149         elif record.type == 'datetime':
150             return record.value_datetime
151         elif record.type == 'date':
152             if not record.value_datetime:
153                 return False
154             return time.strftime('%Y-%m-%d', time.strptime(record.value_datetime, '%Y-%m-%d %H:%M:%S'))
155         return False
156
157     def get(self, cr, uid, name, model, res_id=False, context=None):
158         domain = self._get_domain(cr, uid, name, model, context=context)
159         if domain is not None:
160             domain = [('res_id', '=', res_id)] + domain
161             nid = self.search(cr, uid, domain, context=context)
162             if not nid: return False
163             record = self.browse(cr, uid, nid[0], context=context)
164             return self.get_by_record(cr, uid, record, context=context)
165         return False
166
167     def _get_domain_default(self, cr, uid, prop_name, model, context=None):
168         domain = self._get_domain(cr, uid, prop_name, model, context=context)
169         if domain is None:
170             return None
171         return ['&', ('res_id', '=', False)] + domain
172
173     def _get_domain(self, cr, uid, prop_name, model, context=None):
174         context = context or {}
175         cr.execute('select id from ir_model_fields where name=%s and model=%s', (prop_name, model))
176         res = cr.fetchone()
177         if not res:
178             return None
179
180         if 'force_company' in context and context['force_company']:
181             cid = context['force_company']
182         else:
183             company = self.pool.get('res.company')
184             cid = company._company_default_get(cr, uid, model, res[0], context=context)
185
186         domain = ['&', ('fields_id', '=', res[0]),
187                   '|', ('company_id', '=', cid), ('company_id', '=', False)]
188         return domain
189
190 ir_property()
191
192
193
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
195