From feeddda90d9e2af541907199ed7da3b54f53e926 Mon Sep 17 00:00:00 2001 From: mipel Date: Mon, 4 Oct 2021 16:06:20 +0200 Subject: [PATCH] #7 New Form * fixes bug where geometries have been created each time a SimpleGeomForm has been saved --- compensation/forms/forms.py | 54 +++++++++++++++++++++++++++++++++++++ konova/forms.py | 26 +++++++++++++++--- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/compensation/forms/forms.py b/compensation/forms/forms.py index f004e47e..292dce91 100644 --- a/compensation/forms/forms.py +++ b/compensation/forms/forms.py @@ -22,6 +22,11 @@ from user.models import UserActionLogEntry, UserAction class NewCompensationForm(BaseForm): + """ Form for creating new compensations. + + Can be initialized with an intervention id for preselecting the related intervention. + + """ identifier = forms.CharField( label=_("Identifier"), label_suffix="", @@ -145,3 +150,52 @@ class NewCompensationForm(BaseForm): return comp +class EditCompensationForm(NewCompensationForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.form_title = _("Edit compensation") + self.action_url = reverse("compensation:edit", args=(self.instance.id,)) + self.cancel_redirect = reverse("compensation:open", args=(self.instance.id,)) + + # Initialize form data + form_data = { + "identifier": self.instance.identifier, + "title": self.instance.title, + "intervention": self.instance.intervention, + "fundings": self.instance.fundings.all(), + "comment": self.instance.comment, + } + disabled_fields = [] + self.load_initial_data( + form_data, + disabled_fields + ) + + def save(self, user: User, geom_form: SimpleGeomForm): + with transaction.atomic(): + # Fetch data from cleaned POST values + identifier = self.cleaned_data.get("identifier", None) + title = self.cleaned_data.get("title", None) + fundings = self.cleaned_data.get("fundings", None) + intervention = self.cleaned_data.get("intervention", None) + comment = self.cleaned_data.get("comment", None) + + # Create log entry + action = UserActionLogEntry.objects.create( + user=user, + action=UserAction.EDITED, + ) + + # Process the geometry form + geometry = geom_form.save(action) + + # Finally create main object + self.instance.identifier = identifier + self.instance.title = title + self.instance.intervention = intervention + self.instance.geometry = geometry + self.instance.comment = comment + self.instance.fundings.set(fundings) + + self.instance.log.add(action) + return self.instance \ No newline at end of file diff --git a/konova/forms.py b/konova/forms.py index 0e6ad47e..d0dedef4 100644 --- a/konova/forms.py +++ b/konova/forms.py @@ -275,10 +275,28 @@ class SimpleGeomForm(BaseForm): self.fields["geom"].disabled = True def save(self, action: UserActionLogEntry): - geometry = Geometry.objects.create( - geom=self.cleaned_data.get("geom", MultiPolygon(srid=DEFAULT_SRID)), - created=action, - ) + """ Saves the form's geometry + + Creates a new geometry entry if none is set, yet + + Args: + action (): + + Returns: + + """ + if self.instance.geometry is None: + geometry = Geometry.objects.create( + geom=self.cleaned_data.get("geom", MultiPolygon(srid=DEFAULT_SRID)), + created=action, + ) + self.instance.geometry = geometry + self.instance.save() + else: + geometry = self.instance.geometry + geometry.geom = self.cleaned_data.get("geom", MultiPolygon(srid=DEFAULT_SRID)) + geometry.modified = action + geometry.save() return geometry