#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:
parent
59c5caf8ac
commit
fa6603f218
@ -78,16 +78,21 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
|||||||
Returns:
|
Returns:
|
||||||
obj (Compensation)
|
obj (Compensation)
|
||||||
"""
|
"""
|
||||||
|
if obj.intervention.id == intervention_id:
|
||||||
|
# Nothing to do here
|
||||||
|
return obj
|
||||||
|
|
||||||
intervention = Intervention.objects.get(
|
intervention = Intervention.objects.get(
|
||||||
id=intervention_id,
|
id=intervention_id,
|
||||||
)
|
)
|
||||||
is_shared = intervention.is_shared_with(user)
|
is_shared = intervention.is_shared_with(user)
|
||||||
|
|
||||||
if not is_shared:
|
if not is_shared:
|
||||||
raise PermissionError("Intervention not shared with user")
|
raise PermissionError("Intervention not shared with user")
|
||||||
|
|
||||||
obj.intervention = intervention
|
obj.intervention = intervention
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def create_model_from_json(self, json_model, user):
|
def create_model_from_json(self, json_model, user):
|
||||||
""" Creates a new entry for the model based on the contents of json_model
|
""" Creates a new entry for the model based on the contents of json_model
|
||||||
|
|
||||||
@ -123,6 +128,23 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
|||||||
|
|
||||||
return obj.id
|
return obj.id
|
||||||
|
|
||||||
|
def get_obj_from_db(self, id, user):
|
||||||
|
""" Returns the object from database
|
||||||
|
|
||||||
|
Fails if id not found or user does not have shared access
|
||||||
|
|
||||||
|
Args:
|
||||||
|
id (str): The object's id
|
||||||
|
user (User): The API user
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.model.objects.get(
|
||||||
|
id=id,
|
||||||
|
intervention__users__in=[user]
|
||||||
|
)
|
||||||
|
|
||||||
def update_model_from_json(self, id, json_model, user):
|
def update_model_from_json(self, id, json_model, user):
|
||||||
""" Updates an entry for the model based on the contents of json_model
|
""" Updates an entry for the model based on the contents of json_model
|
||||||
|
|
||||||
@ -135,21 +157,28 @@ class CompensationAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensa
|
|||||||
created_id (str): The id of the newly created Compensation entry
|
created_id (str): The id of the newly created Compensation entry
|
||||||
"""
|
"""
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
update_action = UserActionLogEntry.get_edited_action(user, "API update")
|
||||||
obj = self.get_obj_from_db(id, user)
|
obj = self.get_obj_from_db(id, user)
|
||||||
|
|
||||||
# Fill in data to objects
|
# Fill in data to objects
|
||||||
properties = json_model["properties"]
|
properties = json_model["properties"]
|
||||||
obj.title = properties["title"]
|
obj.title = properties["title"]
|
||||||
self.set_responsibility(obj, properties["responsible"])
|
obj.is_cef = properties["is_cef"]
|
||||||
self.set_legal(obj, properties["legal"])
|
obj.is_coherence_keeping = properties["is_coherence_keeping"]
|
||||||
|
obj.modified = update_action
|
||||||
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
||||||
|
obj.geometry.modified = update_action
|
||||||
|
obj = self.set_intervention(obj, properties["intervention"], user)
|
||||||
|
|
||||||
obj.responsible.save()
|
|
||||||
obj.geometry.save()
|
obj.geometry.save()
|
||||||
obj.legal.save()
|
|
||||||
obj.save()
|
obj.save()
|
||||||
|
|
||||||
obj.users.add(user)
|
obj = self.set_compensation_actions(obj, properties["actions"])
|
||||||
|
obj = self.set_compensation_states(obj, properties["before_states"], obj.before_states)
|
||||||
|
obj = self.set_compensation_states(obj, properties["after_states"], obj.after_states)
|
||||||
|
obj = self.set_deadlines(obj, properties["deadlines"])
|
||||||
|
|
||||||
|
obj.log.add(update_action)
|
||||||
|
|
||||||
celery_update_parcels.delay(obj.geometry.id)
|
celery_update_parcels.delay(obj.geometry.id)
|
||||||
|
|
||||||
|
@ -9,16 +9,20 @@ from django.db import transaction
|
|||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, \
|
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, \
|
||||||
ResponsibilityAPISerializerV1Mixin, LegalAPISerializerV1Mixin
|
ResponsibilityAPISerializerV1Mixin, LegalAPISerializerV1Mixin, DeductableAPISerializerV1Mixin
|
||||||
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID, \
|
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID, \
|
||||||
CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID
|
CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID
|
||||||
|
from compensation.models import Payment
|
||||||
from intervention.models import Intervention, Responsibility, Legal
|
from intervention.models import Intervention, Responsibility, Legal
|
||||||
from konova.models import Geometry
|
from konova.models import Geometry
|
||||||
from konova.tasks import celery_update_parcels
|
from konova.tasks import celery_update_parcels
|
||||||
from user.models import UserActionLogEntry
|
from user.models import UserActionLogEntry
|
||||||
|
|
||||||
|
|
||||||
class InterventionAPISerializerV1(AbstractModelAPISerializerV1, ResponsibilityAPISerializerV1Mixin, LegalAPISerializerV1Mixin):
|
class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
|
||||||
|
ResponsibilityAPISerializerV1Mixin,
|
||||||
|
LegalAPISerializerV1Mixin,
|
||||||
|
DeductableAPISerializerV1Mixin):
|
||||||
model = Intervention
|
model = Intervention
|
||||||
|
|
||||||
def compensations_to_json(self, qs: QuerySet):
|
def compensations_to_json(self, qs: QuerySet):
|
||||||
@ -119,6 +123,58 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, ResponsibilityAP
|
|||||||
obj.responsible.handler = responsibility_data["handler"]
|
obj.responsible.handler = responsibility_data["handler"]
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
def set_payments(self, obj, payment_data):
|
||||||
|
""" Sets the linked Payment data according to the given payment_data
|
||||||
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Compensation): The Compensation object
|
||||||
|
payment_data (dict): The posted payment_data
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
obj (intervention)
|
||||||
|
"""
|
||||||
|
payments = []
|
||||||
|
for entry in payment_data:
|
||||||
|
due_on = entry["due_on"]
|
||||||
|
amount = float(entry["amount"])
|
||||||
|
comment = entry["comment"]
|
||||||
|
|
||||||
|
# Check on validity
|
||||||
|
if amount <= 0:
|
||||||
|
raise ValueError("Payment amount must be > 0")
|
||||||
|
|
||||||
|
no_due_on = due_on is None or len(due_on) == 0
|
||||||
|
no_comment = comment is None or len(comment) == 0
|
||||||
|
|
||||||
|
if no_due_on and no_comment:
|
||||||
|
raise ValueError("If no due_on can be provided, you need to explain why using the comment")
|
||||||
|
|
||||||
|
# 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_payment = obj.payments.filter(
|
||||||
|
amount=amount,
|
||||||
|
due_on=due_on,
|
||||||
|
comment=comment,
|
||||||
|
).exclude(
|
||||||
|
id__in=payments
|
||||||
|
).first()
|
||||||
|
if pre_existing_payment is not None:
|
||||||
|
payments.append(pre_existing_payment.id)
|
||||||
|
else:
|
||||||
|
# Create and add id to list
|
||||||
|
new_payment = Payment.objects.create(
|
||||||
|
amount=amount,
|
||||||
|
due_on=due_on,
|
||||||
|
comment=comment,
|
||||||
|
)
|
||||||
|
payments.append(new_payment.id)
|
||||||
|
payments = Payment.objects.filter(
|
||||||
|
id__in=payments
|
||||||
|
)
|
||||||
|
obj.payments.set(payments)
|
||||||
|
return obj
|
||||||
|
|
||||||
def create_model_from_json(self, json_model, user):
|
def create_model_from_json(self, json_model, user):
|
||||||
""" Creates a new entry for the model based on the contents of json_model
|
""" Creates a new entry for the model based on the contents of json_model
|
||||||
|
|
||||||
@ -163,21 +219,25 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, ResponsibilityAP
|
|||||||
created_id (str): The id of the newly created Intervention entry
|
created_id (str): The id of the newly created Intervention entry
|
||||||
"""
|
"""
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
|
update_action = UserActionLogEntry.get_edited_action(user, "API update")
|
||||||
obj = self.get_obj_from_db(id, user)
|
obj = self.get_obj_from_db(id, user)
|
||||||
|
|
||||||
# Fill in data to objects
|
# Fill in data to objects
|
||||||
properties = json_model["properties"]
|
properties = json_model["properties"]
|
||||||
obj.title = properties["title"]
|
obj.title = properties["title"]
|
||||||
|
obj.modified = update_action
|
||||||
self.set_responsibility(obj, properties["responsible"])
|
self.set_responsibility(obj, properties["responsible"])
|
||||||
self.set_legal(obj, properties["legal"])
|
self.set_legal(obj, properties["legal"])
|
||||||
|
self.set_payments(obj, properties["payments"])
|
||||||
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
obj.geometry.geom = self.create_geometry_from_json(json_model)
|
||||||
|
obj.geometry.modified = update_action
|
||||||
|
|
||||||
obj.responsible.save()
|
obj.responsible.save()
|
||||||
obj.geometry.save()
|
obj.geometry.save()
|
||||||
obj.legal.save()
|
obj.legal.save()
|
||||||
obj.save()
|
obj.save()
|
||||||
|
|
||||||
obj.users.add(user)
|
obj.log.add(update_action)
|
||||||
|
|
||||||
celery_update_parcels.delay(obj.geometry.id)
|
celery_update_parcels.delay(obj.geometry.id)
|
||||||
|
|
||||||
|
@ -189,32 +189,36 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
Returns:
|
Returns:
|
||||||
obj (Compensation)
|
obj (Compensation)
|
||||||
"""
|
"""
|
||||||
found_deadlines = []
|
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"]
|
||||||
|
|
||||||
|
# Check on validity
|
||||||
if deadline_type not in DeadlineType:
|
if deadline_type not in DeadlineType:
|
||||||
raise ValueError(f"Invalid deadline type. Choices are {DeadlineType.values}")
|
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,
|
type=deadline_type,
|
||||||
date=date,
|
date=date,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
).exclude(
|
).exclude(
|
||||||
id__in=found_deadlines
|
id__in=deadlines
|
||||||
)
|
).first()
|
||||||
if pre_existing_deadlines.count() > 0:
|
if pre_existing_deadline is not None:
|
||||||
found_deadlines += pre_existing_deadlines.values_list("id", flat=True)
|
deadlines.append(pre_existing_deadline.id)
|
||||||
else:
|
else:
|
||||||
# Create!
|
# Create and add id to list
|
||||||
new_deadline = Deadline.objects.create(
|
new_deadline = Deadline.objects.create(
|
||||||
type=deadline_type,
|
type=deadline_type,
|
||||||
date=date,
|
date=date,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
)
|
)
|
||||||
obj.deadlines.add(new_deadline)
|
deadlines.append(new_deadline.id)
|
||||||
|
obj.deadlines.set(deadlines)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def set_compensation_states(self, obj, states_data, states_manager):
|
def set_compensation_states(self, obj, states_data, states_manager):
|
||||||
@ -229,27 +233,34 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
Returns:
|
Returns:
|
||||||
obj (Compensation)
|
obj (Compensation)
|
||||||
"""
|
"""
|
||||||
found_states = []
|
states = []
|
||||||
for entry in states_data:
|
for entry in states_data:
|
||||||
biotope_type = entry["biotope"]
|
biotope_type = entry["biotope"]
|
||||||
surface = float(entry["surface"])
|
surface = float(entry["surface"])
|
||||||
|
|
||||||
|
# Check on validity
|
||||||
if surface <= 0:
|
if surface <= 0:
|
||||||
raise ValueError("State surfaces must be > 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,
|
biotope_type__atom_id=biotope_type,
|
||||||
surface=surface,
|
surface=surface,
|
||||||
).exclude(
|
).exclude(
|
||||||
id__in=found_states
|
id__in=states
|
||||||
)
|
).first()
|
||||||
if pre_existing_states.count() > 0:
|
if pre_existing_state is not None:
|
||||||
found_states += pre_existing_states.values_list("id", flat=True)
|
states.append(pre_existing_state.id)
|
||||||
else:
|
else:
|
||||||
# Create!
|
# Create and add id to list
|
||||||
new_state = CompensationState.objects.create(
|
new_state = CompensationState.objects.create(
|
||||||
biotope_type=self.konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID),
|
biotope_type=self.konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID),
|
||||||
surface=surface
|
surface=surface
|
||||||
)
|
)
|
||||||
states_manager.add(new_state)
|
states.append(new_state.id)
|
||||||
|
|
||||||
|
states_manager.set(states)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def set_compensation_actions(self, obj, actions_data):
|
def set_compensation_actions(self, obj, actions_data):
|
||||||
@ -263,36 +274,41 @@ class AbstractCompensationAPISerializerV1Mixin:
|
|||||||
Returns:
|
Returns:
|
||||||
obj (Compensation)
|
obj (Compensation)
|
||||||
"""
|
"""
|
||||||
found_actions = []
|
actions = []
|
||||||
for entry in actions_data:
|
for entry in actions_data:
|
||||||
action = entry["action"]
|
action = entry["action"]
|
||||||
amount = float(entry["amount"])
|
amount = float(entry["amount"])
|
||||||
unit = entry["unit"]
|
unit = entry["unit"]
|
||||||
comment = entry["comment"]
|
comment = entry["comment"]
|
||||||
|
|
||||||
|
# Check on validity
|
||||||
if amount <= 0:
|
if amount <= 0:
|
||||||
raise ValueError("Action amount must be > 0")
|
raise ValueError("Action amount must be > 0")
|
||||||
if unit not in UnitChoices:
|
if unit not in UnitChoices:
|
||||||
raise ValueError(f"Invalid unit. Choices are {UnitChoices.values}")
|
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,
|
action_type__atom_id=action,
|
||||||
amount=amount,
|
amount=amount,
|
||||||
unit=unit,
|
unit=unit,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
).exclude(
|
).exclude(
|
||||||
id__in=found_actions
|
id__in=actions
|
||||||
)
|
).first()
|
||||||
if pre_existing_actions.count() > 0:
|
if pre_existing_action is not None:
|
||||||
found_actions += pre_existing_actions.values_list("id", flat=True)
|
actions.append(pre_existing_action.id)
|
||||||
else:
|
else:
|
||||||
# Create!
|
# Create and add id to list
|
||||||
new_action = CompensationAction.objects.create(
|
new_action = CompensationAction.objects.create(
|
||||||
action_type=self.konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
|
action_type=self.konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
|
||||||
amount=amount,
|
amount=amount,
|
||||||
unit=unit,
|
unit=unit,
|
||||||
comment=comment,
|
comment=comment,
|
||||||
)
|
)
|
||||||
obj.actions.add(new_action)
|
actions.append(new_action.id)
|
||||||
|
obj.actions.set(actions)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def compensation_state_to_json(self, qs: QuerySet):
|
def compensation_state_to_json(self, qs: QuerySet):
|
||||||
|
Loading…
Reference in New Issue
Block a user