diff --git a/compensation/urls/compensation.py b/compensation/urls/compensation.py index dd089510..dcd065fd 100644 --- a/compensation/urls/compensation.py +++ b/compensation/urls/compensation.py @@ -14,7 +14,8 @@ from compensation.views.compensation.report import report_view from compensation.views.compensation.deadline import NewCompensationDeadlineView, EditCompensationDeadlineView, \ RemoveCompensationDeadlineView from compensation.views.compensation.action import action_edit_view, action_new_view, action_remove_view -from compensation.views.compensation.state import state_new_view, state_remove_view, state_edit_view +from compensation.views.compensation.state import NewCompensationStateView, EditCompensationStateView, \ + RemoveCompensationStateView from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \ remove_view from compensation.views.compensation.log import CompensationLogView @@ -30,9 +31,9 @@ urlpatterns = [ path('/edit', edit_view, name='edit'), path('/remove', remove_view, name='remove'), - path('/state/new', state_new_view, name='new-state'), - path('/state//edit', state_edit_view, name='state-edit'), - path('/state//remove', state_remove_view, name='state-remove'), + path('/state/new', NewCompensationStateView.as_view(), name='new-state'), + path('/state//edit', EditCompensationStateView.as_view(), name='state-edit'), + path('/state//remove', RemoveCompensationStateView.as_view(), name='state-remove'), path('/action/new', action_new_view, name='new-action'), path('/action//edit', action_edit_view, name='action-edit'), diff --git a/compensation/urls/eco_account.py b/compensation/urls/eco_account.py index 90784fb4..60ee786c 100644 --- a/compensation/urls/eco_account.py +++ b/compensation/urls/eco_account.py @@ -14,7 +14,8 @@ from compensation.views.eco_account.log import EcoAccountLogView from compensation.views.eco_account.record import EcoAccountRecordView from compensation.views.eco_account.report import report_view from compensation.views.eco_account.resubmission import EcoAccountResubmissionView -from compensation.views.eco_account.state import state_new_view, state_remove_view, state_edit_view +from compensation.views.eco_account.state import NewEcoAccountStateView, EditEcoAccountStateView, \ + RemoveEcoAccountStateView from compensation.views.eco_account.action import action_edit_view, action_new_view, action_remove_view from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, EditEcoAccountDeadlineView, \ RemoveEcoAccountDeadlineView @@ -36,9 +37,9 @@ urlpatterns = [ path('/remove', remove_view, name='remove'), path('/resub', EcoAccountResubmissionView.as_view(), name='resubmission-create'), - path('/state/new', state_new_view, name='new-state'), - path('/state//edit', state_edit_view, name='state-edit'), - path('/state//remove', state_remove_view, name='state-remove'), + path('/state/new', NewEcoAccountStateView.as_view(), name='new-state'), + path('/state//edit', EditEcoAccountStateView.as_view(), name='state-edit'), + path('/state//remove', RemoveEcoAccountStateView.as_view(), name='state-remove'), path('/action/new', action_new_view, name='new-action'), path('/action//edit', action_edit_view, name='action-edit'), diff --git a/compensation/views/compensation/state.py b/compensation/views/compensation/state.py index ca8c5d0c..136e8604 100644 --- a/compensation/views/compensation/state.py +++ b/compensation/views/compensation/state.py @@ -6,84 +6,42 @@ Created on: 19.08.22 """ from django.contrib.auth.decorators import login_required -from django.http import HttpRequest -from django.shortcuts import get_object_or_404 -from django.urls import reverse +from django.utils.decorators import method_decorator -from compensation.forms.modals.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \ - NewCompensationStateModalForm -from compensation.models import Compensation, CompensationState +from compensation.models import Compensation from konova.decorators import shared_access_required, default_group_required -from konova.utils.message_templates import COMPENSATION_STATE_EDITED, COMPENSATION_STATE_REMOVED, \ - COMPENSATION_STATE_ADDED +from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \ + AbstractRemoveCompensationStateView -@login_required -@default_group_required -@shared_access_required(Compensation, "id") -def state_new_view(request: HttpRequest, id: str): - """ Renders a form for adding new states for a compensation +class NewCompensationStateView(AbstractNewCompensationStateView): + model = Compensation + redirect_url = "compensation:detail" - Args: - request (HttpRequest): The incoming request - id (str): The compensation's id to which the new state will be related - - Returns: - - """ - comp = get_object_or_404(Compensation, id=id) - form = NewCompensationStateModalForm(request.POST or None, instance=comp, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_ADDED, - redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Compensation, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@default_group_required -@shared_access_required(Compensation, "id") -def state_remove_view(request: HttpRequest, id: str, state_id: str): - """ Renders a form for removing a compensation state +class EditCompensationStateView(AbstractEditCompensationStateView): + model = Compensation + redirect_url = "compensation:detail" - 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 = RemoveCompensationStateModalForm(request.POST or None, instance=comp, state=state, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_REMOVED, - redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Compensation, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@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" - ) +class RemoveCompensationStateView(AbstractRemoveCompensationStateView): + model = Compensation + redirect_url = "compensation:detail" + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(Compensation, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) diff --git a/compensation/views/eco_account/state.py b/compensation/views/eco_account/state.py index 15590fc7..0591a17c 100644 --- a/compensation/views/eco_account/state.py +++ b/compensation/views/eco_account/state.py @@ -6,83 +6,42 @@ Created on: 19.08.22 """ from django.contrib.auth.decorators import login_required -from django.http import HttpRequest -from django.shortcuts import get_object_or_404 -from django.urls import reverse +from django.utils.decorators import method_decorator -from compensation.forms.modals.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \ - NewCompensationStateModalForm -from compensation.models import EcoAccount, CompensationState +from compensation.models import EcoAccount from konova.decorators import shared_access_required, default_group_required -from konova.utils.message_templates import COMPENSATION_STATE_EDITED, COMPENSATION_STATE_REMOVED, \ - COMPENSATION_STATE_ADDED +from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \ + AbstractRemoveCompensationStateView -@login_required -@default_group_required -@shared_access_required(EcoAccount, "id") -def state_new_view(request: HttpRequest, id: str): - """ Renders a form for adding new states for an eco account +class NewEcoAccountStateView(AbstractNewCompensationStateView): + model = EcoAccount + redirect_url = "compensation:acc:detail" - Args: - request (HttpRequest): The incoming request - id (str): The account's id to which the new state will be related - - Returns: - - """ - acc = get_object_or_404(EcoAccount, id=id) - form = NewCompensationStateModalForm(request.POST or None, instance=acc, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_ADDED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) - -@login_required -@default_group_required -@shared_access_required(EcoAccount, "id") -def state_remove_view(request: HttpRequest, id: str, state_id: str): - """ Renders a form for removing a compensation state - - Args: - request (HttpRequest): The incoming request - id (str): The compensation's id - state_id (str): The state's id - - Returns: - - """ - acc = get_object_or_404(EcoAccount, id=id) - state = get_object_or_404(CompensationState, id=state_id) - form = RemoveCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_REMOVED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(EcoAccount, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@default_group_required -@shared_access_required(EcoAccount, "id") -def state_edit_view(request: HttpRequest, id: str, state_id: str): - """ Renders a form for editing a compensation state +class EditEcoAccountStateView(AbstractEditCompensationStateView): + model = EcoAccount + redirect_url = "compensation:acc:detail" - Args: - request (HttpRequest): The incoming request - id (str): The compensation's id - state_id (str): The state's id + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(EcoAccount, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) - Returns: - """ - acc = get_object_or_404(EcoAccount, id=id) - state = get_object_or_404(CompensationState, id=state_id) - form = EditCompensationStateModalForm(request.POST or None, instance=acc, state=state, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_EDITED, - redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data" - ) +class RemoveEcoAccountStateView(AbstractRemoveCompensationStateView): + model = EcoAccount + redirect_url = "compensation:acc:detail" + @method_decorator(login_required) + @method_decorator(default_group_required) + @method_decorator(shared_access_required(EcoAccount, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) diff --git a/ema/urls.py b/ema/urls.py index eda06728..faeb0f2c 100644 --- a/ema/urls.py +++ b/ema/urls.py @@ -16,7 +16,7 @@ from ema.views.record import EmaRecordView from ema.views.report import report_view from ema.views.resubmission import EmaResubmissionView from ema.views.share import share_view, create_share_view -from ema.views.state import state_new_view, state_remove_view, state_edit_view +from ema.views.state import NewEmaStateView, EditEmaStateView, RemoveEmaStateView app_name = "ema" urlpatterns = [ @@ -31,9 +31,9 @@ urlpatterns = [ path('/report', report_view, name='report'), path('/resub', EmaResubmissionView.as_view(), name='resubmission-create'), - path('/state/new', state_new_view, name='new-state'), - path('/state//remove', state_remove_view, name='state-remove'), - path('/state//edit', state_edit_view, name='state-edit'), + path('/state/new', NewEmaStateView.as_view(), name='new-state'), + path('/state//remove', RemoveEmaStateView.as_view(), name='state-remove'), + path('/state//edit', EditEmaStateView.as_view(), name='state-edit'), path('/action/new', action_new_view, name='new-action'), path('/action//edit', action_edit_view, name='action-edit'), diff --git a/ema/views/state.py b/ema/views/state.py index 1c7b0a44..717ae7f7 100644 --- a/ema/views/state.py +++ b/ema/views/state.py @@ -6,85 +6,42 @@ Created on: 19.08.22 """ from django.contrib.auth.decorators import login_required -from django.http import HttpRequest -from django.shortcuts import get_object_or_404 -from django.urls import reverse +from django.utils.decorators import method_decorator -from compensation.forms.modals.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, \ - EditCompensationStateModalForm -from compensation.models import CompensationState from ema.models import Ema from konova.decorators import conservation_office_group_required, shared_access_required -from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_REMOVED, \ - COMPENSATION_STATE_EDITED +from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \ + AbstractRemoveCompensationStateView -@login_required -@conservation_office_group_required -@shared_access_required(Ema, "id") -def state_new_view(request: HttpRequest, id: str): - """ Renders a form for adding new states for an EMA +class NewEmaStateView(AbstractNewCompensationStateView): + model = Ema + redirect_url = "ema:detail" - Args: - request (HttpRequest): The incoming request - id (str): The EMA's id to which the new state will be related - - Returns: - - """ - ema = get_object_or_404(Ema, id=id) - form = NewCompensationStateModalForm(request.POST or None, instance=ema, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_ADDED, - redirect_url=reverse("ema:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(conservation_office_group_required) + @method_decorator(shared_access_required(Ema, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@conservation_office_group_required -@shared_access_required(Ema, "id") -def state_remove_view(request: HttpRequest, id: str, state_id: str): - """ Renders a form for removing an EMA state +class EditEmaStateView(AbstractEditCompensationStateView): + model = Ema + redirect_url = "ema:detail" - Args: - request (HttpRequest): The incoming request - id (str): The ema id - state_id (str): The state's id - - Returns: - - """ - ema = get_object_or_404(Ema, id=id) - state = get_object_or_404(CompensationState, id=state_id) - form = RemoveCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_REMOVED, - redirect_url=reverse("ema:detail", args=(id,)) + "#related_data" - ) + @method_decorator(login_required) + @method_decorator(conservation_office_group_required) + @method_decorator(shared_access_required(Ema, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) -@login_required -@conservation_office_group_required -@shared_access_required(Ema, "id") -def state_edit_view(request: HttpRequest, id: str, state_id: str): - """ Renders a form for editing an EMA state - - Args: - request (HttpRequest): The incoming request - id (str): The ema id - state_id (str): The state's id - - Returns: - - """ - ema = get_object_or_404(Ema, id=id) - state = get_object_or_404(CompensationState, id=state_id) - form = EditCompensationStateModalForm(request.POST or None, instance=ema, state=state, request=request) - return form.process_request( - request, - msg_success=COMPENSATION_STATE_EDITED, - redirect_url=reverse("ema:detail", args=(id,)) + "#related_data" - ) +class RemoveEmaStateView(AbstractRemoveCompensationStateView): + model = Ema + redirect_url = "ema:detail" + @method_decorator(login_required) + @method_decorator(conservation_office_group_required) + @method_decorator(shared_access_required(Ema, "id")) + def dispatch(self, request, *args, **kwargs): + return super().dispatch(request, *args, **kwargs) diff --git a/konova/views/state.py b/konova/views/state.py new file mode 100644 index 00000000..1165e08b --- /dev/null +++ b/konova/views/state.py @@ -0,0 +1,107 @@ +""" +Author: Michel Peltriaux +Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany +Contact: ksp-servicestelle@sgdnord.rlp.de +Created on: 22.08.22 + +""" +from django.shortcuts import get_object_or_404 +from django.urls import reverse +from django.views import View + +from compensation.forms.modals.state import NewCompensationStateModalForm, EditCompensationStateModalForm, \ + RemoveCompensationStateModalForm +from compensation.models import CompensationState +from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_EDITED, \ + COMPENSATION_STATE_REMOVED + + +class AbstractCompensationStateView(View): + model = None + redirect_url = None + + class Meta: + abstract = True + + +class AbstractNewCompensationStateView(AbstractCompensationStateView): + class Meta: + abstract = True + + def get(self, request, id: str): + """ Renders a form for adding new states + + Args: + request (HttpRequest): The incoming request + id (str): The object's id to which the new state will be related + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + form = NewCompensationStateModalForm(request.POST or None, instance=obj, request=request) + return form.process_request( + request, + msg_success=COMPENSATION_STATE_ADDED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data" + ) + + def post(self, request, id: str): + return self.get(request, id) + + +class AbstractEditCompensationStateView(AbstractCompensationStateView): + class Meta: + abstract = True + + def get(self, request, id: str, state_id: str): + """ Renders a form for editing a state + + Args: + request (HttpRequest): The incoming request + id (str): The object id + state_id (str): The state's id + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + state = get_object_or_404(CompensationState, id=state_id) + form = EditCompensationStateModalForm(request.POST or None, instance=obj, state=state, request=request) + return form.process_request( + request, + msg_success=COMPENSATION_STATE_EDITED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data" + ) + + def post(self, request, id: str, state_id: str): + return self.get(request, id, state_id) + + +class AbstractRemoveCompensationStateView(AbstractCompensationStateView): + class Meta: + abstract = True + + def get(self, request, id: str, state_id: str): + """ Renders a form for removing astate + + Args: + request (HttpRequest): The incoming request + id (str): The object id + state_id (str): The state's id + + Returns: + + """ + obj = get_object_or_404(self.model, id=id) + state = get_object_or_404(CompensationState, id=state_id) + form = RemoveCompensationStateModalForm(request.POST or None, instance=obj, state=state, request=request) + return form.process_request( + request, + msg_success=COMPENSATION_STATE_REMOVED, + redirect_url=reverse(self.redirect_url, args=(id,)) + "#related_data" + ) + + def post(self, request, id: str, state_id: str): + return self.get(request, id, state_id) +