konova/compensation/views.py
mipel a6c51aede6 Payments add modal form
* adds help texts to add payment form
* adds removing button for payments
* refactors user fetching into BaseForm
* adds generic RemoveModalForm which is intended to be used for every modal form which shall remove something
* adds translations
* removes unused html
* prepares payment amount field to be able to process german inputs like '1.000,50' which is not the international default
2021-07-26 11:29:05 +02:00

191 lines
4.7 KiB
Python

from django.contrib.auth.decorators import login_required
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
from compensation.models import Compensation, EcoAccount, Payment
from compensation.tables import CompensationTable, EcoAccountTable
from intervention.models import Intervention
from konova.contexts import BaseContext
from konova.decorators import *
from konova.forms import RemoveModalForm
@login_required
def index_view(request: HttpRequest):
"""
Renders the index view for compensation
Args:
request (HttpRequest): The incoming request
Returns:
A rendered view
"""
template = "generic_index.html"
user = request.user
compensations = Compensation.objects.filter(
deleted_on=None,
)
table = CompensationTable(
request=request,
queryset=compensations
)
context = {
"table": table,
}
context = BaseContext(request, context).context
return render(request, template, context)
@login_required
def new_view(request: HttpRequest):
# ToDo
pass
@login_required
def edit_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def open_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def remove_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
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_on=None,
)
table = EcoAccountTable(
request=request,
queryset=eco_accounts
)
context = {
"table": table,
}
context = BaseContext(request, context).context
return render(request, template, context)
@login_required
def account_new_view(request: HttpRequest):
# ToDo
pass
@login_required
def account_edit_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def account_open_view(request: HttpRequest, id: str):
# ToDo
pass
@login_required
def account_remove_view(request: HttpRequest, id: str):
# ToDo
pass
@login_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:
"""
template = "modal/modal_form.html"
intervention = get_object_or_404(Intervention, id=intervention_id)
form = NewPaymentForm(request.POST or None, instance=intervention, user=request.user)
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,
_("There was an error on this form.")
)
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
def payment_remove_view(request: HttpRequest, id: str):
""" Renders a modal view for adding new payments
Args:
request (HttpRequest): The incoming request
id (str): The payment's id
Returns:
"""
template = "modal/modal_form.html"
payment = get_object_or_404(Payment, id=id)
form = RemoveModalForm(request.POST or None, instance=payment, user=request.user)
if request.method == "POST":
if form.is_valid():
form.save()
messages.success(
request,
_("Payment removed")
)
return redirect(request.META.get("HTTP_REFERER", "home"))
else:
messages.info(
request,
_("There was an error on this form.")
)
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