From 0c261196a4dfc217df3c779021de537c6002070b Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Thu, 10 Feb 2022 11:02:30 +0100 Subject: [PATCH] #86 Edit states compensation * adds support for editing of states * adds buttons and urls for compensation --- compensation/forms/modalForms.py | 25 +++++++++++++++- .../compensation/includes/states-after.html | 7 +++-- .../compensation/includes/states-before.html | 7 +++-- compensation/urls/compensation.py | 1 + compensation/views/compensation.py | 29 +++++++++++++++++-- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/compensation/forms/modalForms.py b/compensation/forms/modalForms.py index 5368077c..ddf8502b 100644 --- a/compensation/forms/modalForms.py +++ b/compensation/forms/modalForms.py @@ -21,7 +21,7 @@ from konova.contexts import BaseContext from konova.forms import BaseModalForm, NewDocumentModalForm, RemoveModalForm from konova.models import DeadlineType from konova.utils.message_templates import FORM_INVALID, ADDED_COMPENSATION_STATE, ADDED_DEADLINE, \ - ADDED_COMPENSATION_ACTION, PAYMENT_EDITED + ADDED_COMPENSATION_ACTION, PAYMENT_EDITED, COMPENSATION_STATE_EDITED class NewPaymentForm(BaseModalForm): @@ -261,6 +261,29 @@ class NewStateModalForm(BaseModalForm): raise NotImplementedError +class EditCompensationStateModalForm(NewStateModalForm): + state = None + + def __init__(self, *args, **kwargs): + self.state = kwargs.pop("state", None) + super().__init__(*args, **kwargs) + form_data = { + "biotope_type": self.state.biotope_type, + "biotope_extra": self.state.biotope_type_details.all(), + "surface": self.state.surface, + } + self.load_initial_data(form_data) + + def save(self, is_before_state: bool = False): + state = self.state + state.biotope_type = self.cleaned_data.get("biotope_type", None) + state.biotope_type_details.set(self.cleaned_data.get("biotope_extra", [])) + state.surface = self.cleaned_data.get("surface", None) + state.save() + self.instance.mark_as_edited(self.user, self.request, edit_comment=COMPENSATION_STATE_EDITED) + return state + + class RemoveCompensationStateModalForm(RemoveModalForm): """ Removing modal form for CompensationState diff --git a/compensation/templates/compensation/detail/compensation/includes/states-after.html b/compensation/templates/compensation/detail/compensation/includes/states-after.html index b3e86a26..c0d8b2d5 100644 --- a/compensation/templates/compensation/detail/compensation/includes/states-after.html +++ b/compensation/templates/compensation/detail/compensation/includes/states-after.html @@ -57,9 +57,12 @@ {% endif %} {{ state.surface|floatformat:2 }} m² - + {% if is_default_member and has_access %} - + {% endif %} diff --git a/compensation/templates/compensation/detail/compensation/includes/states-before.html b/compensation/templates/compensation/detail/compensation/includes/states-before.html index c3c7e0f1..50855430 100644 --- a/compensation/templates/compensation/detail/compensation/includes/states-before.html +++ b/compensation/templates/compensation/detail/compensation/includes/states-before.html @@ -57,9 +57,12 @@ {% endif %} {{ state.surface|floatformat:2 }} m² - + {% if is_default_member and has_access %} - + {% endif %} diff --git a/compensation/urls/compensation.py b/compensation/urls/compensation.py index ea50b011..9d644e73 100644 --- a/compensation/urls/compensation.py +++ b/compensation/urls/compensation.py @@ -21,6 +21,7 @@ urlpatterns = [ path('/state/new', state_new_view, name='new-state'), path('/action/new', action_new_view, name='new-action'), path('/state//remove', state_remove_view, name='state-remove'), + path('/state//edit', state_edit_view, name='state-edit'), path('/action//remove', action_remove_view, name='action-remove'), path('/deadline/new', deadline_new_view, name="new-deadline"), path('/deadline//remove', deadline_remove_view, name='deadline-remove'), diff --git a/compensation/views/compensation.py b/compensation/views/compensation.py index 40ea9335..3baf03a8 100644 --- a/compensation/views/compensation.py +++ b/compensation/views/compensation.py @@ -6,7 +6,8 @@ from django.utils.translation import gettext_lazy as _ from compensation.forms.forms import NewCompensationForm, EditCompensationForm from compensation.forms.modalForms import NewStateModalForm, NewDeadlineModalForm, NewActionModalForm, \ - NewCompensationDocumentModalForm, RemoveCompensationActionModalForm, RemoveCompensationStateModalForm + NewCompensationDocumentModalForm, RemoveCompensationActionModalForm, RemoveCompensationStateModalForm, \ + EditCompensationStateModalForm from compensation.models import Compensation, CompensationState, CompensationAction, CompensationDocument from compensation.tables import CompensationTable from intervention.models import Intervention @@ -20,7 +21,7 @@ from konova.utils.generators import generate_qr_code from konova.utils.message_templates import FORM_INVALID, IDENTIFIER_REPLACED, DATA_UNSHARED_EXPLANATION, \ CHECKED_RECORDED_RESET, COMPENSATION_ADDED_TEMPLATE, COMPENSATION_REMOVED_TEMPLATE, DOCUMENT_ADDED, \ COMPENSATION_STATE_REMOVED, COMPENSATION_STATE_ADDED, COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_ADDED, \ - DEADLINE_ADDED, DEADLINE_REMOVED, DOCUMENT_EDITED + DEADLINE_ADDED, DEADLINE_REMOVED, DOCUMENT_EDITED, COMPENSATION_STATE_EDITED from konova.utils.user_checks import in_group @@ -469,6 +470,30 @@ def state_remove_view(request: HttpRequest, id: str, state_id: str): ) +@login_required +@default_group_required +@shared_access_required(Compensation, "id") +def state_edit_view(request: HttpRequest, id: str, state_id: str): + """ Renders a form for editing a compensation state + + Args: + request (HttpRequest): The incoming request + id (str): The compensation's id + state_id (str): The state's id + + Returns: + + """ + comp = get_object_or_404(Compensation, id=id) + state = get_object_or_404(CompensationState, id=state_id) + form = EditCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request) + return form.process_request( + request, + msg_success=COMPENSATION_STATE_EDITED, + redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data" + ) + + @login_required @default_group_required @shared_access_required(Compensation, "id")