63b2d3ef66
* compensations won't be listed in index table if related intervention has been deleted * adds functionality for intervention remove button * adds outcommented code * adds configurable redirect_url for RemoveModalForm's process_request method * adds translation
212 lines
5.3 KiB
Python
212 lines
5.3 KiB
Python
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 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
|
|
from konova.utils.message_templates import FORM_INVALID
|
|
|
|
|
|
@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"
|
|
compensations = Compensation.objects.filter(
|
|
deleted=None, # only show those which are not deleted individually
|
|
intervention__deleted=None, # and don't show the ones whose intervention has been deleted
|
|
)
|
|
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):
|
|
""" Renders a modal view for removing the compensation
|
|
|
|
Args:
|
|
request (HttpRequest): The incoming request
|
|
id (str): The compensation's id
|
|
|
|
Returns:
|
|
|
|
"""
|
|
comp = get_object_or_404(Compensation, id=id)
|
|
form = RemoveModalForm(request.POST or None, instance=comp, user=request.user)
|
|
return form.process_request(
|
|
request=request,
|
|
msg_success=_("Compensation removed"),
|
|
)
|
|
|
|
|
|
@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=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:
|
|
|
|
"""
|
|
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
|
|
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
|
|
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")
|
|
)
|