#7 New Form
* adds NewEmaForm and EditEmaForm * refactors ResponsibilityData related form fields into reusable mixin CompensationResponsibleFormMixin * used in NewEcoAccountForm and NewEmaForm for easier maintaining and reducing amount of code * refactors templates /xy/new/view.html into /xy/form/view.html since the same template file is used for new and edit forms
This commit is contained in:
159
ema/forms.py
Normal file
159
ema/forms.py
Normal file
@@ -0,0 +1,159 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 06.10.21
|
||||
|
||||
"""
|
||||
from django.contrib.auth.models import User
|
||||
from django.db import transaction
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from compensation.forms.forms import AbstractCompensationForm, CompensationResponsibleFormMixin
|
||||
from ema.models import Ema
|
||||
from intervention.models import ResponsibilityData
|
||||
from konova.forms import SimpleGeomForm
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
|
||||
|
||||
class NewEmaForm(AbstractCompensationForm, CompensationResponsibleFormMixin):
|
||||
""" Form for creating new EMA objects.
|
||||
|
||||
Inherits basic form fields from AbstractCompensationForm and additional from CompensationResponsibleFormMixin.
|
||||
Second holds self.instance.response related fields
|
||||
|
||||
"""
|
||||
field_order = [
|
||||
"identifier",
|
||||
"title",
|
||||
"conservation_office",
|
||||
"conservation_file_number",
|
||||
"handler",
|
||||
"fundings",
|
||||
"comment",
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.form_title = _("New EMA")
|
||||
|
||||
self.action_url = reverse("ema:new")
|
||||
self.cancel_redirect = reverse("ema:index")
|
||||
|
||||
tmp = Ema()
|
||||
identifier = tmp.generate_new_identifier()
|
||||
self.initialize_form_field("identifier", identifier)
|
||||
self.fields["identifier"].widget.attrs["url"] = reverse_lazy("ema:new-id")
|
||||
self.fields["title"].widget.attrs["placeholder"] = _("Compensation XY; Location ABC")
|
||||
|
||||
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)
|
||||
handler = self.cleaned_data.get("handler", None)
|
||||
conservation_office = self.cleaned_data.get("conservation_office", None)
|
||||
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
||||
comment = self.cleaned_data.get("comment", None)
|
||||
|
||||
# Create log entry
|
||||
action = UserActionLogEntry.objects.create(
|
||||
user=user,
|
||||
action=UserAction.CREATED,
|
||||
)
|
||||
# Process the geometry form
|
||||
geometry = geom_form.save(action)
|
||||
|
||||
responsible = ResponsibilityData.objects.create(
|
||||
handler=handler,
|
||||
conservation_file_number=conservation_file_number,
|
||||
conservation_office=conservation_office,
|
||||
)
|
||||
|
||||
# Finally create main object
|
||||
acc = Ema.objects.create(
|
||||
identifier=identifier,
|
||||
title=title,
|
||||
responsible=responsible,
|
||||
created=action,
|
||||
geometry=geometry,
|
||||
comment=comment,
|
||||
)
|
||||
acc.fundings.set(fundings)
|
||||
|
||||
# Add the creating user to the list of shared users
|
||||
acc.users.add(user)
|
||||
|
||||
# Add the log entry to the main objects log list
|
||||
acc.log.add(action)
|
||||
return acc
|
||||
|
||||
|
||||
class EditEmaForm(NewEmaForm):
|
||||
""" Form for editing EMAs
|
||||
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.form_title = _("Edit EMA")
|
||||
|
||||
self.action_url = reverse("ema:edit", args=(self.instance.id,))
|
||||
self.cancel_redirect = reverse("ema:open", args=(self.instance.id,))
|
||||
|
||||
self.fields["identifier"].widget.attrs["url"] = reverse_lazy("ema:new-id")
|
||||
self.fields["title"].widget.attrs["placeholder"] = _("Compensation XY; Location ABC")
|
||||
|
||||
# Initialize form data
|
||||
form_data = {
|
||||
"identifier": self.instance.identifier,
|
||||
"title": self.instance.title,
|
||||
"handler": self.instance.responsible.handler,
|
||||
"conservation_office": self.instance.responsible.conservation_office,
|
||||
"conservation_file_number": self.instance.responsible.conservation_file_number,
|
||||
"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)
|
||||
handler = self.cleaned_data.get("handler", None)
|
||||
conservation_office = self.cleaned_data.get("conservation_office", None)
|
||||
conservation_file_number = self.cleaned_data.get("conservation_file_number", 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)
|
||||
|
||||
# Update responsible data
|
||||
self.instance.responsible.handler = handler
|
||||
self.instance.responsible.conservation_office = conservation_office
|
||||
self.instance.responsible.conservation_file_number = conservation_file_number
|
||||
self.instance.responsible.save()
|
||||
|
||||
# Update main oject data
|
||||
self.instance.identifier = identifier
|
||||
self.instance.title = title
|
||||
self.instance.geometry = geometry
|
||||
self.instance.comment = comment
|
||||
self.instance.modified = action
|
||||
self.instance.save()
|
||||
self.instance.fundings.set(fundings)
|
||||
|
||||
# Add the log entry to the main objects log list
|
||||
self.instance.log.add(action)
|
||||
return self.instance
|
||||
Reference in New Issue
Block a user