2008-07-23 15:01:27 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2007-09-14 13:42:06 +00:00
|
|
|
##############################################################################
|
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
2009-01-04 22:13:29 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
2008-11-03 18:27:16 +00:00
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-09-14 13:42:06 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import fields, osv
|
|
|
|
|
|
|
|
|
|
|
|
class Country(osv.osv):
|
2008-07-22 14:24:36 +00:00
|
|
|
_name = 'res.country'
|
|
|
|
_description = 'Country'
|
|
|
|
_columns = {
|
|
|
|
'name': fields.char('Country Name', size=64,
|
2009-01-22 07:44:44 +00:00
|
|
|
help='The full name of the country.', required=True, translate=True),
|
2008-07-22 14:24:36 +00:00
|
|
|
'code': fields.char('Country Code', size=2,
|
|
|
|
help='The ISO country code in two chars.\n'
|
|
|
|
'You can use this field for quick search.', required=True),
|
|
|
|
}
|
|
|
|
_sql_constraints = [
|
|
|
|
('name_uniq', 'unique (name)',
|
|
|
|
'The name of the country must be unique !'),
|
|
|
|
('code_uniq', 'unique (code)',
|
|
|
|
'The code of the country must be unique !')
|
|
|
|
]
|
2007-09-14 13:42:06 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def name_search(self, cr, user, name='', args=None, operator='ilike',
|
|
|
|
context=None, limit=80):
|
|
|
|
if not args:
|
|
|
|
args=[]
|
|
|
|
if not context:
|
|
|
|
context={}
|
|
|
|
ids = False
|
|
|
|
if len(name) == 2:
|
2008-09-10 21:43:11 +00:00
|
|
|
ids = self.search(cr, user, [('code', '=', name)] + args,
|
2008-07-22 14:24:36 +00:00
|
|
|
limit=limit, context=context)
|
|
|
|
if not ids:
|
|
|
|
ids = self.search(cr, user, [('name', operator, name)] + args,
|
|
|
|
limit=limit, context=context)
|
|
|
|
return self.name_get(cr, user, ids, context)
|
|
|
|
_order='name'
|
2007-09-14 13:42:06 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def create(self, cursor, user, vals, context=None):
|
|
|
|
if 'code' in vals:
|
|
|
|
vals['code'] = vals['code'].upper()
|
|
|
|
return super(Country, self).create(cursor, user, vals,
|
|
|
|
context=context)
|
2007-09-14 13:42:06 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def write(self, cursor, user, ids, vals, context=None):
|
|
|
|
if 'code' in vals:
|
|
|
|
vals['code'] = vals['code'].upper()
|
|
|
|
return super(Country, self).write(cursor, user, ids, vals,
|
|
|
|
context=context)
|
2007-09-14 13:42:06 +00:00
|
|
|
|
|
|
|
Country()
|
|
|
|
|
|
|
|
|
|
|
|
class CountryState(osv.osv):
|
2008-07-22 14:24:36 +00:00
|
|
|
_description="Country state"
|
|
|
|
_name = 'res.country.state'
|
|
|
|
_columns = {
|
|
|
|
'country_id': fields.many2one('res.country', 'Country',
|
|
|
|
required=True),
|
|
|
|
'name': fields.char('State Name', size=64, required=True),
|
|
|
|
'code': fields.char('State Code', size=3, required=True),
|
|
|
|
}
|
|
|
|
def name_search(self, cr, user, name='', args=None, operator='ilike',
|
|
|
|
context=None, limit=80):
|
|
|
|
if not args:
|
|
|
|
args = []
|
|
|
|
if not context:
|
|
|
|
context = {}
|
|
|
|
ids = self.search(cr, user, [('code', '=', name)] + args, limit=limit,
|
|
|
|
context=context)
|
|
|
|
if not ids:
|
|
|
|
ids = self.search(cr, user, [('name', operator, name)] + args,
|
|
|
|
limit=limit, context=context)
|
|
|
|
return self.name_get(cr, user, ids, context)
|
2007-09-14 13:42:06 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
_order = 'code'
|
2007-09-14 13:42:06 +00:00
|
|
|
CountryState()
|
|
|
|
|
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|