diff --git a/api/utils/serializer/v1/ema.py b/api/utils/serializer/v1/ema.py index a2e0bc1f..8b1f358a 100644 --- a/api/utils/serializer/v1/ema.py +++ b/api/utils/serializer/v1/ema.py @@ -5,10 +5,15 @@ Contact: michel.peltriaux@sgdnord.rlp.de Created on: 24.01.22 """ +from django.db import transaction + from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, \ ResponsibilityAPISerializerV1Mixin from ema.models import Ema from intervention.models import Responsibility +from konova.models import Geometry +from konova.tasks import celery_update_parcels +from user.models import UserActionLogEntry class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISerializerV1Mixin, ResponsibilityAPISerializerV1Mixin): @@ -27,3 +32,103 @@ class EmaAPISerializerV1(AbstractModelAPISerializerV1, AbstractCompensationAPISe self.properties_data["after_states"] = self.compensation_state_to_json(entry.after_states.all()) self.properties_data["actions"] = self.compensation_actions_to_json(entry.actions.all()) self.properties_data["deadlines"] = self.deadlines_to_json(entry.deadlines.all()) + + def initialize_objects(self, json_model, user): + """ Initializes all needed objects from the json_model data + + Does not persist data to the DB! + + Args: + json_model (dict): The json data + user (User): The API user + + Returns: + obj (Compensation) + """ + create_action = UserActionLogEntry.get_created_action(user, comment="API Import") + # Create geometry + json_geom = self.create_geometry_from_json(json_model) + geometry = Geometry() + geometry.geom = json_geom + geometry.created = create_action + + # Create linked objects + obj = Ema() + obj.responsible = Responsibility() + created = create_action + obj.created = created + obj.geometry = geometry + 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 + + Args: + json_model (dict): The json containing data + user (User): The API user + + Returns: + created_id (str): The id of the newly created Ema entry + """ + with transaction.atomic(): + obj = self.initialize_objects(json_model, user) + + # Fill in data to objects + properties = json_model["properties"] + obj.identifier = obj.generate_new_identifier() + obj.title = properties["title"] + obj = self.set_responsibility(obj, properties["responsible"]) + + obj.geometry.save() + obj.responsible.save() + obj.save() + + 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(obj.created) + obj.users.add(user) + + celery_update_parcels.delay(obj.geometry.id) + + return obj.id + + def update_model_from_json(self, id, json_model, user): + """ Updates an entry for the model based on the contents of json_model + + Args: + id (str): The object's id + json_model (dict): The json containing data + user (User): The API user + + Returns: + created_id (str): The id of the newly created Ema 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 + obj.geometry.geom = self.create_geometry_from_json(json_model) + obj.geometry.modified = update_action + obj = self.set_responsibility(obj, properties["responsible"]) + + obj.geometry.save() + obj.responsible.save() + obj.save() + + 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) + + return obj.id diff --git a/api/utils/serializer/v1/intervention.py b/api/utils/serializer/v1/intervention.py index b77d7e33..cca95062 100644 --- a/api/utils/serializer/v1/intervention.py +++ b/api/utils/serializer/v1/intervention.py @@ -10,8 +10,6 @@ from django.db.models import QuerySet from api.utils.serializer.v1.serializer import AbstractModelAPISerializerV1, \ 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 @@ -80,53 +78,6 @@ class InterventionAPISerializerV1(AbstractModelAPISerializerV1, obj.responsible = resp return obj - def set_legal(self, obj, legal_data): - """ Sets the legal data contents to the provided legal_data dict - - Args: - obj (Intervention): The intervention object - legal_data (dict): The new data - - Returns: - obj - """ - if legal_data is None: - return obj - obj.legal.registration_date = legal_data["registration_date"] - obj.legal.binding_date = legal_data["binding_date"] - obj.legal.process_type = self.konova_code_from_json( - legal_data["process_type"], - CODELIST_PROCESS_TYPE_ID, - ) - laws = [self.konova_code_from_json(law, CODELIST_LAW_ID) for law in legal_data["laws"]] - obj.legal.laws.set(laws) - return obj - - 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.registration_office = self.konova_code_from_json( - responsibility_data["registration_office"], - CODELIST_REGISTRATION_OFFICE_ID - ) - obj.responsible.registration_file_number = responsibility_data["registration_file_number"] - 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_payments(self, obj, payment_data): """ Sets the linked Payment data according to the given payment_data diff --git a/api/utils/serializer/v1/serializer.py b/api/utils/serializer/v1/serializer.py index e97b1ee9..ace721ef 100644 --- a/api/utils/serializer/v1/serializer.py +++ b/api/utils/serializer/v1/serializer.py @@ -12,7 +12,8 @@ from django.db.models import QuerySet from api.utils.serializer.serializer import AbstractModelAPISerializer from codelist.models import KonovaCode -from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID +from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID, CODELIST_PROCESS_TYPE_ID, \ + CODELIST_LAW_ID, CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID from compensation.models import CompensationAction, UnitChoices, CompensationState from intervention.models import Responsibility, Legal from konova.models import Deadline, DeadlineType @@ -154,6 +155,31 @@ class ResponsibilityAPISerializerV1Mixin: "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.registration_office = self.konova_code_from_json( + responsibility_data.get("registration_office", None), + CODELIST_REGISTRATION_OFFICE_ID + ) + obj.responsible.registration_file_number = responsibility_data.get("registration_file_number", None) + obj.responsible.conservation_office = self.konova_code_from_json( + responsibility_data.get("conservation_office", None), + CODELIST_CONSERVATION_OFFICE_ID, + ) + obj.responsible.conservation_file_number = responsibility_data.get("conservation_file_number", None) + obj.responsible.handler = responsibility_data.get("handler", None) + return obj + class LegalAPISerializerV1Mixin: class Meta: @@ -175,6 +201,28 @@ class LegalAPISerializerV1Mixin: "laws": [self.konova_code_to_json(law) for law in legal.laws.all()], } + def set_legal(self, obj, legal_data): + """ Sets the legal data contents to the provided legal_data dict + + Args: + obj (Intervention): The intervention object + legal_data (dict): The new data + + Returns: + obj + """ + if legal_data is None: + return obj + obj.legal.registration_date = legal_data.get("registration_date", None) + obj.legal.binding_date = legal_data.get("binding_date", None) + obj.legal.process_type = self.konova_code_from_json( + legal_data.get("process_type", None), + CODELIST_PROCESS_TYPE_ID, + ) + laws = [self.konova_code_from_json(law, CODELIST_LAW_ID) for law in legal_data.get("laws", [])] + obj.legal.laws.set(laws) + return obj + class AbstractCompensationAPISerializerV1Mixin: class Meta: