Eco Accounts

* adds related eco account withdraw detail view to intervention detail view
* adds new route for removing withdraws using RemoveModalForm
* adds EcoAccountWithdraw model
* adds admin interfaces for EcoAccount and EcoAccountWithdraw
* adds message_templates.py to konova/utils for reusable messages
* splits related-documents.html and related-objects.html into separate templates for each related object type: compensations.html, documents.html, eco-account-withdraws.html and payments.html
* adds translations
This commit is contained in:
mipel
2021-08-02 10:14:34 +02:00
parent 221f7dfb79
commit 98de05089e
16 changed files with 412 additions and 209 deletions

View File

@@ -1,6 +1,7 @@
from django.contrib import admin
from compensation.models import Compensation, CompensationAction, CompensationState, CompensationControl, Payment
from compensation.models import Compensation, CompensationAction, CompensationState, CompensationControl, Payment, \
EcoAccountWithdraw, EcoAccount
class CompensationControlAdmin(admin.ModelAdmin):
@@ -34,9 +35,23 @@ class CompensationActionAdmin(admin.ModelAdmin):
class CompensationAdmin(admin.ModelAdmin):
list_display = [
"id",
"identifier",
"title",
"created_on",
"created_by",
]
class EcoAccountAdmin(admin.ModelAdmin):
list_display = [
"id",
"identifier",
"title",
"created_on",
"created_by",
]
class PaymentAdmin(admin.ModelAdmin):
list_display = [
"id",
@@ -47,8 +62,21 @@ class PaymentAdmin(admin.ModelAdmin):
]
class EcoAccountWithdrawAdmin(admin.ModelAdmin):
list_display = [
"id",
"account",
"intervention",
"amount",
"created_by",
"created_on",
]
admin.site.register(Compensation, CompensationAdmin)
admin.site.register(Payment, PaymentAdmin)
admin.site.register(CompensationAction, CompensationActionAdmin)
admin.site.register(CompensationState, CompensationStateAdmin)
admin.site.register(CompensationControl, CompensationControlAdmin)
admin.site.register(EcoAccount, EcoAccountAdmin)
admin.site.register(EcoAccountWithdraw, EcoAccountWithdrawAdmin)

View File

@@ -7,7 +7,7 @@ Created on: 17.11.20
"""
from django.contrib.auth.models import User
from django.contrib.gis.db import models
from django.core.validators import MinValueValidator
from django.core.validators import MinValueValidator, MaxValueValidator
from django.utils import timezone
from django.utils.timezone import now
@@ -152,3 +152,40 @@ class EcoAccount(Compensation):
"""
# Users having access on this object
users = models.ManyToManyField(User)
def __str__(self):
return "{}".format(self.identifier)
class EcoAccountWithdraw(BaseResource):
"""
A withdraw object for eco accounts
"""
account = models.ForeignKey(
EcoAccount,
on_delete=models.SET_NULL,
null=True,
blank=True,
help_text="Withdrawn from",
related_name="eco_withdraws",
)
amount = models.FloatField(
null=True,
blank=True,
help_text="Amount withdrawn (percentage)",
validators=[
MinValueValidator(limit_value=0.00),
MaxValueValidator(limit_value=100),
]
)
intervention = models.ForeignKey(
Intervention,
on_delete=models.CASCADE,
null=True,
blank=True,
help_text="Withdrawn for",
related_name="eco_withdraws",
)
def __str__(self):
return "{} of {}".format(self.amount, self.account)

View File

@@ -30,4 +30,8 @@ urlpatterns = [
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'),
# Eco-account withdraws
path('acc/<id>/remove/<withdraw_id>', withdraw_remove_view, name='withdraw-remove'),
]

View File

@@ -1,5 +1,6 @@
from django.contrib.auth.decorators import login_required
from django.http import HttpRequest
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 _
@@ -10,6 +11,7 @@ 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
@@ -139,7 +141,7 @@ def new_payment_view(request: HttpRequest, intervention_id: str):
else:
messages.info(
request,
_("There was an error on this form.")
FORM_INVALID
)
return redirect(request.META.get("HTTP_REFERER", "home"))
elif request.method == "GET":
@@ -154,7 +156,7 @@ def new_payment_view(request: HttpRequest, intervention_id: str):
@login_required
def payment_remove_view(request: HttpRequest, id: str):
""" Renders a modal view for adding new payments
""" Renders a modal view for removing payments
Args:
request (HttpRequest): The incoming request
@@ -177,7 +179,51 @@ def payment_remove_view(request: HttpRequest, id: str):
else:
messages.info(
request,
_("There was an error on this form.")
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 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)
template = form.template
if request.method == "POST":
if form.is_valid():
form.save()
messages.success(
request,
_("Withdraw removed")
)
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":