Files
tryton/address.py
T

124 lines
4.6 KiB
Python
Raw Normal View History

2008-10-22 20:07:32 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2008-04-08 11:26:36 +02:00
'Address'
2009-02-28 17:00:12 +01:00
from trytond.model import ModelView, ModelSQL, fields
2011-08-21 11:20:44 +02:00
from trytond.pyson import Eval, If
2011-07-03 13:19:00 +02:00
from trytond.pool import Pool
2008-04-08 11:26:36 +02:00
2008-02-26 12:41:13 +01:00
STATES = {
2011-08-21 11:20:44 +02:00
'readonly': ~Eval('active'),
}
2011-08-04 16:57:31 +02:00
DEPENDS = ['active']
2008-02-15 17:42:25 +01:00
2008-04-08 11:26:36 +02:00
2009-02-28 17:00:12 +01:00
class Address(ModelSQL, ModelView):
2008-05-21 18:12:20 +02:00
"Address"
2008-10-22 20:07:32 +02:00
_name = 'party.address'
2008-02-15 17:42:25 +01:00
_description = __doc__
2008-10-22 20:07:32 +02:00
party = fields.Many2One('party.party', 'Party', required=True,
2011-08-04 16:57:31 +02:00
ondelete='CASCADE', select=1, states={
2011-08-21 11:20:44 +02:00
'readonly': If(~Eval('active'), True, Eval('id', 0) > 0),
2011-08-04 16:57:31 +02:00
},
depends=['active', 'id'])
name = fields.Char('Name', states=STATES, depends=DEPENDS)
street = fields.Char('Street', states=STATES, depends=DEPENDS)
streetbis = fields.Char('Street (bis)', states=STATES, depends=DEPENDS)
2008-10-23 14:04:22 +02:00
zip = fields.Char('Zip', change_default=True,
2011-08-04 16:57:31 +02:00
states=STATES, depends=DEPENDS)
city = fields.Char('City', states=STATES, depends=DEPENDS)
2008-10-22 20:07:32 +02:00
country = fields.Many2One('country.country', 'Country',
2011-08-04 16:57:31 +02:00
on_change=['country', 'subdivision'], states=STATES, depends=DEPENDS)
2008-10-22 20:07:32 +02:00
subdivision = fields.Many2One("country.subdivision",
2010-01-20 11:00:56 +01:00
'Subdivision', domain=[('country', '=', Eval('country'))],
2011-08-04 16:57:31 +02:00
states=STATES, depends=['active', 'country'])
2008-02-26 12:41:13 +01:00
active = fields.Boolean('Active')
sequence = fields.Integer("Sequence")
2010-03-11 16:48:37 +01:00
full_address = fields.Function(fields.Text('Full Address'),
'get_full_address')
2008-04-22 17:48:57 +02:00
def __init__(self):
super(Address, self).__init__()
2008-05-21 18:12:20 +02:00
self._order.insert(0, ('party', 'ASC'))
2008-04-22 17:48:57 +02:00
self._order.insert(1, ('sequence', 'ASC'))
2008-07-17 19:37:09 +02:00
self._error_messages.update({
'write_party': 'You can not modify the party of an address!',
})
2008-04-22 17:48:57 +02:00
2010-08-10 11:59:01 -04:00
def default_active(self):
2008-10-23 13:38:29 +02:00
return True
2008-02-26 16:23:30 +01:00
2010-08-10 11:59:01 -04:00
def get_full_address(self, ids, name):
2008-10-22 20:07:32 +02:00
if not ids:
return {}
2008-10-22 20:07:32 +02:00
res = {}
2010-08-10 11:59:01 -04:00
for address in self.browse(ids):
2008-10-22 20:07:32 +02:00
res[address.id] = ''
if address.name:
res[address.id] += address.name
if address.street:
if res[address.id]:
res[address.id] += '\n'
res[address.id] += address.street
if address.streetbis:
if res[address.id]:
res[address.id] += '\n'
res[address.id] += address.streetbis
if address.zip or address.city:
if res[address.id]:
res[address.id] += '\n'
2008-11-12 22:24:15 +01:00
if address.zip:
res[address.id] += address.zip
if address.city:
if res[address.id][-1:] != '\n':
res[address.id] += ' '
res[address.id] += address.city
2008-10-22 20:07:32 +02:00
if address.country or address.subdivision:
if res[address.id]:
res[address.id] += '\n'
if address.subdivision:
2008-11-12 22:24:15 +01:00
res[address.id] += address.subdivision.name
2008-10-22 20:07:32 +02:00
if address.country:
2008-11-12 22:24:15 +01:00
if res[address.id][-1:] != '\n':
res[address.id] += ' '
2008-10-22 20:07:32 +02:00
res[address.id] += address.country.name
return res
2010-08-10 11:59:01 -04:00
def get_rec_name(self, ids, name):
2008-04-08 11:26:36 +02:00
if not ids:
return {}
res = {}
2010-08-10 11:59:01 -04:00
for address in self.browse(ids):
res[address.id] = ", ".join(x for x in [address.name,
address.party.rec_name, address.zip, address.city] if x)
2008-03-05 03:12:45 +01:00
return res
2010-08-10 11:59:01 -04:00
def search_rec_name(self, name, clause):
ids = self.search(['OR',
('zip',) + clause[1:],
('city',) + clause[1:],
('name',) + clause[1:],
2010-08-10 11:59:01 -04:00
], order=[])
if ids:
return [('id', 'in', ids)]
return [('party',) + clause[1:]]
2010-08-10 11:59:01 -04:00
def write(self, ids, vals):
2008-06-28 20:46:14 +02:00
if 'party' in vals:
2008-07-24 22:23:03 +02:00
if isinstance(ids, (int, long)):
ids = [ids]
2010-08-10 11:59:01 -04:00
for address in self.browse(ids):
2008-06-28 20:46:14 +02:00
if address.party.id != vals['party']:
2010-08-10 11:59:01 -04:00
self.raise_user_error('write_party')
return super(Address, self).write(ids, vals)
2008-06-28 20:46:14 +02:00
def on_change_country(self, vals):
2011-07-03 13:19:00 +02:00
subdivision_obj = Pool().get('country.subdivision')
result = dict((k, vals.get(k, False))
for k in ('country', 'subdivision'))
if vals['subdivision']:
subdivision = subdivision_obj.browse(vals['subdivision'])
if subdivision.country.id != vals['country']:
result['subdivision'] = False
return result
2008-02-15 17:42:25 +01:00
Address()