* adds functionality for EditInterventionForm (WIP)
This commit is contained in:
mipel 2021-09-27 09:40:00 +02:00
parent 72ed82ecd6
commit 7ee28e526e
3 changed files with 77 additions and 42 deletions

View File

@ -11,6 +11,7 @@ from django.contrib.auth.models import User
from django.contrib.gis.geos import Polygon from django.contrib.gis.geos import Polygon
from django.db import transaction from django.db import transaction
from django.urls import reverse from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from codelist.models import KonovaCode 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=_("Registration office file number"),
label_suffix="", label_suffix="",
max_length=255, 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=_("Conservation office file number"),
label_suffix="", label_suffix="",
max_length=255, max_length=255,
@ -198,8 +199,8 @@ class NewInterventionForm(BaseForm):
handler = self.cleaned_data.get("handler", None) handler = self.cleaned_data.get("handler", None)
registration_office = self.cleaned_data.get("registration_office", None) registration_office = self.cleaned_data.get("registration_office", None)
conservation_office = self.cleaned_data.get("conservation_office", None) conservation_office = self.cleaned_data.get("conservation_office", None)
conservation_office_file_number = self.cleaned_data.get("conservation_office_file_number", None) conservation_file_number = self.cleaned_data.get("conservation_file_number", None)
registration_office_file_number = self.cleaned_data.get("registration_office_file_number", None) registration_file_number = self.cleaned_data.get("registration_file_number", None)
binding_date = self.cleaned_data.get("binding_date", None) binding_date = self.cleaned_data.get("binding_date", None)
registration_date = self.cleaned_data.get("registration_date", None) registration_date = self.cleaned_data.get("registration_date", None)
comment = self.cleaned_data.get("comment", None) comment = self.cleaned_data.get("comment", None)
@ -223,8 +224,8 @@ class NewInterventionForm(BaseForm):
responsibility_data = ResponsibilityData.objects.create( responsibility_data = ResponsibilityData.objects.create(
registration_office=registration_office, registration_office=registration_office,
conservation_office=conservation_office, conservation_office=conservation_office,
registration_file_number=registration_office_file_number, registration_file_number=registration_file_number,
conservation_file_number=conservation_office_file_number, conservation_file_number=conservation_file_number,
handler=handler, handler=handler,
) )
@ -251,11 +252,16 @@ class NewInterventionForm(BaseForm):
class EditInterventionForm(NewInterventionForm): class EditInterventionForm(NewInterventionForm):
""" Subclasses NewInterventionForm
Simply adds initializing of a provided self.instance object into the form fields
"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.instance is not None: if self.instance is not None:
self.action_url = reverse("intervention:edit", args=(self.instance.id,)) 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_title = _("Edit intervention")
self.form_caption = "" self.form_caption = ""
@ -263,13 +269,16 @@ class EditInterventionForm(NewInterventionForm):
form_data = { form_data = {
"identifier": self.instance.identifier, "identifier": self.instance.identifier,
"title": self.instance.title, "title": self.instance.title,
"type": self.instance.type, "type": self.instance.legal.process_type,
"law": self.instance.law, "laws": list(self.instance.legal.laws.values_list("id", flat=True)),
"handler": self.instance.handler, "handler": self.instance.responsible.handler,
"data_provider": self.instance.data_provider, "registration_office": self.instance.responsible.registration_office,
"data_provider_detail": self.instance.data_provider_detail, "registration_file_number": self.instance.responsible.registration_file_number,
"geometry": self.instance.geometry, "conservation_office": self.instance.responsible.conservation_office,
"documents": self.instance.documents.all(), "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 = [ disabled_fields = [
"identifier", "identifier",
@ -279,7 +288,7 @@ class EditInterventionForm(NewInterventionForm):
disabled_fields, disabled_fields,
) )
def save(self, user: User): def save(self, user: User, geom_form: SimpleGeomForm):
""" Overwrite instance with new form data """ Overwrite instance with new form data
Args: Args:
@ -291,29 +300,45 @@ class EditInterventionForm(NewInterventionForm):
with transaction.atomic(): with transaction.atomic():
identifier = self.cleaned_data.get("identifier", None) identifier = self.cleaned_data.get("identifier", None)
title = self.cleaned_data.get("title", None) title = self.cleaned_data.get("title", None)
_type = self.cleaned_data.get("type", None) process_type = self.cleaned_data.get("type", None)
law = self.cleaned_data.get("law", None) laws = self.cleaned_data.get("laws", None)
handler = self.cleaned_data.get("handler", None) handler = self.cleaned_data.get("handler", None)
data_provider = self.cleaned_data.get("data_provider", None) registration_office = self.cleaned_data.get("registration_office", None)
data_provider_detail = self.cleaned_data.get("data_provider_detail", None) registration_file_number = self.cleaned_data.get("registration_file_number", None)
geometry = self.cleaned_data.get("geometry", Polygon()) conservation_office = self.cleaned_data.get("conservation_office", None)
documents = self.cleaned_data.get("documents", []) or [] 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.identifier = identifier
self.instance.title = title self.instance.title = title
self.instance.type = _type self.instance.comment = comment
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.modified = user_action self.instance.modified = user_action
self.instance.save() self.instance.save()

View File

@ -32,7 +32,7 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if is_default_member %} {% 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' %}"> <button class="btn btn-default" title="{% trans 'Edit' %}">
{% fa5_icon 'edit' %} {% fa5_icon 'edit' %}
</button> </button>

View File

@ -220,19 +220,29 @@ def edit_view(request: HttpRequest, id: str):
Returns: Returns:
""" """
template = "konova/form.html" template = "intervention/new/view.html"
intervention = get_object_or_404(Intervention, id=id) 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": if request.method == "POST":
form = EditInterventionForm(request.POST or None, instance=intervention) if data_form.is_valid() and geom_form.is_valid():
if form.is_valid(): generated_identifier = data_form.cleaned_data.get("identifier", None)
intervention = form.save(request.user) intervention = data_form.save(request.user, geom_form)
messages.success(request, _("{} edited").format(intervention)) 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") return redirect("intervention:index")
else: else:
messages.error(request, _("Invalid input")) messages.error(request, _("Invalid input"))
form = EditInterventionForm(instance=intervention) else:
# For clarification: nothing in this case
pass
context = { context = {
"form": form, "data_form": data_form,
"geom_form": geom_form,
} }
context = BaseContext(request, context).context context = BaseContext(request, context).context
return render(request, template, context) return render(request, template, context)