EcoAccount index

* renames eco_withdraws to withdraws
* adds EcoAccountTableFilter
* changes percentage withdraw to surface withdraw --> renames amount to surface
* updates EcoAccountTable
  * adds column for rendering rest of available account using a progress bar
* adds progressbar.html to konova/custom_widgets
* adds/updates translations
This commit is contained in:
mipel
2021-08-09 14:16:54 +02:00
parent c016ab2e76
commit 3dc2af4b7c
9 changed files with 279 additions and 112 deletions
+109 -30
View File
@@ -5,14 +5,16 @@ Contact: michel.peltriaux@sgdnord.rlp.de
Created on: 01.12.20
"""
from django.db.models import Sum
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.html import format_html
from django.utils.timezone import localtime
from django.utils.translation import gettext_lazy as _
from compensation.filters import CompensationTableFilter
from compensation.models import Compensation
from compensation.filters import CompensationTableFilter, EcoAccountTableFilter
from compensation.models import Compensation, EcoAccount
from intervention.filters import InterventionTableFilter
from konova.sub_settings.django_settings import DEFAULT_DATE_TIME_FORMAT
from konova.utils.tables import BaseTable
@@ -57,7 +59,7 @@ class CompensationTable(BaseTable):
class Meta(BaseTable.Meta):
template_name = "django_tables2/bootstrap4.html"
def __init__(self, request:HttpRequest, *args, **kwargs):
def __init__(self, request: HttpRequest, *args, **kwargs):
self.title = _("Compensations")
self.add_new_url = reverse("compensation:new")
qs = kwargs.get("queryset", None)
@@ -170,43 +172,120 @@ class EcoAccountTable(BaseTable):
orderable=True,
accessor="title",
)
d = tables.Column(
verbose_name=_("Created on"),
av = tables.Column(
verbose_name=_("Available"),
orderable=True,
empty_values=[],
)
r = tables.Column(
verbose_name=_("Recorded"),
orderable=True,
empty_values=[],
accessor="recorded",
)
e = tables.Column(
verbose_name=_("Editable"),
orderable=True,
empty_values=[],
accessor="users",
)
lm = tables.Column(
verbose_name=_("Last edit"),
orderable=True,
accessor="created__timestamp",
)
ac = tables.Column(
verbose_name=_("Actions"),
orderable=False,
empty_values=[],
attrs={"td": {"class": "action-col"}}
)
class Meta(BaseTable.Meta):
pass
template_name = "django_tables2/bootstrap4.html"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, request: HttpRequest, *args, **kwargs):
self.title = _("Eco Accounts")
self.add_new_url = reverse("compensation:acc-new")
qs = kwargs.get("queryset", None)
self.filter = EcoAccountTableFilter(
user=request.user,
data=request.GET,
queryset=qs,
)
super().__init__(request, self.filter, *args, **kwargs)
def render_id(self, value, record: EcoAccount):
""" Renders the id column for an eco account
Args:
value (str): The identifier value
record (EcoAccount): The eco account record
Returns:
def render_ac(self, value, record):
"""
Renders possible actions for this record, such as delete.
"""
intervention = _("Compensation")
html = ""
html += self.render_open_btn(
_("Open {}").format(intervention),
reverse("compensation:open", args=(record.id,)),
new_tab=True
)
html += self.render_edit_btn(
_("Edit {}").format(intervention),
reverse("compensation:edit", args=(record.id,)),
)
html += self.render_delete_btn(
_("Delete {}").format(intervention),
reverse("compensation:remove", args=(record.id,)),
html += self.render_link(
tooltip=_("Open {}").format(_("Eco-account")),
href=reverse("compensation:acc-open", args=(record.id,)),
txt=value,
new_tab=False,
)
return format_html(html)
def render_av(self, value, record: EcoAccount):
""" Renders the available column for an eco account
Args:
value (str): The identifier value
record (EcoAccount): The eco account record
Returns:
"""
withdraws = record.withdraws.all()
withdraw_surfaces = withdraws.aggregate(Sum("amount"))["amount__sum"] or 0
after_states_surfaces = record.after_states.all().aggregate(Sum("surface"))["surface__sum"] or withdraw_surfaces ## no division by zero
value = int(((after_states_surfaces - withdraw_surfaces) / after_states_surfaces) * 100)
html = render_to_string("konova/custom_widgets/progressbar.html", {"value": value})
return format_html(html)
def render_r(self, value, record: EcoAccount):
""" Renders the registered column for an eco account
Args:
value (str): The identifier value
record (EcoAccount): The eco account record
Returns:
"""
html = ""
checked = value is not None
tooltip = _("Not recorded yet. Can not be used for withdraws, yet.")
if checked:
value = value.timestamp
value = localtime(value)
on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
tooltip = _("Recorded on {} by {}").format(on, record.recorded.user)
html += self.render_bookmark(
tooltip=tooltip,
icn_filled=checked,
)
return format_html(html)
def render_e(self, value, record: EcoAccount):
""" Renders the registered column for an eco account
Args:
value (str): The identifier value
record (EcoAccount): The eco account record
Returns:
"""
html = ""
has_access = value.filter(
username=self.user.username
).exists()
html += self.render_icn(
tooltip=_("Full access granted") if has_access else _("Access not granted"),
icn_class="fas fa-edit rlp-r-inv" if has_access else "far fa-edit",
)
return format_html(html)