Compensation index view
* adds filters and table optimizations for compensation index rendering * fixes recorded_by/checked_by confusion in intervention table
This commit is contained in:
parent
c76a061740
commit
287e0b4068
37
compensation/filters.py
Normal file
37
compensation/filters.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""
|
||||
Author: Michel Peltriaux
|
||||
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
||||
Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 29.07.21
|
||||
|
||||
"""
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from intervention.filters import InterventionTableFilter
|
||||
|
||||
|
||||
class CompensationTableFilter(InterventionTableFilter):
|
||||
""" TableFilter for compensations
|
||||
|
||||
Based widely on InterventionTableFilter.
|
||||
Just some minor changes for Compensation model.
|
||||
|
||||
"""
|
||||
|
||||
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(
|
||||
intervention__recorded_on=None,
|
||||
)
|
||||
else:
|
||||
return queryset
|
@ -5,10 +5,16 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 01.12.20
|
||||
|
||||
"""
|
||||
from django.http import HttpRequest
|
||||
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 intervention.filters import InterventionTableFilter
|
||||
from konova.sub_settings.django_settings import DEFAULT_DATE_TIME_FORMAT
|
||||
from konova.utils.tables import BaseTable
|
||||
import django_tables2 as tables
|
||||
|
||||
@ -24,44 +30,129 @@ class CompensationTable(BaseTable):
|
||||
orderable=True,
|
||||
accessor="title",
|
||||
)
|
||||
d = tables.Column(
|
||||
verbose_name=_("Created on"),
|
||||
c = tables.Column(
|
||||
verbose_name=_("Checked"),
|
||||
orderable=True,
|
||||
empty_values=[],
|
||||
accessor="intervention__checked_on",
|
||||
)
|
||||
r = tables.Column(
|
||||
verbose_name=_("Recorded"),
|
||||
orderable=True,
|
||||
empty_values=[],
|
||||
accessor="intervention__recorded_on",
|
||||
)
|
||||
e = tables.Column(
|
||||
verbose_name=_("Editable"),
|
||||
orderable=True,
|
||||
empty_values=[],
|
||||
accessor="intervention__users",
|
||||
)
|
||||
lm = tables.Column(
|
||||
verbose_name=_("Last edit"),
|
||||
orderable=True,
|
||||
accessor="created_on",
|
||||
)
|
||||
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 = _("Compensations")
|
||||
self.add_new_url = reverse("compensation:new")
|
||||
qs = kwargs.get("queryset", None)
|
||||
self.filter = CompensationTableFilter(
|
||||
user=request.user,
|
||||
data=request.GET,
|
||||
queryset=qs,
|
||||
)
|
||||
super().__init__(request, self.filter, *args, **kwargs)
|
||||
|
||||
def render_id(self, value, record: Compensation):
|
||||
""" Renders the id column for a compensation
|
||||
|
||||
Args:
|
||||
value (str): The identifier value
|
||||
record (Compensation): The compensation 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_link(
|
||||
tooltip=_("Open {}").format(_("Compensation")),
|
||||
href=reverse("compensation:open", args=(record.id,)),
|
||||
txt=value,
|
||||
new_tab=False,
|
||||
)
|
||||
html += self.render_edit_btn(
|
||||
_("Edit {}").format(intervention),
|
||||
reverse("compensation:edit", args=(record.id,)),
|
||||
return format_html(html)
|
||||
|
||||
def render_c(self, value, record: Compensation):
|
||||
""" Renders the checked column for a compensation
|
||||
|
||||
checked_on is set by the main object Intervention
|
||||
|
||||
Args:
|
||||
value (str): The identifier value
|
||||
record (Compensation): The compensation record
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
html = ""
|
||||
checked = value is not None
|
||||
tooltip = _("Not checked yet")
|
||||
if checked:
|
||||
value = localtime(value)
|
||||
checked_on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
|
||||
tooltip = _("Checked on {} by {}").format(checked_on, record.intervention.checked_by)
|
||||
html += self.render_checked_star(
|
||||
tooltip=tooltip,
|
||||
icn_filled=checked,
|
||||
)
|
||||
html += self.render_delete_btn(
|
||||
_("Delete {}").format(intervention),
|
||||
reverse("compensation:remove", args=(record.id,)),
|
||||
return format_html(html)
|
||||
|
||||
def render_r(self, value, record: Compensation):
|
||||
""" Renders the registered column for a compensation
|
||||
|
||||
Args:
|
||||
value (str): The identifier value
|
||||
record (Compensation): The compensation record
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
html = ""
|
||||
checked = value is not None
|
||||
tooltip = _("Not registered yet")
|
||||
if checked:
|
||||
value = localtime(value)
|
||||
on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
|
||||
tooltip = _("Registered on {} by {}").format(on, record.intervention.recorded_by)
|
||||
html += self.render_bookmark(
|
||||
tooltip=tooltip,
|
||||
icn_filled=checked,
|
||||
)
|
||||
return format_html(html)
|
||||
|
||||
def render_e(self, value, record: Compensation):
|
||||
""" Renders the registered column for a compensation
|
||||
|
||||
Args:
|
||||
value (str): The identifier value
|
||||
record (Compensation): The compensation 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)
|
||||
|
||||
|
@ -17,6 +17,9 @@ from intervention.models import Intervention
|
||||
|
||||
|
||||
class InterventionTableFilter(django_filters.FilterSet):
|
||||
""" TableFilter for Intervention model
|
||||
|
||||
"""
|
||||
sa = django_filters.BooleanFilter(
|
||||
method='_filter_show_all',
|
||||
label=_("Show unshared"),
|
||||
|
@ -56,6 +56,9 @@ class Intervention(BaseObject):
|
||||
# Users having access on this object
|
||||
users = models.ManyToManyField(User)
|
||||
|
||||
def __str__(self):
|
||||
return "{} ({})".format(self.identifier, self.title)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
""" Custom delete functionality
|
||||
|
||||
|
@ -134,8 +134,8 @@ class InterventionTable(BaseTable):
|
||||
tooltip = _("Not registered yet")
|
||||
if checked:
|
||||
value = localtime(value)
|
||||
checked_on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
|
||||
tooltip = _("Registered on {} by {}").format(checked_on, record.checked_by)
|
||||
on = value.strftime(DEFAULT_DATE_TIME_FORMAT)
|
||||
tooltip = _("Registered on {} by {}").format(on, record.recorded_by)
|
||||
html += self.render_bookmark(
|
||||
tooltip=tooltip,
|
||||
icn_filled=checked,
|
||||
|
Loading…
Reference in New Issue
Block a user