State views

* replaces function based state views with class based
This commit is contained in:
mpeltriaux 2022-08-22 08:27:36 +02:00
parent d6057575ea
commit 1b5cbba48b
7 changed files with 204 additions and 221 deletions

View File

@ -14,7 +14,8 @@ 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 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, \ from compensation.views.compensation.compensation import index_view, new_view, new_id_view, detail_view, edit_view, \
remove_view remove_view
from compensation.views.compensation.log import CompensationLogView from compensation.views.compensation.log import CompensationLogView
@ -30,9 +31,9 @@ urlpatterns = [
path('<id>/edit', edit_view, name='edit'), path('<id>/edit', edit_view, name='edit'),
path('<id>/remove', remove_view, name='remove'), path('<id>/remove', remove_view, name='remove'),
path('<id>/state/new', state_new_view, name='new-state'), path('<id>/state/new', NewCompensationStateView.as_view(), name='new-state'),
path('<id>/state/<state_id>/edit', state_edit_view, name='state-edit'), path('<id>/state/<state_id>/edit', EditCompensationStateView.as_view(), name='state-edit'),
path('<id>/state/<state_id>/remove', state_remove_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', action_new_view, name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'),

View File

@ -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.record import EcoAccountRecordView
from compensation.views.eco_account.report import report_view 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 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.action import action_edit_view, action_new_view, action_remove_view
from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, EditEcoAccountDeadlineView, \ from compensation.views.eco_account.deadline import NewEcoAccountDeadlineView, EditEcoAccountDeadlineView, \
RemoveEcoAccountDeadlineView RemoveEcoAccountDeadlineView
@ -36,9 +37,9 @@ urlpatterns = [
path('<id>/remove', remove_view, name='remove'), path('<id>/remove', remove_view, name='remove'),
path('<id>/resub', EcoAccountResubmissionView.as_view(), name='resubmission-create'), path('<id>/resub', EcoAccountResubmissionView.as_view(), name='resubmission-create'),
path('<id>/state/new', state_new_view, name='new-state'), path('<id>/state/new', NewEcoAccountStateView.as_view(), name='new-state'),
path('<id>/state/<state_id>/edit', state_edit_view, name='state-edit'), path('<id>/state/<state_id>/edit', EditEcoAccountStateView.as_view(), name='state-edit'),
path('<id>/state/<state_id>/remove', state_remove_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', action_new_view, name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', action_edit_view, name='action-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.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \ from compensation.models import Compensation
NewCompensationStateModalForm
from compensation.models import Compensation, CompensationState
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_STATE_EDITED, COMPENSATION_STATE_REMOVED, \ from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \
COMPENSATION_STATE_ADDED AbstractRemoveCompensationStateView
@login_required class NewCompensationStateView(AbstractNewCompensationStateView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
def state_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new states 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 = 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"
)
@login_required class EditCompensationStateView(AbstractEditCompensationStateView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
def state_remove_view(request: HttpRequest, id: str, state_id: str):
""" Renders a form for removing a compensation state
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"))
state_id (str): The state's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
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"
)
@login_required class RemoveCompensationStateView(AbstractRemoveCompensationStateView):
@default_group_required model = Compensation
@shared_access_required(Compensation, "id") redirect_url = "compensation:detail"
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"
)
@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,83 +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.state import EditCompensationStateModalForm, RemoveCompensationStateModalForm, \ from compensation.models import EcoAccount
NewCompensationStateModalForm
from compensation.models import EcoAccount, CompensationState
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_STATE_EDITED, COMPENSATION_STATE_REMOVED, \ from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \
COMPENSATION_STATE_ADDED AbstractRemoveCompensationStateView
@login_required class NewEcoAccountStateView(AbstractNewCompensationStateView):
@default_group_required model = EcoAccount
@shared_access_required(EcoAccount, "id") redirect_url = "compensation:acc:detail"
def state_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new states 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 = 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"
)
@login_required class EditEcoAccountStateView(AbstractEditCompensationStateView):
@default_group_required model = EcoAccount
@shared_access_required(EcoAccount, "id") redirect_url = "compensation:acc:detail"
def state_edit_view(request: HttpRequest, id: str, state_id: str):
""" Renders a form for editing a compensation state
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"))
state_id (str): The state's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
Returns:
""" class RemoveEcoAccountStateView(AbstractRemoveCompensationStateView):
acc = get_object_or_404(EcoAccount, id=id) model = EcoAccount
state = get_object_or_404(CompensationState, id=state_id) redirect_url = "compensation:acc:detail"
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"
)
@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

@ -16,7 +16,7 @@ from ema.views.record import EmaRecordView
from ema.views.report import report_view from ema.views.report import report_view
from ema.views.resubmission import EmaResubmissionView from ema.views.resubmission import EmaResubmissionView
from ema.views.share import share_view, create_share_view 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" app_name = "ema"
urlpatterns = [ urlpatterns = [
@ -31,9 +31,9 @@ urlpatterns = [
path('<id>/report', report_view, name='report'), path('<id>/report', report_view, name='report'),
path('<id>/resub', EmaResubmissionView.as_view(), name='resubmission-create'), path('<id>/resub', EmaResubmissionView.as_view(), name='resubmission-create'),
path('<id>/state/new', state_new_view, name='new-state'), path('<id>/state/new', NewEmaStateView.as_view(), name='new-state'),
path('<id>/state/<state_id>/remove', state_remove_view, name='state-remove'), path('<id>/state/<state_id>/remove', RemoveEmaStateView.as_view(), name='state-remove'),
path('<id>/state/<state_id>/edit', state_edit_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', action_new_view, name='new-action'),
path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'), path('<id>/action/<action_id>/edit', action_edit_view, name='action-edit'),

View File

@ -6,85 +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.state import NewCompensationStateModalForm, RemoveCompensationStateModalForm, \
EditCompensationStateModalForm
from compensation.models import CompensationState
from ema.models import Ema from ema.models import Ema
from konova.decorators import conservation_office_group_required, shared_access_required from konova.decorators import conservation_office_group_required, shared_access_required
from konova.utils.message_templates import COMPENSATION_STATE_ADDED, COMPENSATION_STATE_REMOVED, \ from konova.views.state import AbstractNewCompensationStateView, AbstractEditCompensationStateView, \
COMPENSATION_STATE_EDITED AbstractRemoveCompensationStateView
@login_required class NewEmaStateView(AbstractNewCompensationStateView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
def state_new_view(request: HttpRequest, id: str):
""" Renders a form for adding new states 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 = 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"
)
@login_required class EditEmaStateView(AbstractEditCompensationStateView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
def state_remove_view(request: HttpRequest, id: str, state_id: str):
""" Renders a form for removing an EMA state
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"))
state_id (str): The state's id def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
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"
)
@login_required class RemoveEmaStateView(AbstractRemoveCompensationStateView):
@conservation_office_group_required model = Ema
@shared_access_required(Ema, "id") redirect_url = "ema:detail"
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"
)
@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)

107
konova/views/state.py Normal file
View File

@ -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)