#31 API POST Compensation

* adds documentations
* adds check for valid deadline type
This commit is contained in:
mpeltriaux 2022-01-24 15:04:20 +01:00
parent 08ab36ffda
commit b3b9bfca09

View File

@ -11,7 +11,7 @@ from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID
from compensation.models import Compensation, CompensationAction, CompensationState, UnitChoices from compensation.models import Compensation, CompensationAction, CompensationState, UnitChoices
from intervention.models import Intervention from intervention.models import Intervention
from konova.models import Geometry, Deadline from konova.models import Geometry, Deadline, DeadlineType
from konova.tasks import celery_update_parcels from konova.tasks import celery_update_parcels
from user.models import UserActionLogEntry from user.models import UserActionLogEntry
@ -67,7 +67,7 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1):
return obj return obj
def set_intervention(self, obj, intervention_id, user): def set_intervention(self, obj, intervention_id, user):
""" Sets the linked intervention according to the given id """ Sets the linked compensation according to the given id
Fails if no such intervention found or user has no shared access Fails if no such intervention found or user has no shared access
@ -89,12 +89,25 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1):
return obj return obj
def set_deadlines(self, obj, deadline_data): def set_deadlines(self, obj, deadline_data):
""" Sets the linked deadline data according to the given deadline_data
Args:
obj (Compensation): The Compensation object
deadline_data (dict): The posted deadline_data
Returns:
obj (Compensation)
"""
found_deadlines = [] found_deadlines = []
for entry in deadline_data: for entry in deadline_data:
deadline_type = entry["type"] deadline_type = entry["type"]
date = entry["date"] date = entry["date"]
comment = entry["comment"] comment = entry["comment"]
if deadline_type not in DeadlineType:
raise ValueError(f"Invalid deadline type. Choices are {DeadlineType.values}")
pre_existing_deadlines = obj.deadlines.filter( pre_existing_deadlines = obj.deadlines.filter(
type=deadline_type, type=deadline_type,
date=date, date=date,
@ -115,6 +128,17 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1):
return obj return obj
def set_compensation_states(self, obj, states_data, states_manager): def set_compensation_states(self, obj, states_data, states_manager):
""" Sets the linked compensation state data according to the given states_data
Args:
obj (Compensation): The Compensation object
states_data (dict): The posted states_data
states_manager (Manager): The before_states or after_states manager
Returns:
obj (Compensation)
"""
found_states = [] found_states = []
for entry in states_data: for entry in states_data:
biotope_type = entry["biotope"] biotope_type = entry["biotope"]
@ -139,6 +163,16 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1):
return obj return obj
def set_compensation_actions(self, obj, actions_data): def set_compensation_actions(self, obj, actions_data):
""" Sets the linked compensation action data according to the given actions_data
Args:
obj (Compensation): The Compensation object
actions_data (dict): The posted actions_data
Returns:
obj (Compensation)
"""
found_actions = [] found_actions = []
for entry in actions_data: for entry in actions_data:
action = entry["action"] action = entry["action"]