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

View File

@@ -52,7 +52,7 @@ class EcoAccountWithdrawAdmin(admin.ModelAdmin):
"id",
"account",
"intervention",
"amount",
"surface",
]

View File

@@ -53,3 +53,48 @@ class CompensationTableFilter(InterventionTableFilter):
)
else:
return queryset
class EcoAccountTableFilter(InterventionTableFilter):
""" TableFilter for eco accounts
Based widely on InterventionTableFilter.
Just some minor changes for EcoAccount model.
"""
def _filter_show_all(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_all' setting
Args:
queryset ():
name ():
value ():
Returns:
"""
if not value:
return queryset.filter(
users__in=[self.user], # requesting user has access
)
else:
return queryset
def _filter_show_recorded(self, queryset, name, value) -> QuerySet:
""" Filters queryset depending on value of 'show_recorded' setting
Args:
queryset ():
name ():
value ():
Returns:
"""
if not value:
return queryset.filter(
recorded=None,
)
else:
return queryset

View File

@@ -175,6 +175,16 @@ class EcoAccount(AbstractCompensation):
help_text="Users having access (shared with)"
)
# Refers to "verzeichnen"
recorded = models.OneToOneField(
UserActionLogEntry,
on_delete=models.SET_NULL,
null=True,
blank=True,
help_text="Holds data on user and timestamp of this action",
related_name="+"
)
def __str__(self):
return "{}".format(self.identifier)
@@ -189,15 +199,14 @@ class EcoAccountWithdraw(BaseResource):
null=True,
blank=True,
help_text="Withdrawn from",
related_name="eco_withdraws",
related_name="withdraws",
)
amount = models.FloatField(
surface = models.FloatField(
null=True,
blank=True,
help_text="Amount withdrawn (percentage)",
help_text="Amount withdrawn ()",
validators=[
MinValueValidator(limit_value=0.00),
MaxValueValidator(limit_value=100),
]
)
intervention = models.ForeignKey(
@@ -206,8 +215,8 @@ class EcoAccountWithdraw(BaseResource):
null=True,
blank=True,
help_text="Withdrawn for",
related_name="eco_withdraws",
related_name="withdraws",
)
def __str__(self):
return "{} of {}".format(self.amount, self.account)
return "{} of {}".format(self.surface, self.account)

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)

View File

@@ -90,7 +90,7 @@ def withdraw_remove_view(request: HttpRequest, id: str, withdraw_id: str):
"""
acc = get_object_or_404(EcoAccount, id=id)
try:
eco_withdraw = acc.eco_withdraws.get(id=withdraw_id)
eco_withdraw = acc.withdraws.get(id=withdraw_id)
except ObjectDoesNotExist:
raise Http404("Unknown withdraw")