parent
72ed82ecd6
commit
7ee28e526e
@ -11,6 +11,7 @@ from django.contrib.auth.models import User
|
||||
from django.contrib.gis.geos import Polygon
|
||||
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
|
||||
@ -106,7 +107,7 @@ class NewInterventionForm(BaseForm):
|
||||
}
|
||||
),
|
||||
)
|
||||
registration_office_file_number = forms.CharField(
|
||||
registration_file_number = forms.CharField(
|
||||
label=_("Registration office file number"),
|
||||
label_suffix="",
|
||||
max_length=255,
|
||||
@ -117,7 +118,7 @@ class NewInterventionForm(BaseForm):
|
||||
}
|
||||
)
|
||||
)
|
||||
conservation_office_file_number = forms.CharField(
|
||||
conservation_file_number = forms.CharField(
|
||||
label=_("Conservation office file number"),
|
||||
label_suffix="",
|
||||
max_length=255,
|
||||
@ -198,8 +199,8 @@ class NewInterventionForm(BaseForm):
|
||||
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_office_file_number = self.cleaned_data.get("conservation_office_file_number", None)
|
||||
registration_office_file_number = self.cleaned_data.get("registration_office_file_number", 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)
|
||||
@ -223,8 +224,8 @@ class NewInterventionForm(BaseForm):
|
||||
responsibility_data = ResponsibilityData.objects.create(
|
||||
registration_office=registration_office,
|
||||
conservation_office=conservation_office,
|
||||
registration_file_number=registration_office_file_number,
|
||||
conservation_file_number=conservation_office_file_number,
|
||||
registration_file_number=registration_file_number,
|
||||
conservation_file_number=conservation_file_number,
|
||||
handler=handler,
|
||||
)
|
||||
|
||||
@ -251,11 +252,16 @@ class NewInterventionForm(BaseForm):
|
||||
|
||||
|
||||
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:index")
|
||||
self.cancel_redirect = reverse("intervention:open", args=(self.instance.id,))
|
||||
self.form_title = _("Edit intervention")
|
||||
self.form_caption = ""
|
||||
|
||||
@ -263,13 +269,16 @@ class EditInterventionForm(NewInterventionForm):
|
||||
form_data = {
|
||||
"identifier": self.instance.identifier,
|
||||
"title": self.instance.title,
|
||||
"type": self.instance.type,
|
||||
"law": self.instance.law,
|
||||
"handler": self.instance.handler,
|
||||
"data_provider": self.instance.data_provider,
|
||||
"data_provider_detail": self.instance.data_provider_detail,
|
||||
"geometry": self.instance.geometry,
|
||||
"documents": self.instance.documents.all(),
|
||||
"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": self.instance.legal.registration_date.isoformat(),
|
||||
"binding_date": self.instance.legal.binding_date.isoformat(),
|
||||
"comment": self.instance.comment,
|
||||
}
|
||||
disabled_fields = [
|
||||
"identifier",
|
||||
@ -279,7 +288,7 @@ class EditInterventionForm(NewInterventionForm):
|
||||
disabled_fields,
|
||||
)
|
||||
|
||||
def save(self, user: User):
|
||||
def save(self, user: User, geom_form: SimpleGeomForm):
|
||||
""" Overwrite instance with new form data
|
||||
|
||||
Args:
|
||||
@ -291,29 +300,45 @@ class EditInterventionForm(NewInterventionForm):
|
||||
with transaction.atomic():
|
||||
identifier = self.cleaned_data.get("identifier", None)
|
||||
title = self.cleaned_data.get("title", None)
|
||||
_type = self.cleaned_data.get("type", None)
|
||||
law = self.cleaned_data.get("law", None)
|
||||
process_type = self.cleaned_data.get("type", None)
|
||||
laws = self.cleaned_data.get("laws", None)
|
||||
handler = self.cleaned_data.get("handler", None)
|
||||
data_provider = self.cleaned_data.get("data_provider", None)
|
||||
data_provider_detail = self.cleaned_data.get("data_provider_detail", None)
|
||||
geometry = self.cleaned_data.get("geometry", Polygon())
|
||||
documents = self.cleaned_data.get("documents", []) or []
|
||||
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.type = _type
|
||||
self.instance.law = law
|
||||
self.instance.handler = handler
|
||||
self.instance.data_provider = data_provider
|
||||
self.instance.data_provider_detail = data_provider_detail
|
||||
self.instance.geometry = geometry
|
||||
self.instance.save()
|
||||
|
||||
user_action = UserActionLogEntry.objects.create(
|
||||
user=self.user,
|
||||
action=UserAction.EDITED
|
||||
)
|
||||
self.instance.log.add(user_action)
|
||||
self.instance.comment = comment
|
||||
self.instance.modified = user_action
|
||||
self.instance.save()
|
||||
|
||||
|
@ -32,7 +32,7 @@
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if is_default_member %}
|
||||
<a href="{% url 'home' %}" class="mr-2">
|
||||
<a href="{% url 'intervention:edit' intervention.id %}" class="mr-2">
|
||||
<button class="btn btn-default" title="{% trans 'Edit' %}">
|
||||
{% fa5_icon 'edit' %}
|
||||
</button>
|
||||
|
@ -220,19 +220,29 @@ def edit_view(request: HttpRequest, id: str):
|
||||
Returns:
|
||||
|
||||
"""
|
||||
template = "konova/form.html"
|
||||
template = "intervention/new/view.html"
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
data_form = EditInterventionForm(request.POST or None, instance=intervention)
|
||||
geom_form = SimpleGeomForm(request.POST or None, read_only=False, instance=intervention)
|
||||
if request.method == "POST":
|
||||
form = EditInterventionForm(request.POST or None, instance=intervention)
|
||||
if form.is_valid():
|
||||
intervention = form.save(request.user)
|
||||
messages.success(request, _("{} edited").format(intervention))
|
||||
if data_form.is_valid() and geom_form.is_valid():
|
||||
generated_identifier = data_form.cleaned_data.get("identifier", None)
|
||||
intervention = data_form.save(request.user, geom_form)
|
||||
if generated_identifier != intervention.identifier:
|
||||
messages.info(
|
||||
request,
|
||||
_("The identifier '{}' had to be changed to '{}' since another entry has been added in the meanwhile, which uses this identifier")
|
||||
)
|
||||
messages.success(request, _("Intervention {} added").format(intervention.identifier))
|
||||
return redirect("intervention:index")
|
||||
else:
|
||||
messages.error(request, _("Invalid input"))
|
||||
form = EditInterventionForm(instance=intervention)
|
||||
else:
|
||||
# For clarification: nothing in this case
|
||||
pass
|
||||
context = {
|
||||
"form": form,
|
||||
"data_form": data_form,
|
||||
"geom_form": geom_form,
|
||||
}
|
||||
context = BaseContext(request, context).context
|
||||
return render(request, template, context)
|
||||
|
Loading…
Reference in New Issue
Block a user