diff --git a/address.py b/address.py index 5061b2332f..8a179494c1 100644 --- a/address.py +++ b/address.py @@ -128,20 +128,24 @@ class Address(sequence_ordered(), ModelSQL, ModelView): context = Transaction().context substitutions = { 'party_name': '', - 'name': self.name or '', - 'street': self.street or '', - 'zip': self.zip or '', - 'city': self.city or '', - 'subdivision': self.subdivision.name if self.subdivision else '', + 'name': getattr(self, 'name', None) or '', + 'street': getattr(self, 'street', None) or '', + 'zip': getattr(self, 'zip', None) or '', + 'city': getattr(self, 'city', None) or '', + 'subdivision': (self.subdivision.name + if getattr(self, 'subdivision', None) else ''), 'subdivision_code': (self.subdivision.code.split('-', 1)[1] - if self.subdivision else ''), - 'country': self.country.name if self.country else '', - 'country_code': self.country.code if self.country else '', + if getattr(self, 'subdivision', None) else ''), + 'country': (self.country.name + if getattr(self, 'country', None) else ''), + 'country_code': (self.country.code + if getattr(self, 'country', None) else ''), } - if context.get('address_from_country') == self.country: + if context.get('address_from_country') == getattr(self, 'country', ''): substitutions['country'] = '' if context.get('address_with_party', False): - substitutions['party_name'] = self.party.full_name + substitutions['party_name'] = (self.party.full_name + if getattr(self, 'party', None) else '') for key, value in substitutions.items(): substitutions[key.upper()] = value.upper() return substitutions @@ -205,6 +209,10 @@ class AddressFormat(MatchMixin, ModelSQL, ModelView): super(AddressFormat, cls).__setup__() cls._order.insert(0, ('country', 'ASC')) cls._order.insert(1, ('language', 'ASC')) + cls._error_messages.update({ + 'invalid_format': ('Invalid format "%(format)s" ' + 'with exception "%(exception)s".'), + }) @classmethod def default_active(cls): @@ -240,6 +248,25 @@ ${COUNTRY}""" super(AddressFormat, cls).delete(*args, **kwargs) cls._get_format_cache.clear() + @classmethod + def validate(cls, formats): + super(AddressFormat, cls).validate(formats) + for format_ in formats: + format_.check_format() + + def check_format(self): + pool = Pool() + Address = pool.get('party.address') + address = Address() + try: + Template(self.format_).substitute( + **address._get_address_substitutions()) + except Exception, exception: + self.raise_user_error('invalid_format', { + 'format': self.format_, + 'exception': exception, + }) + @classmethod def get_format(cls, address, pattern=None): pool = Pool() diff --git a/address.xml b/address.xml index ecb3fea51f..dbeec22b44 100644 --- a/address.xml +++ b/address.xml @@ -268,7 +268,7 @@ ${COUNTRY} ${party_name} ${name} ${street} -${district} +${subdivision} ${COUNTRY} @@ -297,7 +297,7 @@ ${COUNTRY} ${name} ${street} ${CITY} ${zip} -${district} +${subdivision} ${COUNTRY} @@ -307,7 +307,7 @@ ${COUNTRY} ${name} ${street} ${city} ${zip} -${district} +${subdivision} ${COUNTRY} @@ -317,7 +317,7 @@ ${COUNTRY} ${name} ${city} ${street} -${district} +${subdivision} ${zip} ${COUNTRY} @@ -327,7 +327,7 @@ ${COUNTRY} ${party_name} ${name} ${street} -${district} +${subdivision} ${zip} ${COUNTRY} @@ -355,7 +355,7 @@ ${COUNTRY} ${party_name} ${name} ${street} -${zip} ${city} ${DISTRICT_CODE} +${zip} ${city} ${SUBDIVISION_CODE} ${COUNTRY} @@ -386,7 +386,7 @@ ${COUNTRY} ${name} ${street} ${city} -${district} +${subdivision} ${COUNTRY_CODE}-${ZIP} ${COUNTRY} @@ -472,7 +472,7 @@ ${name} ${street} ${city} ${zip} -${district} +${subdivision} ${COUNTRY} @@ -482,7 +482,7 @@ ${COUNTRY} ${name} ${street} ${city} -${district} +${subdivision} ${COUNTRY} @@ -592,7 +592,7 @@ ${COUNTRY} ${COUNTRY} -${district}${city}${street} +${subdivision}${city}${street} ${zip} --> @@ -657,7 +657,7 @@ ${name}${party_name} ${name} ${party_name} ${street} -${city}, ${district} ${zip} +${city}, ${subdivision} ${zip} ${COUNTRY} @@ -666,7 +666,7 @@ ${COUNTRY} ${party_name} ${street} ${name} -${district} +${subdivision} ${COUNTRY} ${zip} @@ -676,7 +676,7 @@ ${zip} ${party_name} ${name} ${street} -${zip} ${city} ${district} +${zip} ${city} ${subdivision} ${COUNTRY} @@ -686,7 +686,7 @@ ${COUNTRY} ${name} ${street} ${city} -${district} +${subdivision} ${zip} ${COUNTRY} @@ -715,7 +715,7 @@ ${COUNTRY} ${party_name} (${name}) ${street} ${city} -${district} +${subdivision} ${COUNTRY}