From f6304d0b33f98a7c81a527dcccf41b99072a4172 Mon Sep 17 00:00:00 2001
From: mpeltriaux <michel.peltriaux@sgdnord.rlp.de>
Date: Mon, 31 Jan 2022 14:01:02 +0100
Subject: [PATCH] #88 Action/Biotope API extension

* extends the API to support serializing and deserializing of action_details and biotope_details
* renames biotope_extra_types into biotope_type_details on CompensationState model for convenience reasons and to match CompensationAction's action_type_details
---
 api/utils/serializer/v1/serializer.py         | 39 +++++++++++++------
 compensation/models/compensation.py           |  2 +-
 compensation/models/state.py                  |  2 +-
 .../compensation/includes/states-after.html   |  2 +-
 .../compensation/includes/states-before.html  |  2 +-
 .../eco_account/includes/states-after.html    |  2 +-
 .../eco_account/includes/states-before.html   |  2 +-
 .../ema/detail/includes/states-after.html     |  2 +-
 .../ema/detail/includes/states-before.html    |  2 +-
 9 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/api/utils/serializer/v1/serializer.py b/api/utils/serializer/v1/serializer.py
index 66421708..f56db81b 100644
--- a/api/utils/serializer/v1/serializer.py
+++ b/api/utils/serializer/v1/serializer.py
@@ -14,7 +14,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, CODELIST_PROCESS_TYPE_ID, \
-    CODELIST_LAW_ID, CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID
+    CODELIST_LAW_ID, CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID, \
+    CODELIST_COMPENSATION_ACTION_DETAIL_ID, CODELIST_BIOTOPES_EXTRA_CODES_ID
 from compensation.models import CompensationAction, UnitChoices, CompensationState
 from intervention.models import Responsibility, Legal
 from konova.models import Deadline, DeadlineType
@@ -323,6 +324,9 @@ class AbstractCompensationAPISerializerV1Mixin:
         states = []
         for entry in states_data:
             biotope_type = entry["biotope"]
+            biotope_details = [
+                self._konova_code_from_json(e, CODELIST_BIOTOPES_EXTRA_CODES_ID) for e in entry["biotope_details"]
+            ]
             surface = float(entry["surface"])
 
             # Check on validity
@@ -331,22 +335,22 @@ class AbstractCompensationAPISerializerV1Mixin:
 
             # 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(
+            state = states_manager.filter(
                 biotope_type__atom_id=biotope_type,
                 surface=surface,
             ).exclude(
                 id__in=states
             ).first()
-            if pre_existing_state is not None:
-                states.append(pre_existing_state.id)
+            if state is not None:
+                states.append(state.id)
             else:
                 # Create and add id to list
-                new_state = CompensationState.objects.create(
+                state = CompensationState.objects.create(
                     biotope_type=self._konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID),
                     surface=surface
                 )
-                states.append(new_state.id)
-
+                states.append(state.id)
+            state.biotope_type_details.set(biotope_details)
         states_manager.set(states)
         return obj
 
@@ -364,6 +368,9 @@ class AbstractCompensationAPISerializerV1Mixin:
         actions = []
         for entry in actions_data:
             action = entry["action"]
+            action_details = [
+                self._konova_code_from_json(e, CODELIST_COMPENSATION_ACTION_DETAIL_ID) for e in entry["action_details"]
+            ]
             amount = float(entry["amount"])
             unit = entry["unit"]
             comment = entry["comment"]
@@ -376,7 +383,7 @@ class AbstractCompensationAPISerializerV1Mixin:
 
             # 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_entry = obj.actions.filter(
                 action_type__atom_id=action,
                 amount=amount,
                 unit=unit,
@@ -384,17 +391,19 @@ class AbstractCompensationAPISerializerV1Mixin:
             ).exclude(
                 id__in=actions
             ).first()
-            if pre_existing_action is not None:
-                actions.append(pre_existing_action.id)
+            if action_entry is not None:
+                actions.append(action_entry.id)
             else:
                 # Create and add id to list
-                new_action = CompensationAction.objects.create(
+                action_entry = CompensationAction.objects.create(
                     action_type=self._konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
                     amount=amount,
                     unit=unit,
                     comment=comment,
                 )
-                actions.append(new_action.id)
+                actions.append(action_entry.id)
+
+            action_entry.action_type_details.set(action_details)
         obj.actions.set(actions)
         return obj
 
@@ -410,6 +419,9 @@ class AbstractCompensationAPISerializerV1Mixin:
         return [
             {
                 "biotope": self._konova_code_to_json(entry.biotope_type),
+                "biotope_details": [
+                    self._konova_code_to_json(detail) for detail in entry.biotope_type_details.all()
+                ],
                 "surface": entry.surface,
             }
             for entry in qs
@@ -427,6 +439,9 @@ class AbstractCompensationAPISerializerV1Mixin:
         return [
             {
                 "action": self._konova_code_to_json(entry.action_type),
+                "action_details": [
+                    self._konova_code_to_json(detail) for detail in entry.action_type_details.all()
+                ],
                 "amount": entry.amount,
                 "unit": entry.unit,
                 "comment": entry.comment,
diff --git a/compensation/models/compensation.py b/compensation/models/compensation.py
index 8147203f..de5a3461 100644
--- a/compensation/models/compensation.py
+++ b/compensation/models/compensation.py
@@ -117,7 +117,7 @@ class AbstractCompensation(BaseObject, GeoReferencedMixin):
                 surface=form_data["surface"],
             )
             state_additional_types = form_data["biotope_extra"]
-            state.biotope_extra_types.set(state_additional_types)
+            state.biotope_type_details.set(state_additional_types)
             if is_before_state:
                 self.before_states.add(state)
             else:
diff --git a/compensation/models/state.py b/compensation/models/state.py
index a4b8fafb..858ac3d1 100644
--- a/compensation/models/state.py
+++ b/compensation/models/state.py
@@ -29,7 +29,7 @@ class CompensationState(UuidModel):
         },
         related_name='+',
     )
-    biotope_extra_types = models.ManyToManyField(
+    biotope_type_details = models.ManyToManyField(
         KonovaCode,
         blank=True,
         limit_choices_to={
diff --git a/compensation/templates/compensation/detail/compensation/includes/states-after.html b/compensation/templates/compensation/detail/compensation/includes/states-after.html
index 3f0b4eef..408689a7 100644
--- a/compensation/templates/compensation/detail/compensation/includes/states-after.html
+++ b/compensation/templates/compensation/detail/compensation/includes/states-after.html
@@ -52,7 +52,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>
diff --git a/compensation/templates/compensation/detail/compensation/includes/states-before.html b/compensation/templates/compensation/detail/compensation/includes/states-before.html
index 168524cd..991f6c23 100644
--- a/compensation/templates/compensation/detail/compensation/includes/states-before.html
+++ b/compensation/templates/compensation/detail/compensation/includes/states-before.html
@@ -52,7 +52,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>
diff --git a/compensation/templates/compensation/detail/eco_account/includes/states-after.html b/compensation/templates/compensation/detail/eco_account/includes/states-after.html
index 2d9b32e4..70a36018 100644
--- a/compensation/templates/compensation/detail/eco_account/includes/states-after.html
+++ b/compensation/templates/compensation/detail/eco_account/includes/states-after.html
@@ -52,7 +52,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>
diff --git a/compensation/templates/compensation/detail/eco_account/includes/states-before.html b/compensation/templates/compensation/detail/eco_account/includes/states-before.html
index 45ee4167..5482674a 100644
--- a/compensation/templates/compensation/detail/eco_account/includes/states-before.html
+++ b/compensation/templates/compensation/detail/eco_account/includes/states-before.html
@@ -52,7 +52,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>
diff --git a/ema/templates/ema/detail/includes/states-after.html b/ema/templates/ema/detail/includes/states-after.html
index 0cbb5fd6..db9e75b9 100644
--- a/ema/templates/ema/detail/includes/states-after.html
+++ b/ema/templates/ema/detail/includes/states-after.html
@@ -50,7 +50,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>
diff --git a/ema/templates/ema/detail/includes/states-before.html b/ema/templates/ema/detail/includes/states-before.html
index 61dbc0cd..a4da5538 100644
--- a/ema/templates/ema/detail/includes/states-before.html
+++ b/ema/templates/ema/detail/includes/states-before.html
@@ -50,7 +50,7 @@
                     {{ state.biotope_type }}
                 </td>
                 <td class="align-middle">
-                    {% for biotope_extra in state.biotope_extra_types.all %}
+                    {% for biotope_extra in state.biotope_type_details.all %}
                     <div class="mb-2" title="{{ biotope_extra }}">
                         {{ biotope_extra.long_name }}
                     </div>