1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-24 00:12:23 +00:00
nk20/apps/note/tables.py

171 lines
6.6 KiB
Python
Raw Normal View History

2020-02-18 20:30:26 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-14 01:08:23 +00:00
import html
2019-08-15 21:08:15 +00:00
import django_tables2 as tables
2020-02-16 21:29:27 +00:00
from django.db.models import F
from django.utils.html import format_html
from django_tables2.utils import A
2020-03-16 11:11:16 +00:00
from django.utils.translation import gettext_lazy as _
from note_kfet.middlewares import get_current_authenticated_user
from permission.backends import PermissionBackend
2020-03-07 21:28:59 +00:00
2020-03-31 12:57:44 +00:00
from .models.notes import Alias
2020-03-23 14:35:24 +00:00
from .models.transactions import Transaction, TransactionTemplate
2020-03-09 17:53:13 +00:00
from .templatetags.pretty_money import pretty_money
2019-08-15 21:08:15 +00:00
2020-03-09 18:19:56 +00:00
2019-08-15 21:08:15 +00:00
class HistoryTable(tables.Table):
class Meta:
2020-02-18 11:31:15 +00:00
attrs = {
'class':
2020-03-07 21:28:59 +00:00
'table table-condensed table-striped table-hover'
2020-02-18 11:31:15 +00:00
}
2019-08-15 21:08:15 +00:00
model = Transaction
2020-03-26 13:45:48 +00:00
exclude = ("id", "polymorphic_ctype", "invalidity_reason", "source_alias", "destination_alias",)
template_name = 'django_tables2/bootstrap4.html'
sequence = ('...', 'type', 'total', 'valid',)
2020-03-14 01:08:23 +00:00
orderable = False
2019-08-15 21:08:15 +00:00
2020-03-26 13:45:48 +00:00
source = tables.Column(
attrs={
"td": {
"data-toggle": "tooltip",
"title": lambda record: _("used alias").capitalize() + " : " + record.source_alias,
}
}
)
destination = tables.Column(
attrs={
"td": {
"data-toggle": "tooltip",
"title": lambda record: _("used alias").capitalize() + " : " + record.destination_alias,
}
}
)
2020-03-16 11:11:16 +00:00
type = tables.Column()
2019-08-15 21:08:15 +00:00
2020-02-18 20:14:29 +00:00
total = tables.Column() # will use Transaction.total() !!
2019-08-15 21:08:15 +00:00
valid = tables.Column(
attrs={
"td": {
"id": lambda record: "validate_" + str(record.id),
"class": lambda record:
str(record.valid).lower()
+ (' validate' if PermissionBackend.check_perm(get_current_authenticated_user(),
"note.change_transaction_invalidity_reason",
record) else ''),
2020-03-25 13:50:21 +00:00
"data-toggle": "tooltip",
"title": lambda record: (_("Click to invalidate") if record.valid else _("Click to validate"))
if PermissionBackend.check_perm(get_current_authenticated_user(),
"note.change_transaction_invalidity_reason", record) else None,
"onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower() + ')'
if PermissionBackend.check_perm(get_current_authenticated_user(),
"note.change_transaction_invalidity_reason", record) else None,
2020-03-25 13:50:21 +00:00
"onmouseover": lambda record: '$("#invalidity_reason_'
+ str(record.id) + '").show();$("#invalidity_reason_'
+ str(record.id) + '").focus();',
"onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()',
}
}
)
2020-03-14 01:08:23 +00:00
2020-02-18 20:14:29 +00:00
def order_total(self, queryset, is_descending):
2019-08-15 21:08:15 +00:00
# needed for rendering
2020-02-18 20:14:29 +00:00
queryset = queryset.annotate(total=F('amount') * F('quantity')) \
.order_by(('-' if is_descending else '') + 'total')
2020-03-14 01:08:23 +00:00
return queryset, True
2020-02-28 12:37:31 +00:00
2020-03-09 18:19:56 +00:00
def render_amount(self, value):
2020-03-09 17:53:13 +00:00
return pretty_money(value)
2020-03-09 18:19:56 +00:00
def render_total(self, value):
2020-03-09 17:53:13 +00:00
return pretty_money(value)
2020-03-07 21:28:59 +00:00
2020-03-16 11:11:16 +00:00
def render_type(self, value):
return _(value)
2020-03-14 01:08:23 +00:00
# Django-tables escape strings. That's a wrong thing.
def render_reason(self, value):
return html.unescape(value)
def render_valid(self, value, record):
2020-03-25 13:54:26 +00:00
"""
When the validation status is hovered, an input field is displayed to let the user specify an invalidity reason
"""
has_perm = PermissionBackend\
.check_perm(get_current_authenticated_user(), "note.change_transaction_invalidity_reason", record)
val = "" if value else ""
if value and not has_perm:
return val
2020-03-25 13:50:21 +00:00
val += "<input type='text' class='form-control' id='invalidity_reason_" + str(record.id) \
+ "' value='" + (html.escape(record.invalidity_reason)
if record.invalidity_reason else ("" if value else str(_("No reason specified")))) \
+ "'" + ("" if value and has_perm else " disabled") \
2020-03-25 13:50:21 +00:00
+ " placeholder='" + html.escape(_("invalidity reason").capitalize()) + "'" \
+ " style='position: absolute; width: 15em; margin-left: -15.5em; margin-top: -2em; display: none;'>"
return format_html(val)
2020-03-14 01:08:23 +00:00
2020-03-24 23:03:48 +00:00
2020-03-24 22:02:12 +00:00
# function delete_button(id) provided in template file
2020-03-24 23:03:48 +00:00
DELETE_TEMPLATE = """
2020-03-26 16:46:38 +00:00
<button id="{{ record.pk }}" class="btn btn-danger btn-sm" onclick="delete_button(this.id)"> {{ delete_trans }}</button>
2020-03-24 22:02:12 +00:00
"""
2020-03-24 23:03:48 +00:00
2020-03-24 23:12:56 +00:00
2020-02-28 12:37:31 +00:00
class AliasTable(tables.Table):
class Meta:
attrs = {
2020-03-27 13:19:55 +00:00
'class': 'table table condensed table-striped table-hover',
'id': "alias_table"
2020-02-28 12:37:31 +00:00
}
model = Alias
2020-03-07 21:28:59 +00:00
fields = ('name',)
2020-02-28 12:37:31 +00:00
template_name = 'django_tables2/bootstrap4.html'
2020-03-01 12:42:22 +00:00
show_header = False
2020-03-07 21:28:59 +00:00
name = tables.Column(attrs={'td': {'class': 'text-center'}})
2020-03-26 16:47:17 +00:00
2020-03-26 22:04:14 +00:00
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
2020-03-27 13:19:55 +00:00
extra_context={"delete_trans": _('delete')},
2020-04-01 18:56:24 +00:00
attrs={'td': {'class': 'col-sm-1'}},
verbose_name=_("Delete"),)
2020-03-24 23:12:56 +00:00
class ButtonTable(tables.Table):
class Meta:
attrs = {
'class':
2020-03-24 22:32:48 +00:00
'table table-bordered condensed table-hover'
}
2020-03-23 19:21:25 +00:00
row_attrs = {
'class': lambda record: 'table-row ' + ('table-success' if record.display else 'table-danger'),
2020-03-24 23:12:56 +00:00
'id': lambda record: "row-" + str(record.pk),
2020-03-23 19:21:25 +00:00
}
model = TransactionTemplate
2020-04-01 18:56:24 +00:00
exclude = ('id',)
2020-03-24 23:03:48 +00:00
edit = tables.LinkColumn('note:template_update',
args=[A('pk')],
attrs={'td': {'class': 'col-sm-1'},
2020-03-26 16:46:38 +00:00
'a': {'class': 'btn btn-sm btn-primary'}},
2020-03-24 23:03:48 +00:00
text=_('edit'),
2020-04-01 18:56:24 +00:00
accessor='pk',
verbose_name=_("Edit"),)
2020-03-24 23:03:48 +00:00
2020-03-26 22:04:14 +00:00
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
2020-03-27 13:19:55 +00:00
extra_context={"delete_trans": _('delete')},
2020-04-01 18:56:24 +00:00
attrs={'td': {'class': 'col-sm-1'}},
verbose_name=_("Delete"),)
2020-03-24 23:03:48 +00:00
def render_amount(self, value):
return pretty_money(value)