#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
This commit is contained in:
mpeltriaux 2022-01-31 14:01:02 +01:00
parent 2b33f0e23f
commit 8bc13e8c00
9 changed files with 35 additions and 20 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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={

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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>