mpeltriaux
fdb6f2d105
* refactors large forms.py into forms/modalForms.py and forms/forms.py * refactors custom input fields into intervention/inputs.py
363 lines
13 KiB
Python
363 lines
13 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 02.12.20
|
|
|
|
"""
|
|
from dal import autocomplete
|
|
from django import forms
|
|
from django.contrib.auth.models import User
|
|
from django.db import transaction
|
|
from django.urls import reverse
|
|
from django.utils import timezone
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from codelist.models import KonovaCode
|
|
from codelist.settings import CODELIST_PROCESS_TYPE_ID, CODELIST_LAW_ID, \
|
|
CODELIST_REGISTRATION_OFFICE_ID, CODELIST_CONSERVATION_OFFICE_ID
|
|
from intervention.models import Intervention, LegalData, ResponsibilityData
|
|
from konova.forms import BaseForm, SimpleGeomForm
|
|
from user.models import UserActionLogEntry, UserAction
|
|
|
|
|
|
class NewInterventionForm(BaseForm):
|
|
identifier = forms.CharField(
|
|
label=_("Identifier"),
|
|
label_suffix="",
|
|
max_length=255,
|
|
help_text=_("Generated automatically"),
|
|
)
|
|
title = forms.CharField(
|
|
label=_("Title"),
|
|
label_suffix="",
|
|
help_text=_("An explanatory name"),
|
|
max_length=255,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"placeholder": _("Construction XY; Location ABC")
|
|
}
|
|
)
|
|
)
|
|
type = forms.ModelChoiceField(
|
|
label=_("Process type"),
|
|
label_suffix="",
|
|
help_text=_(""),
|
|
required=False,
|
|
queryset=KonovaCode.objects.filter(
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
code_lists__in=[CODELIST_PROCESS_TYPE_ID],
|
|
),
|
|
widget=autocomplete.ModelSelect2(
|
|
url="codes-process-type-autocomplete",
|
|
attrs={
|
|
}
|
|
),
|
|
)
|
|
laws = forms.ModelMultipleChoiceField(
|
|
label=_("Law"),
|
|
label_suffix="",
|
|
help_text=_("Multiple selection possible"),
|
|
required=False,
|
|
queryset=KonovaCode.objects.filter(
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
code_lists__in=[CODELIST_LAW_ID],
|
|
),
|
|
widget=autocomplete.ModelSelect2Multiple(
|
|
url="codes-law-autocomplete",
|
|
attrs={
|
|
}
|
|
),
|
|
)
|
|
registration_office = forms.ModelChoiceField(
|
|
label=_("Registration office"),
|
|
label_suffix="",
|
|
required=False,
|
|
queryset=KonovaCode.objects.filter(
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
code_lists__in=[CODELIST_REGISTRATION_OFFICE_ID],
|
|
),
|
|
widget=autocomplete.ModelSelect2(
|
|
url="codes-registration-office-autocomplete",
|
|
attrs={
|
|
}
|
|
),
|
|
)
|
|
conservation_office = forms.ModelChoiceField(
|
|
label=_("Conservation office"),
|
|
label_suffix="",
|
|
required=False,
|
|
queryset=KonovaCode.objects.filter(
|
|
is_archived=False,
|
|
is_leaf=True,
|
|
code_lists__in=[CODELIST_CONSERVATION_OFFICE_ID],
|
|
),
|
|
widget=autocomplete.ModelSelect2(
|
|
url="codes-conservation-office-autocomplete",
|
|
attrs={
|
|
}
|
|
),
|
|
)
|
|
registration_file_number = forms.CharField(
|
|
label=_("Registration office file number"),
|
|
label_suffix="",
|
|
max_length=255,
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"placeholder": _("ZB-123/ABC.456")
|
|
}
|
|
)
|
|
)
|
|
conservation_file_number = forms.CharField(
|
|
label=_("Conservation office file number"),
|
|
label_suffix="",
|
|
max_length=255,
|
|
required=False,
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"placeholder": _("ETS-123/ABC.456")
|
|
}
|
|
)
|
|
)
|
|
handler = forms.CharField(
|
|
label=_("Intervention handler"),
|
|
label_suffix="",
|
|
max_length=255,
|
|
required=False,
|
|
help_text=_("Who performs the intervention"),
|
|
widget=forms.TextInput(
|
|
attrs={
|
|
"placeholder": _("Company Mustermann")
|
|
}
|
|
)
|
|
)
|
|
registration_date = forms.DateField(
|
|
label=_("Registration date"),
|
|
label_suffix=_(""),
|
|
required=False,
|
|
widget=forms.DateInput(
|
|
attrs={
|
|
"type": "date",
|
|
},
|
|
format="%d.%m.%Y"
|
|
)
|
|
)
|
|
binding_date = forms.DateField(
|
|
label=_("Binding on"),
|
|
label_suffix=_(""),
|
|
required=False,
|
|
widget=forms.DateInput(
|
|
attrs={
|
|
"type": "date",
|
|
},
|
|
format="%d.%m.%Y"
|
|
)
|
|
)
|
|
comment = forms.CharField(
|
|
label_suffix="",
|
|
label=_("Comment"),
|
|
required=False,
|
|
help_text=_("Additional comment"),
|
|
widget=forms.Textarea(
|
|
attrs={
|
|
"rows": 5,
|
|
"class": "w-100"
|
|
}
|
|
)
|
|
)
|
|
|
|
# Define w-100 for all form fields
|
|
full_width_fields = True
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.form_title = _("New intervention")
|
|
self.action_url = reverse("intervention:new")
|
|
self.cancel_redirect = reverse("intervention:index")
|
|
|
|
tmp_intervention = Intervention()
|
|
identifier = tmp_intervention._generate_new_identifier()
|
|
self.initialize_form_field("identifier", identifier)
|
|
|
|
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)
|
|
_type = self.cleaned_data.get("type", None)
|
|
laws = self.cleaned_data.get("laws", None)
|
|
handler = self.cleaned_data.get("handler", None)
|
|
registration_office = self.cleaned_data.get("registration_office", None)
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
|
registration_file_number = self.cleaned_data.get("registration_file_number", None)
|
|
binding_date = self.cleaned_data.get("binding_date", None)
|
|
registration_date = self.cleaned_data.get("registration_date", None)
|
|
comment = self.cleaned_data.get("comment", None)
|
|
|
|
# Create log entry
|
|
action = UserActionLogEntry.objects.create(
|
|
user=user,
|
|
action=UserAction.CREATED,
|
|
)
|
|
|
|
# Create legal data object (without M2M laws first)
|
|
legal_data = LegalData.objects.create(
|
|
registration_date=registration_date,
|
|
binding_date=binding_date,
|
|
process_type=_type,
|
|
)
|
|
# Then add the M2M laws to the object
|
|
legal_data.laws.set(laws)
|
|
|
|
# Create responsible data object
|
|
responsibility_data = ResponsibilityData.objects.create(
|
|
registration_office=registration_office,
|
|
conservation_office=conservation_office,
|
|
registration_file_number=registration_file_number,
|
|
conservation_file_number=conservation_file_number,
|
|
handler=handler,
|
|
)
|
|
|
|
# Process the geometry form
|
|
geometry = geom_form.save(action)
|
|
|
|
# Finally create main object, holding the other objects
|
|
intervention = Intervention.objects.create(
|
|
identifier=identifier,
|
|
title=title,
|
|
responsible=responsibility_data,
|
|
legal=legal_data,
|
|
created=action,
|
|
geometry=geometry,
|
|
comment=comment,
|
|
)
|
|
|
|
# Add the log entry to the main objects log list
|
|
intervention.log.add(action)
|
|
|
|
# Add the performing user as the first user having access to the data
|
|
intervention.users.add(user)
|
|
return intervention
|
|
|
|
|
|
class EditInterventionForm(NewInterventionForm):
|
|
""" Subclasses NewInterventionForm
|
|
|
|
Simply adds initializing of a provided self.instance object into the form fields
|
|
|
|
"""
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
if self.instance is not None:
|
|
self.action_url = reverse("intervention:edit", args=(self.instance.id,))
|
|
self.cancel_redirect = reverse("intervention:open", args=(self.instance.id,))
|
|
self.form_title = _("Edit intervention")
|
|
self.form_caption = ""
|
|
|
|
reg_date = self.instance.legal.registration_date
|
|
bind_date = self.instance.legal.binding_date
|
|
if reg_date is not None:
|
|
reg_date = reg_date.isoformat()
|
|
if bind_date is not None:
|
|
bind_date = bind_date.isoformat()
|
|
|
|
# Initialize form data
|
|
form_data = {
|
|
"identifier": self.instance.identifier,
|
|
"title": self.instance.title,
|
|
"type": self.instance.legal.process_type,
|
|
"laws": list(self.instance.legal.laws.values_list("id", flat=True)),
|
|
"handler": self.instance.responsible.handler,
|
|
"registration_office": self.instance.responsible.registration_office,
|
|
"registration_file_number": self.instance.responsible.registration_file_number,
|
|
"conservation_office": self.instance.responsible.conservation_office,
|
|
"conservation_file_number": self.instance.responsible.conservation_file_number,
|
|
"registration_date": reg_date,
|
|
"binding_date": bind_date,
|
|
"comment": self.instance.comment,
|
|
}
|
|
disabled_fields = [
|
|
"identifier",
|
|
]
|
|
self.load_initial_data(
|
|
form_data,
|
|
disabled_fields,
|
|
)
|
|
|
|
def save(self, user: User, geom_form: SimpleGeomForm):
|
|
""" Overwrite instance with new form data
|
|
|
|
Args:
|
|
user ():
|
|
|
|
Returns:
|
|
|
|
"""
|
|
with transaction.atomic():
|
|
identifier = self.cleaned_data.get("identifier", None)
|
|
title = self.cleaned_data.get("title", None)
|
|
process_type = self.cleaned_data.get("type", None)
|
|
laws = self.cleaned_data.get("laws", None)
|
|
handler = self.cleaned_data.get("handler", None)
|
|
registration_office = self.cleaned_data.get("registration_office", None)
|
|
registration_file_number = self.cleaned_data.get("registration_file_number", None)
|
|
conservation_office = self.cleaned_data.get("conservation_office", None)
|
|
conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
|
|
registration_date = self.cleaned_data.get("registration_date", None)
|
|
binding_date = self.cleaned_data.get("binding_date", None)
|
|
comment = self.cleaned_data.get("comment", None)
|
|
|
|
self.instance.legal.process_type = process_type
|
|
self.instance.legal.registration_date = registration_date
|
|
self.instance.legal.binding_date = binding_date
|
|
self.instance.legal.laws.set(laws)
|
|
self.instance.legal.save()
|
|
|
|
self.instance.responsible.handler = handler
|
|
self.instance.responsible.registration_office = registration_office
|
|
self.instance.responsible.registration_file_number = registration_file_number
|
|
self.instance.responsible.conservation_office = conservation_office
|
|
self.instance.responsible.conservation_file_number = conservation_file_number
|
|
self.instance.responsible.save()
|
|
|
|
user_action = UserActionLogEntry.objects.create(
|
|
user=user,
|
|
timestamp=timezone.now(),
|
|
action=UserAction.EDITED,
|
|
)
|
|
|
|
geometry = geom_form.save(user_action)
|
|
self.instance.geometry = geometry
|
|
self.instance.geometry.save()
|
|
|
|
self.instance.log.add(user_action)
|
|
|
|
self.instance.identifier = identifier
|
|
self.instance.title = title
|
|
self.instance.comment = comment
|
|
self.instance.modified = user_action
|
|
self.instance.save()
|
|
|
|
return self.instance
|
|
|
|
|
|
class OpenInterventionForm(EditInterventionForm):
|
|
"""
|
|
This form is not intended to be used as data-input form. It's used to simplify the rendering of intervention:open
|
|
"""
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
# Resize map
|
|
self.fields["geometry"].widget.attrs["map_width"] = 500
|
|
self.fields["geometry"].widget.attrs["map_height"] = 300
|
|
|
|
# Disable all form fields
|
|
for field in self.fields:
|
|
self.disable_form_field(field)
|