#31 API PUT Compensation
* adds support for PUT compensation (Update) * improves updating of related objects * adds missing payment PUT support for intervention API
This commit is contained in:
@@ -189,32 +189,36 @@ class AbstractCompensationAPISerializerV1Mixin:
|
||||
Returns:
|
||||
obj (Compensation)
|
||||
"""
|
||||
found_deadlines = []
|
||||
deadlines = []
|
||||
for entry in deadline_data:
|
||||
deadline_type = entry["type"]
|
||||
date = entry["date"]
|
||||
comment = entry["comment"]
|
||||
|
||||
# Check on validity
|
||||
if deadline_type not in DeadlineType:
|
||||
raise ValueError(f"Invalid deadline type. Choices are {DeadlineType.values}")
|
||||
|
||||
pre_existing_deadlines = obj.deadlines.filter(
|
||||
# If this exact data is already existing, we do not create it new. Instead put it's id in the list of
|
||||
# entries, we will use to set the new actions
|
||||
pre_existing_deadline = obj.deadlines.filter(
|
||||
type=deadline_type,
|
||||
date=date,
|
||||
comment=comment,
|
||||
).exclude(
|
||||
id__in=found_deadlines
|
||||
)
|
||||
if pre_existing_deadlines.count() > 0:
|
||||
found_deadlines += pre_existing_deadlines.values_list("id", flat=True)
|
||||
id__in=deadlines
|
||||
).first()
|
||||
if pre_existing_deadline is not None:
|
||||
deadlines.append(pre_existing_deadline.id)
|
||||
else:
|
||||
# Create!
|
||||
# Create and add id to list
|
||||
new_deadline = Deadline.objects.create(
|
||||
type=deadline_type,
|
||||
date=date,
|
||||
comment=comment,
|
||||
)
|
||||
obj.deadlines.add(new_deadline)
|
||||
deadlines.append(new_deadline.id)
|
||||
obj.deadlines.set(deadlines)
|
||||
return obj
|
||||
|
||||
def set_compensation_states(self, obj, states_data, states_manager):
|
||||
@@ -229,27 +233,34 @@ class AbstractCompensationAPISerializerV1Mixin:
|
||||
Returns:
|
||||
obj (Compensation)
|
||||
"""
|
||||
found_states = []
|
||||
states = []
|
||||
for entry in states_data:
|
||||
biotope_type = entry["biotope"]
|
||||
surface = float(entry["surface"])
|
||||
|
||||
# Check on validity
|
||||
if surface <= 0:
|
||||
raise ValueError("State surfaces must be > 0")
|
||||
pre_existing_states = states_manager.filter(
|
||||
|
||||
# If this exact data is already existing, we do not create it new. Instead put it's id in the list of
|
||||
# entries, we will use to set the new actions
|
||||
pre_existing_state = states_manager.filter(
|
||||
biotope_type__atom_id=biotope_type,
|
||||
surface=surface,
|
||||
).exclude(
|
||||
id__in=found_states
|
||||
)
|
||||
if pre_existing_states.count() > 0:
|
||||
found_states += pre_existing_states.values_list("id", flat=True)
|
||||
id__in=states
|
||||
).first()
|
||||
if pre_existing_state is not None:
|
||||
states.append(pre_existing_state.id)
|
||||
else:
|
||||
# Create!
|
||||
# Create and add id to list
|
||||
new_state = CompensationState.objects.create(
|
||||
biotope_type=self.konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID),
|
||||
surface=surface
|
||||
)
|
||||
states_manager.add(new_state)
|
||||
states.append(new_state.id)
|
||||
|
||||
states_manager.set(states)
|
||||
return obj
|
||||
|
||||
def set_compensation_actions(self, obj, actions_data):
|
||||
@@ -263,36 +274,41 @@ class AbstractCompensationAPISerializerV1Mixin:
|
||||
Returns:
|
||||
obj (Compensation)
|
||||
"""
|
||||
found_actions = []
|
||||
actions = []
|
||||
for entry in actions_data:
|
||||
action = entry["action"]
|
||||
amount = float(entry["amount"])
|
||||
unit = entry["unit"]
|
||||
comment = entry["comment"]
|
||||
|
||||
# Check on validity
|
||||
if amount <= 0:
|
||||
raise ValueError("Action amount must be > 0")
|
||||
if unit not in UnitChoices:
|
||||
raise ValueError(f"Invalid unit. Choices are {UnitChoices.values}")
|
||||
pre_existing_actions = obj.actions.filter(
|
||||
|
||||
# If this exact data is already existing, we do not create it new. Instead put it's id in the list of
|
||||
# entries, we will use to set the new actions
|
||||
pre_existing_action = obj.actions.filter(
|
||||
action_type__atom_id=action,
|
||||
amount=amount,
|
||||
unit=unit,
|
||||
comment=comment,
|
||||
).exclude(
|
||||
id__in=found_actions
|
||||
)
|
||||
if pre_existing_actions.count() > 0:
|
||||
found_actions += pre_existing_actions.values_list("id", flat=True)
|
||||
id__in=actions
|
||||
).first()
|
||||
if pre_existing_action is not None:
|
||||
actions.append(pre_existing_action.id)
|
||||
else:
|
||||
# Create!
|
||||
# Create and add id to list
|
||||
new_action = CompensationAction.objects.create(
|
||||
action_type=self.konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
|
||||
amount=amount,
|
||||
unit=unit,
|
||||
comment=comment,
|
||||
)
|
||||
obj.actions.add(new_action)
|
||||
actions.append(new_action.id)
|
||||
obj.actions.set(actions)
|
||||
return obj
|
||||
|
||||
def compensation_state_to_json(self, qs: QuerySet):
|
||||
|
||||
Reference in New Issue
Block a user