konova/api/utils/serializer/v1/intervention.py
mpeltriaux d0f3fb9f61 #31 API POST Intervention
* adds support for proper POST of intervention
* makes /<id> optional (required for Post)
2022-01-24 12:17:17 +01:00

89 lines
3.6 KiB
Python

"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 24.01.22
"""
from django.db import transaction
from django.db.models import QuerySet
from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1
from codelist.settings import CODELIST_CONSERVATION_OFFICE_ID, CODELIST_REGISTRATION_OFFICE_ID, \
CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID
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):
model = Intervention
def compensations_to_json(self, qs: QuerySet):
return list(
qs.values(
"id", "identifier", "title"
)
)
def extend_properties_data(self, entry):
self.properties_data["responsible"] = self.responsible_to_json(entry.responsible)
self.properties_data["legal"] = self.legal_to_json(entry.legal)
self.properties_data["compensations"] = self.compensations_to_json(entry.compensations.all())
self.properties_data["payments"] = self.payments_to_json(entry.payments.all())
self.properties_data["deductions"] = self.deductions_to_json(entry.deductions.all())
def create_model_from_json(self, json_model, user):
with transaction.atomic():
# Create geometry
json_geom = self.create_geometry_from_json(json_model)
geometry = Geometry()
geometry.geom = json_geom
# Create linked objects
obj = Intervention()
resp = Responsibility()
legal = Legal()
created = UserActionLogEntry.get_created_action(user, comment="API Import")
obj.legal = legal
obj.created = created
obj.geometry = geometry
obj.responsible = resp
# Fill in data to objects
properties = json_model["properties"]
obj.identifier = obj.generate_new_identifier()
obj.title = properties["title"]
obj.responsible.registration_office = self.konova_code_from_json(
properties["responsible"]["registration_office"],
CODELIST_REGISTRATION_OFFICE_ID
)
obj.responsible.registration_file_number = properties["responsible"]["registration_file_number"]
obj.responsible.conservation_office = self.konova_code_from_json(
properties["responsible"]["conservation_office"],
CODELIST_CONSERVATION_OFFICE_ID,
)
obj.responsible.conservation_file_number = properties["responsible"]["conservation_file_number"]
obj.responsible.handler = properties["responsible"]["handler"]
obj.legal.registration_date = properties["legal"]["registration_date"]
obj.legal.binding_date = properties["legal"]["binding_date"]
obj.legal.process_type = self.konova_code_from_json(
properties["legal"]["process_type"],
CODELIST_PROCESS_TYPE_ID,
)
laws = [self.konova_code_from_json(law, CODELIST_LAW_ID) for law in properties["legal"]["laws"]]
obj.legal.laws.set(laws)
obj.responsible.save()
obj.geometry.save()
obj.legal.save()
obj.save()
obj.users.add(user)
celery_update_parcels.delay(geometry.id)
return obj.id