# Refactoring eiv-kom remove view
* refactors removing compensation from intervention view * drops unused view on api app
This commit is contained in:
22
intervention/forms/modals/remove.py
Normal file
22
intervention/forms/modals/remove.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Created on: 08.11.25
|
||||
|
||||
"""
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from compensation.models import Compensation
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
|
||||
|
||||
class RemoveCompensationFromInterventionModalForm(RemoveModalForm):
|
||||
""" Specific form for removing a compensation from an intervention
|
||||
|
||||
"""
|
||||
def __init__(self, *args, **kwargs):
|
||||
# The 'instance' that is pushed into the constructor by the generic base class points to the
|
||||
# intervention instead of the compensation, which shall be deleted. Therefore we need to fetch the compensation
|
||||
# and replace the instance parameter with that object
|
||||
instance = get_object_or_404(Compensation, id=kwargs.pop("comp_id"))
|
||||
kwargs["instance"] = instance
|
||||
super().__init__(*args, **kwargs)
|
||||
@@ -9,7 +9,7 @@ from django.urls import path
|
||||
|
||||
from intervention.autocomplete.intervention import InterventionAutocomplete
|
||||
from intervention.views.check import InterventionCheckView
|
||||
from intervention.views.compensation import remove_compensation_view
|
||||
from intervention.views.compensation import RemoveCompensationFromInterventionView
|
||||
from intervention.views.deduction import NewInterventionDeductionView, EditInterventionDeductionView, \
|
||||
RemoveInterventionDeductionView
|
||||
from intervention.views.document import NewInterventionDocumentView, GetInterventionDocumentView, \
|
||||
@@ -41,7 +41,7 @@ urlpatterns = [
|
||||
path('<id>/resub', InterventionResubmissionView.as_view(), name='resubmission-create'),
|
||||
|
||||
# Compensations
|
||||
path('<id>/compensation/<comp_id>/remove', remove_compensation_view, name='remove-compensation'),
|
||||
path('<id>/compensation/<comp_id>/remove', RemoveCompensationFromInterventionView.as_view(), name='remove-compensation'),
|
||||
|
||||
# Documents
|
||||
path('<id>/document/new/', NewInterventionDocumentView.as_view(), name='new-doc'),
|
||||
|
||||
@@ -5,42 +5,36 @@ Contact: ksp-servicestelle@sgdnord.rlp.de
|
||||
Created on: 19.08.22
|
||||
|
||||
"""
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.http import HttpRequest, Http404
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
|
||||
from compensation.models import Compensation
|
||||
from intervention.forms.modals.remove import RemoveCompensationFromInterventionModalForm
|
||||
from intervention.models import Intervention
|
||||
from konova.decorators import shared_access_required, login_required_modal
|
||||
from konova.forms.modals import RemoveModalForm
|
||||
from konova.utils.message_templates import COMPENSATION_REMOVED_TEMPLATE
|
||||
from konova.views.remove import BaseRemoveModalFormView
|
||||
|
||||
|
||||
@login_required_modal
|
||||
@login_required
|
||||
@shared_access_required(Intervention, "id")
|
||||
def remove_compensation_view(request: HttpRequest, id: str, comp_id: str):
|
||||
""" Renders a modal view for removing the compensation
|
||||
class RemoveCompensationFromInterventionView(LoginRequiredMixin, BaseRemoveModalFormView):
|
||||
_MODEL_CLS = Intervention
|
||||
_FORM_CLS = RemoveCompensationFromInterventionModalForm
|
||||
_MSG_SUCCESS = COMPENSATION_REMOVED_TEMPLATE
|
||||
_REDIRECT_URL = "intervention:detail"
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The incoming request
|
||||
id (str): The compensation's id
|
||||
def _user_has_shared_access(self, user, **kwargs):
|
||||
compensation_id = kwargs.get("comp_id", None)
|
||||
compensation = get_object_or_404(Compensation, id=compensation_id)
|
||||
return compensation.is_shared_with(user)
|
||||
|
||||
Returns:
|
||||
def _user_has_permission(self, user, **kwargs):
|
||||
return user.is_default_user()
|
||||
|
||||
"""
|
||||
intervention = get_object_or_404(Intervention, id=id)
|
||||
try:
|
||||
comp = intervention.compensations.get(
|
||||
id=comp_id
|
||||
)
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404("Unknown compensation")
|
||||
form = RemoveModalForm(request.POST or None, instance=comp, request=request)
|
||||
return form.process_request(
|
||||
request=request,
|
||||
msg_success=COMPENSATION_REMOVED_TEMPLATE.format(comp.identifier),
|
||||
redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data",
|
||||
)
|
||||
def _get_msg_success(self, *args, **kwargs):
|
||||
compensation_id = kwargs.get("comp_id", None)
|
||||
compensation = get_object_or_404(Compensation, id=compensation_id)
|
||||
return self._MSG_SUCCESS.format(compensation.identifier)
|
||||
|
||||
def _get_redirect_url(self, *args, **kwargs):
|
||||
obj = kwargs.get("obj")
|
||||
return reverse(self._REDIRECT_URL, args=(obj.id,)) + "#related_data"
|
||||
|
||||
Reference in New Issue
Block a user