80b436b0be11a0141e33fac4c12fe99c0c341982
[odoo/odoo.git] / bin / addons / base / res / country.py
1 ##############################################################################
2 #
3 # Copyright (c) 2007 TINY SPRL. (http://tiny.be) All Rights Reserved.
4 #
5 #
6 # WARNING: This program as such is intended to be used by professional
7 # programmers who take the whole responsability of assessing all potential
8 # consequences resulting from its eventual inadequacies and bugs
9 # End users who are looking for a ready-to-use solution with commercial
10 # garantees and support are strongly adviced to contract a Free Software
11 # Service Company
12 #
13 # This program is Free Software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 ##############################################################################
28
29 from osv import fields, osv
30
31
32 class Country(osv.osv):
33         _name = 'res.country'
34         _description = 'Country'
35         _columns = {
36                 'name': fields.char('Country Name', size=64,
37                         help='The full name of the country.', required=True),
38                 'code': fields.char('Country Code', size=2,
39                         help='The ISO country code in two chars.\n'
40                         'You can use this field for quick search.', required=True),
41         }
42         _sql_constraints = [
43                 ('name_uniq', 'unique (name)',
44                         'The name of the country must be unique !'),
45                 ('code_uniq', 'unique (code)',
46                         'The code of the country must be unique !')
47         ]
48
49         def name_search(self, cr, user, name='', args=None, operator='ilike',
50                         context=None, limit=80):
51                 if not args:
52                         args=[]
53                 if not context:
54                         context={}
55                 ids = False
56                 if len(name) == 2:
57                         ids = self.search(cr, user, [('code', '=', name.upper())] + args,
58                                         limit=limit, context=context)
59                 if not ids:
60                         ids = self.search(cr, user, [('name', operator, name)] + args,
61                                         limit=limit, context=context)
62                 return self.name_get(cr, user, ids, context)
63         _order='name'
64
65         def create(self, cursor, user, vals, context=None):
66                 if 'code' in vals:
67                         vals['code'] = vals['code'].upper()
68                 return super(Country, self).create(cursor, user, vals,
69                                 context=context)
70
71         def write(self, cursor, user, ids, vals, context=None):
72                 if 'code' in vals:
73                         vals['code'] = vals['code'].upper()
74                 return super(Country, self).write(cursor, user, ids, vals,
75                                 context=context)
76
77 Country()
78
79
80 class CountryState(osv.osv):
81         _description="Country state"
82         _name = 'res.country.state'
83         _columns = {
84                 'country_id': fields.many2one('res.country', 'Country',
85                         required=True),
86                 'name': fields.char('State Name', size=64, required=True),
87                 'code': fields.char('State Code', size=3, required=True),
88         }
89         def name_search(self, cr, user, name='', args=None, operator='ilike',
90                         context=None, limit=80):
91                 if not args:
92                         args = []
93                 if not context:
94                         context = {}
95                 ids = self.search(cr, user, [('code', '=', name)] + args, limit=limit,
96                                 context=context)
97                 if not ids:
98                         ids = self.search(cr, user, [('name', operator, name)] + args,
99                                         limit=limit, context=context)
100                 return self.name_get(cr, user, ids, context)
101
102         _order = 'code'
103 CountryState()
104
105