From badf8ccb27e5951b771aed9d2ac033fdfffe3a27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 24 Sep 2012 22:17:55 +0200 Subject: [PATCH] Active Record --- __init__.py | 7 +++++ address.py | 76 ++++++++++++++++++----------------------------------- 2 files changed, 32 insertions(+), 51 deletions(-) diff --git a/__init__.py b/__init__.py index 5a83e939f3..f033af4f89 100644 --- a/__init__.py +++ b/__init__.py @@ -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') diff --git a/address.py b/address.py index 3be971b851..064eacad74 100644 --- a/address.py +++ b/address.py @@ -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 ''