CompensationAction views

* replaces function based action views with class based
This commit is contained in:
mpeltriaux 2022-08-22 08:38:23 +02:00
parent 1b5cbba48b
commit 276e7ed4bb
7 changed files with 204 additions and 213 deletions

View File

@ -13,7 +13,8 @@ from compensation.views.compensation.resubmission import CompensationResubmissio
from compensation.views.compensation.report import report_view from compensation.views.compensation.report import report_view
from compensation.views.compensation.deadline import NewCompensationDeadlineView, EditCompensationDeadlineView, \ from compensation.views.compensation.deadline import NewCompensationDeadlineView, EditCompensationDeadlineView, \
RemoveCompensationDeadlineView RemoveCompensationDeadlineView
from compensation.views.compensation.action import action_edit_view, action_new_view, action_remove_view from compensation.views.compensation.action import NewCompensationActionView, EditCompensationActionView, \
RemoveCompensationActionView
from compensation.views.compensation.state import NewCompensationStateView, EditCompensationStateView, \ from compensation.views.compensation.state import NewCompensationStateView, EditCompensationStateView, \
RemoveCompensationStateView RemoveCompensationStateView
from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \ from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \
@ -35,9 +36,9 @@ urlpatterns = [
path('<id>/state/<state_id>/edit', EditCompensationStateView.as_view(), name='state-edit'), path('<id>/state/<state_id>/edit', EditCompensationStateView.as_view(), name='state-edit'),
path('<id>/state/<state_id>/remove', RemoveCompensationStateView.as_view(), name='state-remove'), path('<id>/state/<state_id>/remove', RemoveCompensationStateView.as_view(), name='state-remove'),
path('<id>/action/new', action_new_view, name='new-action'), path('<id>/action/new', NewCompensationActionView.as_view(), name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', EditCompensationActionView.as_view(), name='action-edit'),
path('<id>/action/<action_id>/remove', action_remove_view, name='action-remove'), path('<id>/action/<action_id>/remove', RemoveCompensationActionView.as_view(), name='action-remove'),
path('<id>/deadline/new', NewCompensationDeadlineView.as_view(), name="new-deadline"), path('<id>/deadline/new', NewCompensationDeadlineView.as_view(), name="new-deadline"),
path('<id>/deadline/<deadline_id>/edit', EditCompensationDeadlineView.as_view(), name='deadline-edit'), path('<id>/deadline/<deadline_id>/edit', EditCompensationDeadlineView.as_view(), name='deadline-edit'),

View File

@ -16,7 +16,8 @@ from compensation.views.eco_account.report import report_view
from compensation.views.eco_account.resubmission import EcoAccountResubmissionView from compensation.views.eco_account.resubmission import EcoAccountResubmissionView
from compensation.views.eco_account.state import NewEcoAccountStateView, EditEcoAccountStateView, \ from compensation.views.eco_account.state import NewEcoAccountStateView, EditEcoAccountStateView, \
RemoveEcoAccountStateView RemoveEcoAccountStateView
from compensation.views.eco_account.action import action_edit_view, action_new_view, action_remove_view from compensation.views.eco_account.action import NewEcoAccountActionView, EditEcoAccountActionView, \
RemoveEcoAccountActionView
from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, EditEcoAccountDeadlineView, \ from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, EditEcoAccountDeadlineView, \
RemoveEcoAccountDeadlineView RemoveEcoAccountDeadlineView
from compensation.views.eco_account.share import share_view, create_share_view from compensation.views.eco_account.share import share_view, create_share_view
@ -41,9 +42,9 @@ urlpatterns = [
path('<id>/state/<state_id>/edit', EditEcoAccountStateView.as_view(), name='state-edit'), path('<id>/state/<state_id>/edit', EditEcoAccountStateView.as_view(), name='state-edit'),
path('<id>/state/<state_id>/remove', RemoveEcoAccountStateView.as_view(), name='state-remove'), path('<id>/state/<state_id>/remove', RemoveEcoAccountStateView.as_view(), name='state-remove'),
path('<id>/action/new', action_new_view, name='new-action'), path('<id>/action/new', NewEcoAccountActionView.as_view(), name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', EditEcoAccountActionView.as_view(), name='action-edit'),
path('<id>/action/<action_id>/remove', action_remove_view, name='action-remove'), path('<id>/action/<action_id>/remove', RemoveEcoAccountActionView.as_view(), name='action-remove'),
path('<id>/deadline/new', NewEcoAccountDeadlineView.as_view(), name="new-deadline"), path('<id>/deadline/new', NewEcoAccountDeadlineView.as_view(), name="new-deadline"),
path('<id>/deadline/<deadline_id>/edit', EditEcoAccountDeadlineView.as_view(), name='deadline-edit'), path('<id>/deadline/<deadline_id>/edit', EditEcoAccountDeadlineView.as_view(), name='deadline-edit'),

View File

@ -9,6 +9,7 @@ from django.contrib.auth.decorators import login_required
from django.http import HttpRequest from django.http import HttpRequest
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.urls import reverse from django.urls import reverse
from django.utils.decorators import method_decorator
from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \ from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \
EditCompensationActionModalForm, NewCompensationActionModalForm EditCompensationActionModalForm, NewCompensationActionModalForm
@ -16,74 +17,38 @@ from compensation.models import Compensation, CompensationAction
from konova.decorators import shared_access_required, default_group_required from konova.decorators import shared_access_required, default_group_required
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \ from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \
COMPENSATION_ACTION_ADDED COMPENSATION_ACTION_ADDED
from konova.views.action import AbstractNewCompensationActionView, AbstractEditCompensationActionView, \
AbstractRemoveCompensationActionView
@login_required class NewCompensationActionView(AbstractNewCompensationActionView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
def action_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new actions for a compensation
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(default_group_required)
id (str): The compensation's id to which the new state will be related @method_decorator(shared_access_required(Compensation, "id"))
def dispatch(self, request, *args, **kwargs):
Returns: return super().dispatch(request, *args, **kwargs)
"""
comp = get_object_or_404(Compensation, id=id)
form = NewCompensationActionModalForm(request.POST or None, instance=comp, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_ADDED,
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
)
@login_required class EditCompensationActionView(AbstractEditCompensationActionView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
def action_edit_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for editing actions for a compensation
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(default_group_required)
id (str): The compensation's id @method_decorator(shared_access_required(Compensation, "id"))
action_id (str): The action's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
Returns:
"""
comp = get_object_or_404(Compensation, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = EditCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_EDITED,
redirect_url=reverse("compensation:detail", args=(id,)) + "#related_data"
)
@login_required class RemoveCompensationActionView(AbstractRemoveCompensationActionView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
def action_remove_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for removing a compensation action
Args:
request (HttpRequest): The incoming request
id (str): The compensation's id
id (str): The action's id
Returns:
"""
comp = get_object_or_404(Compensation, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = RemoveCompensationActionModalForm(request.POST or None, instance=comp, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_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)

View File

@ -6,84 +6,42 @@ Created on: 19.08.22
""" """
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpRequest from django.utils.decorators import method_decorator
from django.shortcuts import get_object_or_404
from django.urls import reverse
from compensation.forms.modals.compensation_action import EditCompensationActionModalForm, \ from compensation.models import EcoAccount
RemoveCompensationActionModalForm, NewCompensationActionModalForm
from compensation.models import EcoAccount, CompensationAction
from konova.decorators import shared_access_required, default_group_required from konova.decorators import shared_access_required, default_group_required
from konova.utils.message_templates import COMPENSATION_ACTION_EDITED, COMPENSATION_ACTION_REMOVED, \ from konova.views.action import AbstractNewCompensationActionView, AbstractEditCompensationActionView, \
COMPENSATION_ACTION_ADDED AbstractRemoveCompensationActionView
@login_required class NewEcoAccountActionView(AbstractNewCompensationActionView):
@default_group_required model = EcoAccount
@shared_access_required(EcoAccount, "id") redirect_url = "compensation:acc:detail"
def action_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new actions for an eco account
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(default_group_required)
id (str): The account's id to which the new state will be related @method_decorator(shared_access_required(EcoAccount, "id"))
def dispatch(self, request, *args, **kwargs):
Returns: return super().dispatch(request, *args, **kwargs)
"""
acc = get_object_or_404(EcoAccount, id=id)
form = NewCompensationActionModalForm(request.POST or None, instance=acc, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_ADDED,
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
)
@login_required class EditEcoAccountActionView(AbstractEditCompensationActionView):
@default_group_required model = EcoAccount
@shared_access_required(EcoAccount, "id") redirect_url = "compensation:acc:detail"
def action_remove_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for removing a compensation action
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(default_group_required)
id (str): The compensation's id @method_decorator(shared_access_required(EcoAccount, "id"))
id (str): The action's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
Returns:
"""
acc = get_object_or_404(EcoAccount, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = RemoveCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_REMOVED,
redirect_url=reverse("compensation:acc:detail", args=(id,)) + "#related_data"
)
@login_required class RemoveEcoAccountActionView(AbstractRemoveCompensationActionView):
@default_group_required model = EcoAccount
@shared_access_required(EcoAccount, "id") redirect_url = "compensation:acc:detail"
def action_edit_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for editing a compensation action
Args:
request (HttpRequest): The incoming request
id (str): The compensation's id
id (str): The action's id
Returns:
"""
acc = get_object_or_404(EcoAccount, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = EditCompensationActionModalForm(request.POST or None, instance=acc, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_EDITED,
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)

View File

@ -7,7 +7,7 @@ Created on: 19.08.21
""" """
from django.urls import path from django.urls import path
from ema.views.action import action_new_view, action_edit_view, action_remove_view from ema.views.action import NewEmaActionView, EditEmaActionView, RemoveEmaActionView
from ema.views.deadline import NewEmaDeadlineView, EditEmaDeadlineView, RemoveEmaDeadlineView from ema.views.deadline import NewEmaDeadlineView, EditEmaDeadlineView, RemoveEmaDeadlineView
from ema.views.document import NewEmaDocumentView, EditEmaDocumentView, RemoveEmaDocumentView, GetEmaDocumentView from ema.views.document import NewEmaDocumentView, EditEmaDocumentView, RemoveEmaDocumentView, GetEmaDocumentView
from ema.views.ema import index_view, new_view, new_id_view, detail_view, edit_view, remove_view from ema.views.ema import index_view, new_view, new_id_view, detail_view, edit_view, remove_view
@ -35,9 +35,9 @@ urlpatterns = [
path('<id>/state/<state_id>/remove', RemoveEmaStateView.as_view(), name='state-remove'), path('<id>/state/<state_id>/remove', RemoveEmaStateView.as_view(), name='state-remove'),
path('<id>/state/<state_id>/edit', EditEmaStateView.as_view(), name='state-edit'), path('<id>/state/<state_id>/edit', EditEmaStateView.as_view(), name='state-edit'),
path('<id>/action/new', action_new_view, name='new-action'), path('<id>/action/new', NewEmaActionView.as_view(), name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', EditEmaActionView.as_view(), name='action-edit'),
path('<id>/action/<action_id>/remove', action_remove_view, name='action-remove'), path('<id>/action/<action_id>/remove', RemoveEmaActionView.as_view(), name='action-remove'),
path('<id>/deadline/new', NewEmaDeadlineView.as_view(), name="new-deadline"), path('<id>/deadline/new', NewEmaDeadlineView.as_view(), name="new-deadline"),
path('<id>/deadline/<deadline_id>/edit', EditEmaDeadlineView.as_view(), name='deadline-edit'), path('<id>/deadline/<deadline_id>/edit', EditEmaDeadlineView.as_view(), name='deadline-edit'),

View File

@ -6,84 +6,42 @@ Created on: 19.08.22
""" """
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpRequest from django.utils.decorators import method_decorator
from django.shortcuts import get_object_or_404
from django.urls import reverse
from compensation.forms.modals.compensation_action import RemoveCompensationActionModalForm, \
EditCompensationActionModalForm, NewCompensationActionModalForm
from compensation.models import CompensationAction
from ema.models import Ema from ema.models import Ema
from konova.decorators import shared_access_required, conservation_office_group_required from konova.decorators import shared_access_required, conservation_office_group_required
from konova.utils.message_templates import COMPENSATION_ACTION_REMOVED, COMPENSATION_ACTION_EDITED, \ from konova.views.action import AbstractNewCompensationActionView, AbstractEditCompensationActionView, \
COMPENSATION_ACTION_ADDED AbstractRemoveCompensationActionView
@login_required class NewEmaActionView(AbstractNewCompensationActionView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
def action_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new actions for an EMA
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(conservation_office_group_required)
id (str): The EMA's id to which the new state will be related @method_decorator(shared_access_required(Ema, "id"))
def dispatch(self, request, *args, **kwargs):
Returns: return super().dispatch(request, *args, **kwargs)
"""
ema = get_object_or_404(Ema, id=id)
form = NewCompensationActionModalForm(request.POST or None, instance=ema, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_ADDED,
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
)
@login_required class EditEmaActionView(AbstractEditCompensationActionView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
def action_edit_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for editing an actions for an EMA
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(conservation_office_group_required)
id (str): The EMA's id @method_decorator(shared_access_required(Ema, "id"))
action_id (str): The action id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
Returns:
"""
ema = get_object_or_404(Ema, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = EditCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_EDITED,
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
)
@login_required class RemoveEmaActionView(AbstractRemoveCompensationActionView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
def action_remove_view(request: HttpRequest, id: str, action_id: str):
""" Renders a form for removing an EMA action
Args: @method_decorator(login_required)
request (HttpRequest): The incoming request @method_decorator(conservation_office_group_required)
id (str): The ema id @method_decorator(shared_access_required(Ema, "id"))
id (str): The action's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
Returns:
"""
ema = get_object_or_404(Ema, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = RemoveCompensationActionModalForm(request.POST or None, instance=ema, action=action, request=request)
return form.process_request(
request,
msg_success=COMPENSATION_ACTION_REMOVED,
redirect_url=reverse("ema:detail", args=(id,)) + "#related_data"
)

108
konova/views/action.py Normal file
View File

@ -0,0 +1,108 @@
"""
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.compensation_action import NewCompensationActionModalForm, \
EditCompensationActionModalForm, RemoveCompensationActionModalForm
from compensation.models import CompensationAction
from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_EDITED, \
COMPENSATION_STATE_REMOVED
class AbstractCompensationActionView(View):
model = None
redirect_url = None
class Meta:
abstract = True
class AbstractNewCompensationActionView(AbstractCompensationActionView):
class Meta:
abstract = True
def get(self, request, id: str):
""" Renders a form for adding new actions
Args:
request (HttpRequest): The incoming request
id (str): The object's id to which the new action will be related
Returns:
"""
obj = get_object_or_404(self.model, id=id)
form = NewCompensationActionModalForm(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 AbstractEditCompensationActionView(AbstractCompensationActionView):
class Meta:
abstract = True
def get(self, request, id: str, action_id: str):
""" Renders a form for editing a action
Args:
request (HttpRequest): The incoming request
id (str): The object id
action_id (str): The action's id
Returns:
"""
obj = get_object_or_404(self.model, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = EditCompensationActionModalForm(request.POST or None, instance=obj, action=action, 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, action_id: str):
return self.get(request, id, action_id)
class AbstractRemoveCompensationActionView(AbstractCompensationActionView):
class Meta:
abstract = True
def get(self, request, id: str, action_id: str):
""" Renders a form for removing aaction
Args:
request (HttpRequest): The incoming request
id (str): The object id
action_id (str): The action's id
Returns:
"""
obj = get_object_or_404(self.model, id=id)
action = get_object_or_404(CompensationAction, id=action_id)
form = RemoveCompensationActionModalForm(request.POST or None, instance=obj, action=action, 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, action_id: str):
return self.get(request, id, action_id)