#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 73cf3fc503
commit f6304d0b33
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 api.utils.serializer.serializer import AbstractModelAPISerializer
from codelist.models import KonovaCode from codelist.models import KonovaCode
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID, CODELIST_PROCESS_TYPE_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 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 compensation.models import CompensationAction, UnitChoices, CompensationState
from intervention.models import Responsibility, Legal from intervention.models import Responsibility, Legal
from konova.models import Deadline, DeadlineType from konova.models import Deadline, DeadlineType
@ -323,6 +324,9 @@ class AbstractCompensationAPISerializerV1Mixin:
states = [] states = []
for entry in states_data: for entry in states_data:
biotope_type = entry["biotope"] 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"]) surface = float(entry["surface"])
# Check on validity # 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 # 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 # 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, biotope_type__atom_id=biotope_type,
surface=surface, surface=surface,
).exclude( ).exclude(
id__in=states id__in=states
).first() ).first()
if pre_existing_state is not None: if state is not None:
states.append(pre_existing_state.id) states.append(state.id)
else: else:
# Create and add id to list # 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), biotope_type=self._konova_code_from_json(biotope_type, CODELIST_BIOTOPES_ID),
surface=surface surface=surface
) )
states.append(new_state.id) states.append(state.id)
state.biotope_type_details.set(biotope_details)
states_manager.set(states) states_manager.set(states)
return obj return obj
@ -364,6 +368,9 @@ class AbstractCompensationAPISerializerV1Mixin:
actions = [] actions = []
for entry in actions_data: for entry in actions_data:
action = entry["action"] 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"]) amount = float(entry["amount"])
unit = entry["unit"] unit = entry["unit"]
comment = entry["comment"] 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 # 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 # 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, action_type__atom_id=action,
amount=amount, amount=amount,
unit=unit, unit=unit,
@ -384,17 +391,19 @@ class AbstractCompensationAPISerializerV1Mixin:
).exclude( ).exclude(
id__in=actions id__in=actions
).first() ).first()
if pre_existing_action is not None: if action_entry is not None:
actions.append(pre_existing_action.id) actions.append(action_entry.id)
else: else:
# Create and add id to list # 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), action_type=self._konova_code_from_json(action, CODELIST_COMPENSATION_ACTION_ID),
amount=amount, amount=amount,
unit=unit, unit=unit,
comment=comment, comment=comment,
) )
actions.append(new_action.id) actions.append(action_entry.id)
action_entry.action_type_details.set(action_details)
obj.actions.set(actions) obj.actions.set(actions)
return obj return obj
@ -410,6 +419,9 @@ class AbstractCompensationAPISerializerV1Mixin:
return [ return [
{ {
"biotope": self._konova_code_to_json(entry.biotope_type), "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, "surface": entry.surface,
} }
for entry in qs for entry in qs
@ -427,6 +439,9 @@ class AbstractCompensationAPISerializerV1Mixin:
return [ return [
{ {
"action": self._konova_code_to_json(entry.action_type), "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, "amount": entry.amount,
"unit": entry.unit, "unit": entry.unit,
"comment": entry.comment, "comment": entry.comment,

View File

@ -117,7 +117,7 @@ class AbstractCompensation(BaseObject, GeoReferencedMixin):
surface=form_data["surface"], surface=form_data["surface"],
) )
state_additional_types = form_data["biotope_extra"] 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: if is_before_state:
self.before_states.add(state) self.before_states.add(state)
else: else:

View File

@ -29,7 +29,7 @@ class CompensationState(UuidModel):
}, },
related_name='+', related_name='+',
) )
biotope_extra_types = models.ManyToManyField( biotope_type_details = models.ManyToManyField(
KonovaCode, KonovaCode,
blank=True, blank=True,
limit_choices_to={ limit_choices_to={

View File

@ -52,7 +52,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>

View File

@ -52,7 +52,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>

View File

@ -52,7 +52,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>

View File

@ -52,7 +52,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>

View File

@ -50,7 +50,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>

View File

@ -50,7 +50,7 @@
{{ state.biotope_type }} {{ state.biotope_type }}
</td> </td>
<td class="align-middle"> <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 }}"> <div class="mb-2" title="{{ biotope_extra }}">
{{ biotope_extra.long_name }} {{ biotope_extra.long_name }}
</div> </div>