[IMP] res_users: removed hack about redirecting the fields view get. Added a simplifi...
[odoo/odoo.git] / openerp / addons / base / res / res_country.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 fields, osv
23
24 def location_name_search(self, cr, user, name='', args=None, operator='ilike',
25                          context=None, limit=100):
26     if not args:
27         args = []
28
29     ids = []
30     if len(name) == 2:
31         ids = self.search(cr, user, [('code', 'ilike', name)] + args,
32                           limit=limit, context=context)
33
34     search_domain = [('name', operator, name)]
35     if ids: search_domain.append(('id', 'not in', ids))
36     ids.extend(self.search(cr, user, search_domain + args,
37                            limit=limit, context=context))
38
39     locations = self.name_get(cr, user, ids, context)
40     return sorted(locations, key=lambda (id, name): ids.index(id))
41
42 class Country(osv.osv):
43     _name = 'res.country'
44     _description = 'Country'
45     _columns = {
46         'name': fields.char('Country Name', size=64,
47             help='The full name of the country.', required=True, translate=True),
48         'code': fields.char('Country Code', size=2,
49             help='The ISO country code in two chars.\n'
50             'You can use this field for quick search.'),
51         'address_format': fields.text('Address Format', help="""You can state here the usual format to use for the \
52 addresses belonging to this country.\n\nYou can use the python-style string patern with all the field of the address \
53 (for example, use '%(street)s' to display the field 'street') plus
54             \n%(state_name)s: the name of the state
55             \n%(state_code)s: the code of the state
56             \n%(country_name)s: the name of the country
57             \n%(country_code)s: the code of the country"""),
58         'currency_id': fields.many2one('res.currency', 'Currency'),
59     }
60     _sql_constraints = [
61         ('name_uniq', 'unique (name)',
62             'The name of the country must be unique !'),
63         ('code_uniq', 'unique (code)',
64             'The code of the country must be unique !')
65     ]
66     _defaults = {
67         'address_format': "%(street)s\n%(street2)s\n%(city)s %(state_code)s %(zip)s\n%(country_name)s",
68     }
69     _order='name'
70
71     name_search = location_name_search
72
73     def create(self, cursor, user, vals, context=None):
74         if 'code' in vals:
75             vals['code'] = vals['code'].upper()
76         return super(Country, self).create(cursor, user, vals,
77                 context=context)
78
79     def write(self, cursor, user, ids, vals, context=None):
80         if 'code' in vals:
81             vals['code'] = vals['code'].upper()
82         return super(Country, self).write(cursor, user, ids, vals,
83                 context=context)
84
85
86 class CountryState(osv.osv):
87     _description="Country state"
88     _name = 'res.country.state'
89     _columns = {
90         'country_id': fields.many2one('res.country', 'Country',
91             required=True),
92         'name': fields.char('State Name', size=64, required=True, 
93                             help='Administrative divisions of a country. E.g. Fed. State, Departement, Canton'),
94         'code': fields.char('State Code', size=3,
95             help='The state code in max. three chars.', required=True),
96     }
97     _order = 'code'
98
99     name_search = location_name_search
100
101 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
102