#284 Empty API data

* adds proper message for certain data parsing in case of an error
This commit is contained in:
mpeltriaux 2023-02-01 07:03:54 +01:00
parent 794001a8ae
commit 123d02abf9

View File

@ -297,9 +297,12 @@ class AbstractCompensationAPISerializerV1Mixin:
""" """
deadlines = [] deadlines = []
for entry in deadline_data: for entry in deadline_data:
deadline_type = entry["type"] try:
date = entry["date"] deadline_type = entry["type"]
comment = entry["comment"] date = entry["date"]
comment = entry["comment"]
except KeyError:
raise ValueError(f"Invalid deadline content. Content was {entry} but should follow the specification")
# Check on validity # Check on validity
if deadline_type not in DeadlineType: if deadline_type not in DeadlineType:
@ -341,11 +344,14 @@ class AbstractCompensationAPISerializerV1Mixin:
""" """
states = [] states = []
for entry in states_data: for entry in states_data:
biotope_type = entry["biotope"] try:
biotope_details = [ biotope_type = entry["biotope"]
self._konova_code_from_json(e, CODELIST_BIOTOPES_EXTRA_CODES_ID) for e in entry["biotope_details"] biotope_details = [
] self._konova_code_from_json(e, CODELIST_BIOTOPES_EXTRA_CODES_ID) for e in entry["biotope_details"]
surface = float(entry["surface"]) ]
surface = float(entry["surface"])
except KeyError:
raise ValueError(f"Invalid biotope content. Content was {entry} but should follow the specification ")
# Check on validity # Check on validity
if surface <= 0: if surface <= 0:
@ -385,16 +391,19 @@ class AbstractCompensationAPISerializerV1Mixin:
""" """
actions = [] actions = []
for entry in actions_data: for entry in actions_data:
action_types = [ try:
self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_ID) for e in entry["action_types"] action_types = [
] self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_ID) for e in entry["action_types"]
action_details = [ ]
self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_DETAIL_ID) for e in entry["action_details"] action_details = [
] self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_DETAIL_ID) for e in entry["action_details"]
amount = float(entry["amount"]) ]
# Mapping of old "qm" into "m²" amount = float(entry["amount"])
unit = UnitChoices.m2.value if entry["unit"] == "qm" else entry["unit"] # Mapping of old "qm" into "m²"
comment = entry["comment"] unit = UnitChoices.m2.value if entry["unit"] == "qm" else entry["unit"]
comment = entry["comment"]
except KeyError:
raise ValueError(f"Invalid action content. Content was {entry} but should follow specification")
# Check on validity # Check on validity
if amount <= 0: if amount <= 0: