#7 New Form
* fixes bug where geometries have been created each time a SimpleGeomForm has been saved
This commit is contained in:
parent
a50deaaf18
commit
feeddda90d
@ -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
|
@ -275,10 +275,28 @@ class SimpleGeomForm(BaseForm):
|
||||
self.fields["geom"].disabled = True
|
||||
|
||||
def save(self, action: UserActionLogEntry):
|
||||
""" 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
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user