#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:
2022-01-24 15:56:02 +01:00
parent 59c5caf8ac
commit fa6603f218
3 changed files with 138 additions and 33 deletions

View File

@@ -9,16 +9,20 @@ from django.db import transaction
from django.db.models import QuerySet
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, \
CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID
from compensation.models import Payment
from intervention.models import Intervention, Responsibility, Legal
from konova.models import Geometry
from konova.tasks import celery_update_parcels
from user.models import UserActionLogEntry
class InterventionAPISerializerV1(AbstractModelAPISerializerV1, ResponsibilityAPISerializerV1Mixin, LegalAPISerializerV1Mixin):
class InterventionAPISerializerV1(AbstractModelAPISerializerV1,
ResponsibilityAPISerializerV1Mixin,
LegalAPISerializerV1Mixin,
DeductableAPISerializerV1Mixin):
model = Intervention
def compensations_to_json(self, qs: QuerySet):
@@ -119,6 +123,58 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, ResponsibilityAP
obj.responsible.handler = responsibility_data["handler"]
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):
""" 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
"""
with transaction.atomic():
update_action = UserActionLogEntry.get_edited_action(user, "API update")
obj = self.get_obj_from_db(id, user)
# Fill in data to objects
properties = json_model["properties"]
obj.title = properties["title"]
obj.modified = update_action
self.set_responsibility(obj, properties["responsible"])
self.set_legal(obj, properties["legal"])
self.set_payments(obj, properties["payments"])
obj.geometry.geom = self.create_geometry_from_json(json_model)
obj.geometry.modified = update_action
obj.responsible.save()
obj.geometry.save()
obj.legal.save()
obj.save()
obj.users.add(user)
obj.log.add(update_action)
celery_update_parcels.delay(obj.geometry.id)