From a7aeecde2e49ff75517710012d0ec510f03f6195 Mon Sep 17 00:00:00 2001 From: mipel Date: Mon, 9 Aug 2021 13:16:50 +0200 Subject: [PATCH] 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 --- compensation/urls.py | 68 ++++---- .../{views.py => views/compensation_views.py} | 153 +----------------- compensation/views/eco_account_views.py | 102 ++++++++++++ compensation/views/payment_views.py | 80 +++++++++ 4 files changed, 224 insertions(+), 179 deletions(-) rename compensation/{views.py => views/compensation_views.py} (64%) create mode 100644 compensation/views/eco_account_views.py create mode 100644 compensation/views/payment_views.py diff --git a/compensation/urls.py b/compensation/urls.py index 0f3dbe36..0014e933 100644 --- a/compensation/urls.py +++ b/compensation/urls.py @@ -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('', open_view, name='open'), - path('/log', log_view, name='log'), - path('/edit', edit_view, name='edit'), - path('/remove', remove_view, name='remove'), - path('/state/new', state_new_view, name='new-state'), - path('/action/new', action_new_view, name='new-action'), - path('/deadline/new', deadline_new_view, name="new-deadline"), - # Documents - path('/document/new/', new_document_view, name='new-doc'), +# Split lists for each sub-component for better overview +urlpatterns_payment = [ + path('pay//new', payment_views.new_payment_view, name='pay-new'), + path('pay//remove', payment_views.payment_remove_view, name='pay-remove'), +] - # Payment - path('pay//new', new_payment_view, name='pay-new'), - path('pay/', open_view, name='pay-open'), - path('pay//edit', edit_view, name='pay-edit'), - path('pay//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/', account_open_view, name='acc-open'), - path('acc//edit', account_edit_view, name='acc-edit'), - path('acc//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/', eco_account_views.account_open_view, name='acc-open'), + path('acc//edit', eco_account_views.account_edit_view, name='acc-edit'), + path('acc//remove', eco_account_views.account_remove_view, name='acc-remove'), # Eco-account withdraws - path('acc//remove/', withdraw_remove_view, name='withdraw-remove'), + path('acc//remove/', 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('', compensation_views.open_view, name='open'), + path('/log', compensation_views.log_view, name='log'), + path('/edit', compensation_views.edit_view, name='edit'), + path('/remove', compensation_views.remove_view, name='remove'), + path('/state/new', compensation_views.state_new_view, name='new-state'), + path('/action/new', compensation_views.action_new_view, name='new-action'), + path('/deadline/new', compensation_views.deadline_new_view, name="new-deadline"), + + # Documents + path('/document/new/', compensation_views.new_document_view, name='new-doc'), # Generic state routes - path('state//remove', state_remove_view, name='state-remove'), + path('state//remove', compensation_views.state_remove_view, name='state-remove'), # Generic action routes - path('action//remove', action_remove_view, name='action-remove'), -] \ No newline at end of file + path('action//remove', compensation_views.action_remove_view, name='action-remove'), + +] + +# Merge all together in the end +urlpatterns = urlpatterns_compensation + urlaptterns_eco_acc + urlpatterns_payment diff --git a/compensation/views.py b/compensation/views/compensation_views.py similarity index 64% rename from compensation/views.py rename to compensation/views/compensation_views.py index 2cd1a16d..a062af8a 100644 --- a/compensation/views.py +++ b/compensation/views/compensation_views.py @@ -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 diff --git a/compensation/views/eco_account_views.py b/compensation/views/eco_account_views.py new file mode 100644 index 00000000..3c63b867 --- /dev/null +++ b/compensation/views/eco_account_views.py @@ -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") + ) + diff --git a/compensation/views/payment_views.py b/compensation/views/payment_views.py new file mode 100644 index 00000000..c71adcb3 --- /dev/null +++ b/compensation/views/payment_views.py @@ -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"), + ) +