JS Tree enhancement

* extends compensation state forms to match the new logic
* adds minor changes for tests
This commit is contained in:
mpeltriaux 2022-05-11 10:16:34 +02:00
parent 5fe27e02ec
commit eb763a94fb
2 changed files with 20 additions and 4 deletions

View File

@ -157,7 +157,7 @@ class NewStateModalForm(BaseModalForm):
What has been on this area before changes/compensations have been applied and what will be the result ('after')? What has been on this area before changes/compensations have been applied and what will be the result ('after')?
""" """
biotope_type = forms.MultipleChoiceField( biotope_type = forms.ChoiceField(
label=_("Biotope Type"), label=_("Biotope Type"),
label_suffix="", label_suffix="",
required=True, required=True,
@ -200,6 +200,16 @@ class NewStateModalForm(BaseModalForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.form_title = _("New state") self.form_title = _("New state")
self.form_caption = _("Insert data for the new state") self.form_caption = _("Insert data for the new state")
choices = KonovaCode.objects.filter(
code_lists__in=[CODELIST_BIOTOPES_ID],
is_archived=False,
is_leaf=True,
).values_list("id", flat=True)
choices = [
(choice, choice)
for choice in choices
]
self.fields["biotope_type"].choices = choices
def save(self, is_before_state: bool = False): def save(self, is_before_state: bool = False):
state = self.instance.add_state(self, is_before_state) state = self.instance.add_state(self, is_before_state)
@ -262,8 +272,9 @@ class EditCompensationStateModalForm(NewStateModalForm):
self.state = kwargs.pop("state", None) self.state = kwargs.pop("state", None)
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.form_title = _("Edit state") self.form_title = _("Edit state")
biotope_type_id = self.state.biotope_type.id if self.state.biotope_type else None
form_data = { form_data = {
"biotope_type": self.state.biotope_type.id, "biotope_type": biotope_type_id,
"biotope_extra": self.state.biotope_type_details.all(), "biotope_extra": self.state.biotope_type_details.all(),
"surface": self.state.surface, "surface": self.state.surface,
} }
@ -271,7 +282,8 @@ class EditCompensationStateModalForm(NewStateModalForm):
def save(self, is_before_state: bool = False): def save(self, is_before_state: bool = False):
state = self.state state = self.state
state.biotope_type = self.cleaned_data.get("biotope_type", None) biotope_type_id = self.cleaned_data.get("biotope_type", None)
state.biotope_type = KonovaCode.objects.get(id=biotope_type_id)
state.biotope_type_details.set(self.cleaned_data.get("biotope_extra", [])) state.biotope_type_details.set(self.cleaned_data.get("biotope_extra", []))
state.surface = self.cleaned_data.get("surface", None) state.surface = self.cleaned_data.get("surface", None)
state.save() state.save()

View File

@ -8,6 +8,8 @@ Created on: 16.11.21
import shutil import shutil
from django.contrib import messages from django.contrib import messages
from codelist.models import KonovaCode
from user.models import User, Team from user.models import User, Team
from django.db import models, transaction from django.db import models, transaction
from django.db.models import QuerySet, Sum from django.db.models import QuerySet, Sum
@ -142,8 +144,10 @@ class AbstractCompensation(BaseObject, GeoReferencedMixin):
""" """
form_data = form.cleaned_data form_data = form.cleaned_data
with transaction.atomic(): with transaction.atomic():
biotope_type_id = form_data["biotope_type"]
code = KonovaCode.objects.get(id=biotope_type_id)
state = CompensationState.objects.create( state = CompensationState.objects.create(
biotope_type=form_data["biotope_type"], biotope_type=code,
surface=form_data["surface"], surface=form_data["surface"],
) )
state_additional_types = form_data["biotope_extra"] state_additional_types = form_data["biotope_extra"]