Active Record

This commit is contained in:
Cédric Krier
2012-09-24 22:17:55 +02:00
parent 2d010f9cfb
commit badf8ccb27
2 changed files with 32 additions and 51 deletions
+7
View File
@@ -1,4 +1,11 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from trytond.pool import Pool
from .address import *
def register():
Pool.register(
Address,
module='google_maps', type_='model')
+25 -51
View File
@@ -1,64 +1,38 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import urllib
from trytond.model import ModelView, ModelSQL, fields
from trytond.model import fields
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pool import PoolMeta
__all__ = ['Address']
__metaclass__ = PoolMeta
class Address(ModelSQL, ModelView):
_name = 'party.address'
class Address:
__name__ = 'party.address'
google_maps_url = fields.Function(fields.Char('Google Maps',
on_change_with=['street', 'streetbis', 'zip', 'city', 'country',
'subdivision']), 'get_google_maps_url')
on_change_with=['street', 'streetbis', 'zip', 'city', 'country',
'subdivision']), 'on_change_with_google_maps_url')
def _get_url(self, vals):
def on_change_with_google_maps_url(self, name=None):
lang = Transaction().language[:2]
url = ''
for i in ['street', 'streetbis', 'zip', 'city',
'country', 'subdivision']:
if vals.get(i):
if isinstance(vals[i], str):
url += ' ' + vals[i].decode('utf-8')
for value in (
self.street,
self.streetbis,
self.zip,
self.city,
self.country.name if self.country else None,
self.subdivision.name if self.subdivision else None,
):
if value:
if isinstance(value, str):
url += ' ' + value.decode('utf-8')
else:
url += ' ' + vals[i]
url += ' ' + value
if url.strip():
url = 'http://maps.google.com/maps?hl=%s&q=%s' % \
(lang, urllib.quote(url.strip().encode('utf-8')))
else:
url = ''
return url
def on_change_with_google_maps_url(self, vals):
country_obj = Pool().get('country.country')
subdivision_obj = Pool().get('country.subdivision')
vals = vals.copy()
if vals.get('country'):
country = country_obj.browse(vals['country'])
vals['country'] = country.name
if vals.get('subdivision'):
subdivision = subdivision_obj.browse(vals['subdivision'])
vals['subdivision'] = subdivision.name
return self._get_url(vals)
def get_google_maps_url(self, ids, name):
res = {}
for address in self.browse(ids):
vals = {
'street': address.street,
'streetbis': address.streetbis,
'zip': address.zip,
'city': address.city,
'country': address.country and address.country.name or None,
'subdivision': address.subdivision and \
address.subdivision.name or None,
}
res[address.id] = self._get_url(vals)
return res
Address()
return 'http://maps.google.com/maps?hl=%s&q=%s' % \
(lang, urllib.quote(url.strip().encode('utf-8')))
return ''