Set of label improvements to Open ERP Server.
[odoo/odoo.git] / bin / addons / base / res / country.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 from osv import fields, osv
24
25
26 class Country(osv.osv):
27     _name = 'res.country'
28     _description = 'Country'
29     _columns = {
30         'name': fields.char('Country Name', size=64,
31             help='The full name of the country.', required=True, translate=True),
32         'code': fields.char('Country Code', size=2,
33             help='The ISO country code in two chars.\n'
34             'You can use this field for quick search.', required=True),
35     }
36     _sql_constraints = [
37         ('name_uniq', 'unique (name)',
38             'The name of the country must be unique !'),
39         ('code_uniq', 'unique (code)',
40             'The code of the country must be unique !')
41     ]
42
43     def name_search(self, cr, user, name='', args=None, operator='ilike',
44             context=None, limit=80):
45         if not args:
46             args=[]
47         if not context:
48             context={}
49         ids = False
50         if len(name) == 2:
51             ids = self.search(cr, user, [('code', '=', name)] + args,
52                     limit=limit, context=context)
53         if not ids:
54             ids = self.search(cr, user, [('name', operator, name)] + args,
55                     limit=limit, context=context)
56         return self.name_get(cr, user, ids, context)
57     _order='name'
58
59     def create(self, cursor, user, vals, context=None):
60         if 'code' in vals:
61             vals['code'] = vals['code'].upper()
62         return super(Country, self).create(cursor, user, vals,
63                 context=context)
64
65     def write(self, cursor, user, ids, vals, context=None):
66         if 'code' in vals:
67             vals['code'] = vals['code'].upper()
68         return super(Country, self).write(cursor, user, ids, vals,
69                 context=context)
70
71 Country()
72
73
74 class CountryState(osv.osv):
75     _description="Country state"
76     _name = 'res.country.state'
77     _columns = {
78         'country_id': fields.many2one('res.country', 'Country',
79             required=True),
80         'name': fields.char('State Name', size=64, required=True),
81         'code': fields.char('State Code', size=3,
82             help='The state code in three chars.\n', required=True),
83     }
84     def name_search(self, cr, user, name='', args=None, operator='ilike',
85             context=None, limit=80):
86         if not args:
87             args = []
88         if not context:
89             context = {}
90         ids = self.search(cr, user, [('code', '=', name)] + args, limit=limit,
91                 context=context)
92         if not ids:
93             ids = self.search(cr, user, [('name', operator, name)] + args,
94                     limit=limit, context=context)
95         return self.name_get(cr, user, ids, context)
96
97     _order = 'code'
98 CountryState()
99
100
101
102 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
103