Restructure files

* reduce compensation/views.py file size by splitting into three sub view files in compensation/views/xy_views.py for compensation, eco-account and payment
* restructure urlpattern list by splitting into three smaller lists for a better overview and maintainability
This commit is contained in:
mipel 2021-08-09 13:16:50 +02:00
parent 5efa755188
commit a7aeecde2e
4 changed files with 224 additions and 179 deletions

View File

@ -7,43 +7,51 @@ Created on: 30.11.20
"""
from django.urls import path
from compensation.views import *
from compensation.views import compensation_views
from compensation.views import payment_views
from compensation.views import eco_account_views
app_name = "compensation"
urlpatterns = [
# Main compensation
path("", index_view, name="index"),
path('new', new_view, name='new'),
path('<id>', open_view, name='open'),
path('<id>/log', log_view, name='log'),
path('<id>/edit', edit_view, name='edit'),
path('<id>/remove', remove_view, name='remove'),
path('<id>/state/new', state_new_view, name='new-state'),
path('<id>/action/new', action_new_view, name='new-action'),
path('<id>/deadline/new', deadline_new_view, name="new-deadline"),
# Documents
path('<id>/document/new/', new_document_view, name='new-doc'),
# Split lists for each sub-component for better overview
urlpatterns_payment = [
path('pay/<intervention_id>/new', payment_views.new_payment_view, name='pay-new'),
path('pay/<id>/remove', payment_views.payment_remove_view, name='pay-remove'),
]
# Payment
path('pay/<intervention_id>/new', new_payment_view, name='pay-new'),
path('pay/<id>', open_view, name='pay-open'),
path('pay/<id>/edit', edit_view, name='pay-edit'),
path('pay/<id>/remove', payment_remove_view, name='pay-remove'),
# Eco-account
path("acc/", account_index_view, name="acc-index"),
path('acc/new/', account_new_view, name='acc-new'),
path('acc/<id>', account_open_view, name='acc-open'),
path('acc/<id>/edit', account_edit_view, name='acc-edit'),
path('acc/<id>/remove', account_remove_view, name='acc-remove'),
urlaptterns_eco_acc = [
path("acc/", eco_account_views.account_index_view, name="acc-index"),
path('acc/new/', eco_account_views.account_new_view, name='acc-new'),
path('acc/<id>', eco_account_views.account_open_view, name='acc-open'),
path('acc/<id>/edit', eco_account_views.account_edit_view, name='acc-edit'),
path('acc/<id>/remove', eco_account_views.account_remove_view, name='acc-remove'),
# Eco-account withdraws
path('acc/<id>/remove/<withdraw_id>', withdraw_remove_view, name='withdraw-remove'),
path('acc/<id>/remove/<withdraw_id>', eco_account_views.withdraw_remove_view, name='withdraw-remove'),
]
urlpatterns_compensation = [
# Main compensation
path("", compensation_views.index_view, name="index"),
path('new', compensation_views.new_view, name='new'),
path('<id>', compensation_views.open_view, name='open'),
path('<id>/log', compensation_views.log_view, name='log'),
path('<id>/edit', compensation_views.edit_view, name='edit'),
path('<id>/remove', compensation_views.remove_view, name='remove'),
path('<id>/state/new', compensation_views.state_new_view, name='new-state'),
path('<id>/action/new', compensation_views.action_new_view, name='new-action'),
path('<id>/deadline/new', compensation_views.deadline_new_view, name="new-deadline"),
# Documents
path('<id>/document/new/', compensation_views.new_document_view, name='new-doc'),
# Generic state routes
path('state/<id>/remove', state_remove_view, name='state-remove'),
path('state/<id>/remove', compensation_views.state_remove_view, name='state-remove'),
# Generic action routes
path('action/<id>/remove', action_remove_view, name='action-remove'),
]
path('action/<id>/remove', compensation_views.action_remove_view, name='action-remove'),
]
# Merge all together in the end
urlpatterns = urlpatterns_compensation + urlaptterns_eco_acc + urlpatterns_payment

View File

@ -1,18 +1,15 @@
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Sum
from django.http import HttpRequest, Http404
from django.http import HttpRequest
from django.shortcuts import render, get_object_or_404
from django.utils.translation import gettext_lazy as _
from compensation.forms import NewPaymentForm, NewStateModalForm, NewDeadlineModalForm, NewActionModalForm
from compensation.models import Compensation, EcoAccount, Payment, CompensationState, CompensationAction
from compensation.tables import CompensationTable, EcoAccountTable
from intervention.models import Intervention
from compensation.forms import NewStateModalForm, NewDeadlineModalForm, NewActionModalForm
from compensation.models import Compensation, CompensationState, CompensationAction
from compensation.tables import CompensationTable
from konova.contexts import BaseContext
from konova.decorators import *
from konova.forms import RemoveModalForm, SimpleGeomForm, NewDocumentForm
from konova.utils.message_templates import FORM_INVALID
from konova.utils.user_checks import in_group
@ -145,148 +142,6 @@ def remove_view(request: HttpRequest, id: str):
msg_success=_("Compensation removed"),
redirect_url="",
)
@login_required
@any_group_check
def account_index_view(request: HttpRequest):
"""
Renders the index view for eco accounts
Args:
request (HttpRequest): The incoming request
Returns:
A rendered view
"""
template = "generic_index.html"
user = request.user
eco_accounts = EcoAccount.objects.filter(
deleted=None,
)
table = EcoAccountTable(
request=request,
queryset=eco_accounts
)
context = {
"table": table,
}
context = BaseContext(request, context).context
return render(request, template, context)
@login_required
@default_group_required
def account_new_view(request: HttpRequest):
# ToDo
pass
@login_required
@default_group_required
def account_edit_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
@any_group_check
def account_open_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def account_remove_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
@default_group_required
def new_payment_view(request: HttpRequest, intervention_id: str):
""" Renders a modal view for adding new payments
Args:
request (HttpRequest): The incoming request
intervention_id (str): The intervention's id for which a new payment shall be added
Returns:
"""
intervention = get_object_or_404(Intervention, id=intervention_id)
form = NewPaymentForm(request.POST or None, instance=intervention, user=request.user)
template = form.template
if request.method == "POST":
if form.is_valid():
payment = form.save()
messages.success(
request,
_("Payment added")
)
return redirect(request.META.get("HTTP_REFERER", "home"))
else:
messages.info(
request,
FORM_INVALID
)
return redirect(request.META.get("HTTP_REFERER", "home"))
elif request.method == "GET":
context = {
"form": form,
}
context = BaseContext(request, context).context
return render(request, template, context)
else:
raise NotImplementedError
@login_required
@default_group_required
def payment_remove_view(request: HttpRequest, id: str):
""" Renders a modal view for removing payments
Args:
request (HttpRequest): The incoming request
id (str): The payment's id
Returns:
"""
payment = get_object_or_404(Payment, id=id)
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
return form.process_request(
request=request,
msg_success=_("Payment removed"),
)
@login_required
@default_group_required
def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
""" Renders a modal view for removing withdraws
Args:
request (HttpRequest): The incoming request
id (str): The eco account's id
withdraw_id (str): The withdraw's id
Returns:
"""
acc = get_object_or_404(EcoAccount, id=id)
try:
eco_withdraw = acc.eco_withdraws.get(id=withdraw_id)
except ObjectDoesNotExist:
raise Http404("Unknown withdraw")
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
return form.process_request(
request=request,
msg_success=_("Withdraw removed")
)
@login_required
def new_document_view(request: HttpRequest, id: str):
""" Renders a form for uploading new documents

View File

@ -0,0 +1,102 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 09.08.21
"""
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpRequest, Http404
from django.shortcuts import render, get_object_or_404
from compensation.models import EcoAccount
from compensation.tables import EcoAccountTable
from konova.contexts import BaseContext
from konova.decorators import any_group_check, default_group_required
from konova.forms import RemoveModalForm
@login_required
@any_group_check
def account_index_view(request: HttpRequest):
"""
Renders the index view for eco accounts
Args:
request (HttpRequest): The incoming request
Returns:
A rendered view
"""
template = "generic_index.html"
user = request.user
eco_accounts = EcoAccount.objects.filter(
deleted=None,
)
table = EcoAccountTable(
request=request,
queryset=eco_accounts
)
context = {
"table": table,
}
context = BaseContext(request, context).context
return render(request, template, context)
@login_required
@default_group_required
def account_new_view(request: HttpRequest):
# ToDo
pass
@login_required
@default_group_required
def account_edit_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
@any_group_check
def account_open_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def account_remove_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
@default_group_required
def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
""" Renders a modal view for removing withdraws
Args:
request (HttpRequest): The incoming request
id (str): The eco account's id
withdraw_id (str): The withdraw's id
Returns:
"""
acc = get_object_or_404(EcoAccount, id=id)
try:
eco_withdraw = acc.eco_withdraws.get(id=withdraw_id)
except ObjectDoesNotExist:
raise Http404("Unknown withdraw")
form = RemoveModalForm(request.POST or None, instance=eco_withdraw, user=request.user)
return form.process_request(
request=request,
msg_success=_("Withdraw removed")
)

View File

@ -0,0 +1,80 @@
"""
Author: Michel Peltriaux
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 09.08.21
"""
from django.utils.translation import gettext_lazy as _
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
from django.shortcuts import get_object_or_404, render, redirect
from compensation.forms import NewPaymentForm
from compensation.models import Payment
from intervention.models import Intervention
from konova.contexts import BaseContext
from konova.decorators import default_group_required
from konova.forms import RemoveModalForm
from konova.utils.message_templates import FORM_INVALID
@login_required
@default_group_required
def new_payment_view(request: HttpRequest, intervention_id: str):
""" Renders a modal view for adding new payments
Args:
request (HttpRequest): The incoming request
intervention_id (str): The intervention's id for which a new payment shall be added
Returns:
"""
intervention = get_object_or_404(Intervention, id=intervention_id)
form = NewPaymentForm(request.POST or None, instance=intervention, user=request.user)
template = form.template
if request.method == "POST":
if form.is_valid():
payment = form.save()
messages.success(
request,
_("Payment added")
)
return redirect(request.META.get("HTTP_REFERER", "home"))
else:
messages.info(
request,
FORM_INVALID
)
return redirect(request.META.get("HTTP_REFERER", "home"))
elif request.method == "GET":
context = {
"form": form,
}
context = BaseContext(request, context).context
return render(request, template, context)
else:
raise NotImplementedError
@login_required
@default_group_required
def payment_remove_view(request: HttpRequest, id: str):
""" Renders a modal view for removing payments
Args:
request (HttpRequest): The incoming request
id (str): The payment's id
Returns:
"""
payment = get_object_or_404(Payment, id=id)
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
return form.process_request(
request=request,
msg_success=_("Payment removed"),
)