[IMP] corrected a few titles vs. "shortcuts" and added three titles to res.partner...
[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
25 class Country(osv.osv):
26     _name = 'res.country'
27     _description = 'Country'
28     _columns = {
29         'name': fields.char('Country Name', size=64,
30             help='The full name of the country.', required=True, translate=True),
31         'code': fields.char('Country Code', size=2,
32             help='The ISO country code in two chars.\n'
33             'You can use this field for quick search.', required=True),
34         'address_format': fields.text('Address Format', help="""You can state here the usual format to use for the \
35 addresses belonging to this country.\n\nYou can use the python-style string patern with all the field of the address \
36 (for example, use '%(street)s' to display the field 'street') plus
37             \n%(state_name)s: the name of the state
38             \n%(state_code)s: the code of the state
39             \n%(country_name)s: the name of the country
40             \n%(country_code)s: the code of the country"""),
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     _defaults = {
49         'address_format': "%(street)s\n%(street2)s\n%(city)s,%(state_code)s %(zip)s\n%(country_name)s",
50     }
51
52     def name_search(self, cr, user, name='', args=None, operator='ilike',
53             context=None, limit=100):
54         if not args:
55             args=[]
56         if not context:
57             context={}
58         ids = False
59         if len(name) == 2:
60             ids = self.search(cr, user, [('code', 'ilike', name)] + args,
61                     limit=limit, context=context)
62         if not ids:
63             ids = self.search(cr, user, [('name', operator, name)] + args,
64                     limit=limit, context=context)
65         return self.name_get(cr, user, ids, context)
66     _order='name'
67
68     def create(self, cursor, user, vals, context=None):
69         if 'code' in vals:
70             vals['code'] = vals['code'].upper()
71         return super(Country, self).create(cursor, user, vals,
72                 context=context)
73
74     def write(self, cursor, user, ids, vals, context=None):
75         if 'code' in vals:
76             vals['code'] = vals['code'].upper()
77         return super(Country, self).write(cursor, user, ids, vals,
78                 context=context)
79
80 Country()
81
82
83 class CountryState(osv.osv):
84     _description="Country state"
85     _name = 'res.country.state'
86     _columns = {
87         'country_id': fields.many2one('res.country', 'Country',
88             required=True),
89         'name': fields.char('State Name', size=64, required=True, 
90                             help='Administrative divisions of a country. E.g. Fed. State, Departement, Canton'),
91         'code': fields.char('State Code', size=3,
92             help='The state code in max. three chars.', required=True),
93     }
94     def name_search(self, cr, user, name='', args=None, operator='ilike',
95             context=None, limit=100):
96         if not args:
97             args = []
98         if not context:
99             context = {}
100         ids = self.search(cr, user, [('code', 'ilike', name)] + args, limit=limit,
101                 context=context)
102         if not ids:
103             ids = self.search(cr, user, [('name', operator, name)] + args,
104                     limit=limit, context=context)
105         return self.name_get(cr, user, ids, context)
106
107     _order = 'code'
108 CountryState()
109
110
111
112 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
113