Add validation of address format
The substitutions "district" and "district_code" have no counterpart in the address model and are thus replaced with the semantic equivalents "subdivision" and "subdivision_code". issue6111 review29831002
This commit is contained in:
1 parent
351c71bb31
commit
c8e8003131
2 files changed
+52
-25
No files matched your search
+37
-10
@@ -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()
|
||||
|
||||
+15
-15
@@ -268,7 +268,7 @@ ${COUNTRY}</field>
|
||||
<field name="format_">${party_name}
|
||||
${name}
|
||||
${street}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -297,7 +297,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${street}
|
||||
${CITY} ${zip}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -307,7 +307,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${street}
|
||||
${city} ${zip}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -317,7 +317,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${city}
|
||||
${street}
|
||||
${district}
|
||||
${subdivision}
|
||||
${zip}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
@@ -327,7 +327,7 @@ ${COUNTRY}</field>
|
||||
<field name="format_">${party_name}
|
||||
${name}
|
||||
${street}
|
||||
${district}
|
||||
${subdivision}
|
||||
${zip}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
@@ -355,7 +355,7 @@ ${COUNTRY}</field>
|
||||
<field name="format_">${party_name}
|
||||
${name}
|
||||
${street}
|
||||
${zip} ${city} ${DISTRICT_CODE}
|
||||
${zip} ${city} ${SUBDIVISION_CODE}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -386,7 +386,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${street}
|
||||
${city}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY_CODE}-${ZIP}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
@@ -472,7 +472,7 @@ ${name}
|
||||
${street}
|
||||
${city}
|
||||
${zip}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -482,7 +482,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${street}
|
||||
${city}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -592,7 +592,7 @@ ${COUNTRY}</field>
|
||||
<field name="country" ref="country.kr"/>
|
||||
<field name="language" ref="ir.lang_kp_KR"/>
|
||||
<field name="format_">${COUNTRY}
|
||||
${district}${city}${street}
|
||||
${subdivision}${city}${street}
|
||||
${zip}</field>
|
||||
</record>
|
||||
-->
|
||||
@@ -657,7 +657,7 @@ ${name}${party_name}</field>
|
||||
<field name="country" ref="country.tw"/>
|
||||
<field name="format_">${name} ${party_name}
|
||||
${street}
|
||||
${city}, ${district} ${zip}
|
||||
${city}, ${subdivision} ${zip}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -666,7 +666,7 @@ ${COUNTRY}</field>
|
||||
<field name="format_">${party_name}
|
||||
${street}
|
||||
${name}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}
|
||||
${zip}</field>
|
||||
</record>
|
||||
@@ -676,7 +676,7 @@ ${zip}</field>
|
||||
<field name="format_">${party_name}
|
||||
${name}
|
||||
${street}
|
||||
${zip} ${city} ${district}
|
||||
${zip} ${city} ${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
@@ -686,7 +686,7 @@ ${COUNTRY}</field>
|
||||
${name}
|
||||
${street}
|
||||
${city}
|
||||
${district}
|
||||
${subdivision}
|
||||
${zip}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
@@ -715,7 +715,7 @@ ${COUNTRY}</field>
|
||||
<field name="format_">${party_name} (${name})
|
||||
${street}
|
||||
${city}
|
||||
${district}
|
||||
${subdivision}
|
||||
${COUNTRY}</field>
|
||||
</record>
|
||||
|
||||
|
||||
Reference in new issue
Block a user