* adds creation tests with minimum data for intervention, compensation, ema and ecoaccount
* fixes bug where empty geometry would not be created properly using the API
* reworks key fetching from POST data, so inproperly stated keys will lead to an error for the API user, instead of silently working and use default data
* adds some logical checks for deductable_surface of eco account creation using api
* fixes bug that would have occured on creating compensations via api
This commit is contained in:
2022-01-27 17:09:38 +01:00
parent ea29aa7d6b
commit 80896731a8
14 changed files with 280 additions and 46 deletions

View File

@@ -9,6 +9,7 @@ from django.db import transaction
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, \
LegalAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin, DeductableAPISerializerV1Mixin
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID
from compensation.models import EcoAccount
from intervention.models import Legal, Responsibility
from konova.models import Geometry
@@ -46,6 +47,26 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
"handler": responsible.handler,
}
def set_responsibility(self, obj, responsibility_data: dict):
""" Sets the responsible data contents to the provided responsibility_data dict
Args:
obj (Intervention): The intervention object
responsibility_data (dict): The new data
Returns:
obj
"""
if responsibility_data is None:
return obj
obj.responsible.conservation_office = self.konova_code_from_json(
responsibility_data["conservation_office"],
CODELIST_CONSERVATION_OFFICE_ID,
)
obj.responsible.conservation_file_number = responsibility_data["conservation_file_number"]
obj.responsible.handler = responsibility_data["handler"]
return obj
def set_legal(self, obj, legal_data):
obj.legal.registration_date = legal_data.get("agreement_date", None)
return obj
@@ -95,7 +116,14 @@ class EcoAccountAPISerializerV1(AbstractModelAPISerializerV1,
properties = json_model["properties"]
obj.identifier = obj.generate_new_identifier()
obj.title = properties["title"]
obj.deductable_surface = float(properties["deductable_surface"])
try:
obj.deductable_surface = float(properties["deductable_surface"])
except TypeError:
raise ValueError("Deductable surface (m²) must be a number >= 0")
if obj.deductable_surface < 0:
raise ValueError("Deductable surface (m²) must be greater or equal 0")
obj = self.set_responsibility(obj, properties["responsible"])
obj = self.set_legal(obj, properties["legal"])