From a16f68012d1b8f1f49f87049a304430ed96abf27 Mon Sep 17 00:00:00 2001 From: mpeltriaux Date: Mon, 22 Aug 2022 10:17:49 +0200 Subject: [PATCH] EcoAccount views * splits compensation/views/eco_account.py (+700 lines) into separate files in new module * view files can now be found in /compensation/views/eco_account/... --- compensation/urls/eco_account.py | 9 +- compensation/views/eco_account/deduction.py | 108 +++++----------- intervention/urls.py | 9 +- intervention/views/deduction.py | 106 +++++---------- konova/utils/message_templates.py | 1 + konova/views/deduction.py | 135 ++++++++++++++++++++ 6 files changed, 209 insertions(+), 159 deletions(-) create mode 100644 konova/views/deduction.py diff --git a/compensation/urls/eco_account.py b/compensation/urls/eco_account.py index 01fafc47..af430a93 100644 --- a/compensation/urls/eco_account.py +++ b/compensation/urls/eco_account.py @@ -23,7 +23,8 @@ from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, E from compensation.views.eco_account.share import share_view, create_share_view from compensation.views.eco_account.document import GetEcoAccountDocumentView, NewEcoAccountDocumentView, \ EditEcoAccountDocumentView, RemoveEcoAccountDocumentView -from compensation.views.eco_account.deduction import deduction_edit_view, deduction_remove_view, new_deduction_view +from compensation.views.eco_account.deduction import NewEcoAccountDeductionView, EditEcoAccountDeductionView, \ + RemoveEcoAccountDeductionView app_name = "acc" urlpatterns = [ @@ -60,9 +61,9 @@ urlpatterns = [ path('/document//remove/', RemoveEcoAccountDocumentView.as_view(), name='remove-doc'), # Eco-account deductions - path('/deduction//remove', deduction_remove_view, name='remove-deduction'), - path('/deduction//edit', deduction_edit_view, name='edit-deduction'), - path('/deduct/new', new_deduction_view, name='new-deduction'), + path('/deduction//remove', RemoveEcoAccountDeductionView.as_view(), name='remove-deduction'), + path('/deduction//edit', EditEcoAccountDeductionView.as_view(), name='edit-deduction'), + path('/deduct/new', NewEcoAccountDeductionView.as_view(), name='new-deduction'), # Autocomplete path("atcmplt/eco-accounts", EcoAccountAutocomplete.as_view(), name="autocomplete"), diff --git a/compensation/views/eco_account/deduction.py b/compensation/views/eco_account/deduction.py index e9b1dcb9..c7598bb8 100644 --- a/compensation/views/eco_account/deduction.py +++ b/compensation/views/eco_account/deduction.py @@ -6,96 +6,50 @@ 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.shortcuts import get_object_or_404 -from django.urls import reverse +from django.http import Http404 +from django.utils.decorators import method_decorator from compensation.models import EcoAccount -from intervention.forms.modals.deduction import EditEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \ - NewEcoAccountDeductionModalForm from konova.decorators import default_group_required -from konova.utils.message_templates import DEDUCTION_EDITED, DEDUCTION_REMOVED, DEDUCTION_ADDED +from konova.views.deduction import AbstractNewDeductionView, AbstractEditDeductionView, AbstractRemoveDeductionView -@login_required -@default_group_required -def new_deduction_view(request: HttpRequest, id: str): - """ Renders a modal form view for creating deductions +class NewEcoAccountDeductionView(AbstractNewDeductionView): + model = EcoAccount + redirect_url = "compensation:acc:detail" - Args: - request (HttpRequest): THe incoming request - id (str): The eco account's id + @method_decorator(login_required) + @method_decorator(default_group_required) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) - Returns: - - """ - acc = get_object_or_404(EcoAccount, id=id) - if not acc.recorded: - raise Http404() - form = NewEcoAccountDeductionModalForm(request.POST or None, instance=acc, request=request) - return form.process_request( - request, - msg_success=DEDUCTION_ADDED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) + def _custom_check(self, obj): + if not obj.recorded: + raise Http404() -@login_required -@default_group_required -def deduction_remove_view(request: HttpRequest, id: str, deduction_id: str): - """ Renders a modal view for removing deductions +class EditEcoAccountDeductionView(AbstractEditDeductionView): + def _custom_check(self, obj): + pass - Args: - request (HttpRequest): The incoming request - id (str): The eco account's id - deduction_id (str): The deduction's id + model = EcoAccount + redirect_url = "compensation:acc:detail" - Returns: - - """ - acc = get_object_or_404(EcoAccount, id=id) - try: - eco_deduction = acc.deductions.get(id=deduction_id) - if not eco_deduction.intervention.is_shared_with(request.user): - raise ObjectDoesNotExist() - except ObjectDoesNotExist: - raise Http404("Unknown deduction") - - form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request) - return form.process_request( - request=request, - msg_success=DEDUCTION_REMOVED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@default_group_required -def deduction_edit_view(request: HttpRequest, id: str, deduction_id: str): - """ Renders a modal view for editing deductions +class RemoveEcoAccountDeductionView(AbstractRemoveDeductionView): + def _custom_check(self, obj): + pass - Args: - request (HttpRequest): The incoming request - id (str): The eco account's id - deduction_id (str): The deduction's id - - Returns: - - """ - acc = get_object_or_404(EcoAccount, id=id) - try: - eco_deduction = acc.deductions.get(id=deduction_id) - if not eco_deduction.intervention.is_shared_with(request.user): - raise ObjectDoesNotExist - except ObjectDoesNotExist: - raise Http404("Unknown deduction") - - form = EditEcoAccountDeductionModalForm(request.POST or None, instance=acc, deduction=eco_deduction, request=request) - return form.process_request( - request=request, - msg_success=DEDUCTION_EDITED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) + model = EcoAccount + redirect_url = "compensation:acc:detail" + @method_decorator(login_required) + @method_decorator(default_group_required) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) diff --git a/intervention/urls.py b/intervention/urls.py index 20bd5c16..69c5e767 100644 --- a/intervention/urls.py +++ b/intervention/urls.py @@ -10,7 +10,8 @@ from django.urls import path from intervention.autocomplete.intervention import InterventionAutocomplete from intervention.views.check import check_view from intervention.views.compensation import remove_compensation_view -from intervention.views.deduction import new_deduction_view, edit_deduction_view, remove_deduction_view +from intervention.views.deduction import NewInterventionDeductionView, EditInterventionDeductionView, \ + RemoveInterventionDeductionView from intervention.views.document import NewInterventionDocumentView, GetInterventionDocumentView, \ RemoveInterventionDocumentView, EditInterventionDocumentView from intervention.views.intervention import index_view, new_view, new_id_view, detail_view, edit_view, remove_view @@ -48,9 +49,9 @@ urlpatterns = [ path('/document//edit/', EditInterventionDocumentView.as_view(), name='edit-doc'), # Deductions - path('/deduction/new', new_deduction_view, name='new-deduction'), - path('/deduction//edit', edit_deduction_view, name='edit-deduction'), - path('/deduction//remove', remove_deduction_view, name='remove-deduction'), + path('/deduction/new', NewInterventionDeductionView.as_view(), name='new-deduction'), + path('/deduction//edit', EditInterventionDeductionView.as_view(), name='edit-deduction'), + path('/deduction//remove', RemoveInterventionDeductionView.as_view(), name='remove-deduction'), # Revocation routes path('/revocation/new', new_revocation_view, name='new-revocation'), diff --git a/intervention/views/deduction.py b/intervention/views/deduction.py index 2fb841c1..962fe807 100644 --- a/intervention/views/deduction.py +++ b/intervention/views/deduction.py @@ -6,92 +6,50 @@ 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.shortcuts import get_object_or_404 -from django.urls import reverse +from django.utils.decorators import method_decorator -from intervention.forms.modals.deduction import NewEcoAccountDeductionModalForm, RemoveEcoAccountDeductionModalForm, \ - EditEcoAccountDeductionModalForm from intervention.models import Intervention from konova.decorators import default_group_required, shared_access_required -from konova.utils.message_templates import DEDUCTION_ADDED, DEDUCTION_REMOVED, DEDUCTION_EDITED +from konova.views.deduction import AbstractNewDeductionView, AbstractEditDeductionView, AbstractRemoveDeductionView -@login_required -@default_group_required -@shared_access_required(Intervention, "id") -def new_deduction_view(request: HttpRequest, id: str): - """ Renders a modal form view for creating deductions +class NewInterventionDeductionView(AbstractNewDeductionView): + def _custom_check(self, obj): + pass - Args: - request (HttpRequest): The incoming request - id (str): The intervention's id which shall benefit from this deduction + model = Intervention + redirect_url = "intervention:detail" - Returns: - - """ - intervention = get_object_or_404(Intervention, id=id) - form = NewEcoAccountDeductionModalForm(request.POST or None, instance=intervention, request=request) - return form.process_request( - request, - msg_success=DEDUCTION_ADDED, - redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data", - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Intervention, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@default_group_required -@shared_access_required(Intervention, "id") -def remove_deduction_view(request: HttpRequest, id: str, deduction_id: str): - """ Renders a modal view for removing deductions +class EditInterventionDeductionView(AbstractEditDeductionView): + def _custom_check(self, obj): + pass - Args: - request (HttpRequest): The incoming request - id (str): The intervention's id - deduction_id (str): The deduction's id + model = Intervention + redirect_url = "intervention:detail" - Returns: - - """ - intervention = get_object_or_404(Intervention, id=id) - try: - eco_deduction = intervention.deductions.get(id=deduction_id) - except ObjectDoesNotExist: - raise Http404("Unknown deduction") - - form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request) - return form.process_request( - request=request, - msg_success=DEDUCTION_REMOVED, - redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Intervention, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@default_group_required -@shared_access_required(Intervention, "id") -def edit_deduction_view(request: HttpRequest, id: str, deduction_id: str): - """ Renders a modal view for removing deductions +class RemoveInterventionDeductionView(AbstractRemoveDeductionView): + def _custom_check(self, obj): + pass - Args: - request (HttpRequest): The incoming request - id (str): The intervention's id - deduction_id (str): The deduction's id - - Returns: - - """ - intervention = get_object_or_404(Intervention, id=id) - try: - eco_deduction = intervention.deductions.get(id=deduction_id) - except ObjectDoesNotExist: - raise Http404("Unknown deduction") - - form = EditEcoAccountDeductionModalForm(request.POST or None, instance=intervention, deduction=eco_deduction, request=request) - return form.process_request( - request=request, - msg_success=DEDUCTION_EDITED, - redirect_url=reverse("intervention:detail", args=(id,)) + "#related_data" - ) + model = Intervention + redirect_url = "intervention:detail" + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Intervention, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) diff --git a/konova/utils/message_templates.py b/konova/utils/message_templates.py index f6e3ca18..e144a97f 100644 --- a/konova/utils/message_templates.py +++ b/konova/utils/message_templates.py @@ -51,6 +51,7 @@ COMPENSATION_ACTION_REMOVED = _("Action removed") DEDUCTION_ADDED = _("Deduction added") DEDUCTION_EDITED = _("Deduction edited") DEDUCTION_REMOVED = _("Deduction removed") +DEDUCTION_UNKNOWN = _("Unknown deduction") # DEADLINE DEADLINE_ADDED = _("Deadline added") diff --git a/konova/views/deduction.py b/konova/views/deduction.py new file mode 100644 index 00000000..f31a7015 --- /dev/null +++ b/konova/views/deduction.py @@ -0,0 +1,135 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 22.08.22 + +""" +from django.core.exceptions import ObjectDoesNotExist +from django.http import Http404 +from django.shortcuts import get_object_or_404 +from django.urls import reverse +from django.views import View + +from intervention.forms.modals.deduction import NewEcoAccountDeductionModalForm, EditEcoAccountDeductionModalForm, \ + RemoveEcoAccountDeductionModalForm +from konova.utils.message_templates import DEDUCTION_ADDED, DEDUCTION_EDITED, DEDUCTION_REMOVED, DEDUCTION_UNKNOWN + + +class AbstractDeductionView(View): + model = None + redirect_url = None + + def _custom_check(self, obj): + """ + Can be used by inheriting classes to provide custom checks before further processing + + """ + raise NotImplementedError("Must be implemented in subclasses") + + +class AbstractNewDeductionView(AbstractDeductionView): + + class Meta: + abstract = True + + def get(self, request, id: str): + """ Renders a modal form view for creating deductions + + Args: + request (HttpRequest): The incoming request + id (str): The obj's id which shall benefit from this deduction + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + self._custom_check(obj) + form = NewEcoAccountDeductionModalForm(request.POST or None, instance=obj, request=request) + return form.process_request( + request, + msg_success=DEDUCTION_ADDED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data", + ) + + def post(self, request, id: str): + return self.get(request, id) + + +class AbstractEditDeductionView(AbstractDeductionView): + + def _custom_check(self, obj): + pass + + class Meta: + abstract = True + + def get(self, request, id: str, deduction_id: str): + """ Renders a modal view for editing deductions + + Args: + request (HttpRequest): The incoming request + id (str): The object's id + deduction_id (str): The deduction's id + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + self._custom_check(obj) + try: + eco_deduction = obj.deductions.get(id=deduction_id) + if not eco_deduction.intervention.is_shared_with(request.user): + raise ObjectDoesNotExist + except ObjectDoesNotExist: + raise Http404(DEDUCTION_UNKNOWN) + + form = EditEcoAccountDeductionModalForm(request.POST or None, instance=obj, deduction=eco_deduction, + request=request) + return form.process_request( + request=request, + msg_success=DEDUCTION_EDITED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data" + ) + + def post(self, request, id: str, deduction_id: str): + return self.get(request, id, deduction_id) + + +class AbstractRemoveDeductionView(AbstractDeductionView): + + def _custom_check(self, obj): + pass + + class Meta: + abstract = True + + def get(self, request, id: str, deduction_id: str): + """ Renders a modal view for removing deductions + + Args: + request (HttpRequest): The incoming request + id (str): The object's id + deduction_id (str): The deduction's id + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + self._custom_check(obj) + try: + eco_deduction = obj.deductions.get(id=deduction_id) + if not eco_deduction.intervention.is_shared_with(request.user): + raise ObjectDoesNotExist() + except ObjectDoesNotExist: + raise Http404(DEDUCTION_UNKNOWN) + form = RemoveEcoAccountDeductionModalForm(request.POST or None, instance=obj, deduction=eco_deduction, + request=request) + return form.process_request( + request=request, + msg_success=DEDUCTION_REMOVED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data" + ) + + def post(self, request, id: str, deduction_id: str): + return self.get(request, id, deduction_id)