wpint: More input data validation for content_item and item

The limits are documented in Table 9 of "Anbindungleitfaden"
This commit is contained in:
Harald Welte 2021-03-07 11:17:49 +01:00
parent efb6209c9d
commit f8e81db8cb
1 changed files with 18 additions and 3 deletions

View File

@ -185,10 +185,20 @@ class WarenpostInt(object):
desc=None): desc=None):
"""Build an 'content item' in the language of the WaPoInt API. Represents one """Build an 'content item' in the language of the WaPoInt API. Represents one
line on the customs form.""" line on the customs form."""
line_weight_g = int(line_weight_g)
if line_weight_g > 2000:
raise ValueError('Maximum line weight is 2000g')
qty = int(qty)
if qty > 99 or qty < 1:
raise ValueError('Maximum line quantity is 99')
if desc and len(desc) > 33:
raise ValueError('Maximum length of contentPieceDescription is 33 chars')
if hs_code and (len(hs_code) < 4 or len(hs_code) > 10):
raise ValueError('HS-Code must be between 4 and 10 characters long')
ret = { ret = {
'contentPieceNetweight': line_weight_g, 'contentPieceNetweight': line_weight_g,
'contentPieceValue': str(int(line_value)), 'contentPieceValue': "%.2f" % (line_value),
'contentPieceAmount': int(qty), 'contentPieceAmount': qty,
} }
if hs_code: if hs_code:
ret['contentPieceHsCode'] = str(hs_code) ret['contentPieceHsCode'] = str(hs_code)
@ -201,12 +211,17 @@ class WarenpostInt(object):
def build_item(self, product, sender, recipient, weight_grams, amount=0, currency='EUR', def build_item(self, product, sender, recipient, weight_grams, amount=0, currency='EUR',
contents=None): contents=None):
"""Build an 'item' in the language of the WaPoInt API. Represents one shipment.""" """Build an 'item' in the language of the WaPoInt API. Represents one shipment."""
weight_grams = int(weight_grams)
if weight_grams > 2000:
raise ValueError('Maximum item gross weight is 2000g')
if len(currency) != 3:
raise ValueError('Currency must be expressed as 3-digit ISO-4217 code')
ret = { ret = {
'product': str(product), 'product': str(product),
'serviceLevel': 'STANDARD', 'serviceLevel': 'STANDARD',
'shipmentAmount': int(amount), 'shipmentAmount': int(amount),
'shipmentCurrency': currency, 'shipmentCurrency': currency,
'shipmentGrossWeight': int(weight_grams), 'shipmentGrossWeight': weight_grams,
'shipmentNaturetype': 'SALE_GOODS', 'shipmentNaturetype': 'SALE_GOODS',
} }
# merge in the sender and recipient fields # merge in the sender and recipient fields