2024-02-07 01:26:49 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
2020-02-18 20:30:26 +00:00
|
|
|
# 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
|
2021-10-07 20:54:01 +00:00
|
|
|
from django.utils.html import format_html, mark_safe
|
2020-02-28 14:25:45 +00:00
|
|
|
from django_tables2.utils import A
|
2020-03-16 11:11:16 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-06-15 12:40:32 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
2020-07-30 15:52:04 +00:00
|
|
|
from permission.backends import PermissionBackend
|
2020-03-07 21:28:59 +00:00
|
|
|
|
2021-09-05 19:55:22 +00:00
|
|
|
from .models.notes import Alias, Trust
|
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 = {
|
2020-08-10 16:01:39 +00:00
|
|
|
'class': 'table table-condensed table-striped'
|
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",)
|
2020-02-21 11:29:11 +00:00
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
2020-03-25 12:13:01 +00:00
|
|
|
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": {
|
2020-09-02 21:49:10 +00:00
|
|
|
"class": "text-nowrap",
|
2020-03-26 13:45:48 +00:00
|
|
|
"data-toggle": "tooltip",
|
|
|
|
"title": lambda record: _("used alias").capitalize() + " : " + record.source_alias,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
destination = tables.Column(
|
|
|
|
attrs={
|
|
|
|
"td": {
|
2020-09-02 21:49:10 +00:00
|
|
|
"class": "text-nowrap",
|
2020-03-26 13:45:48 +00:00
|
|
|
"data-toggle": "tooltip",
|
|
|
|
"title": lambda record: _("used alias").capitalize() + " : " + record.destination_alias,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-09-05 12:29:40 +00:00
|
|
|
created_at = tables.DateTimeColumn(
|
|
|
|
format='Y-m-d H:i:s',
|
2020-09-02 21:49:10 +00:00
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"class": "text-nowrap",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
amount = tables.Column(
|
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"class": "text-nowrap",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
reason = tables.Column(
|
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"class": "text-break",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-03-16 11:11:16 +00:00
|
|
|
type = tables.Column()
|
2019-08-15 21:08:15 +00:00
|
|
|
|
2020-09-02 21:49:10 +00:00
|
|
|
total = tables.Column( # will use Transaction.total() !!
|
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"class": "text-nowrap",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
2019-08-15 21:08:15 +00:00
|
|
|
|
2020-03-25 12:13:01 +00:00
|
|
|
valid = tables.Column(
|
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"id": lambda record: "validate_" + str(record.id),
|
2020-07-30 15:52:04 +00:00
|
|
|
"class": lambda record:
|
|
|
|
str(record.valid).lower()
|
2020-08-31 18:15:48 +00:00
|
|
|
+ (' validate' if record.source.is_active and record.destination.is_active and PermissionBackend
|
2021-06-15 12:40:32 +00:00
|
|
|
.check_perm(get_current_request(), "note.change_transaction_invalidity_reason", record)
|
2020-08-31 18:15:48 +00:00
|
|
|
else ''),
|
2020-03-25 13:50:21 +00:00
|
|
|
"data-toggle": "tooltip",
|
2020-07-30 15:52:04 +00:00
|
|
|
"title": lambda record: (_("Click to invalidate") if record.valid else _("Click to validate"))
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(),
|
2020-08-31 18:15:48 +00:00
|
|
|
"note.change_transaction_invalidity_reason", record)
|
|
|
|
and record.source.is_active and record.destination.is_active else None,
|
2020-08-15 17:10:15 +00:00
|
|
|
"onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower()
|
|
|
|
+ ', "' + str(record.__class__.__name__) + '")'
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(),
|
2020-08-31 18:15:48 +00:00
|
|
|
"note.change_transaction_invalidity_reason", record)
|
|
|
|
and record.source.is_active and record.destination.is_active else None,
|
2020-03-25 13:50:21 +00:00
|
|
|
"onmouseover": lambda record: '$("#invalidity_reason_'
|
|
|
|
+ str(record.id) + '").show();$("#invalidity_reason_'
|
2020-08-03 09:41:06 +00:00
|
|
|
+ str(record.id) + '").focus();',
|
|
|
|
"onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()',
|
2020-03-25 12:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2020-03-14 01:08:23 +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)
|
|
|
|
|
2020-03-25 12:13:01 +00:00
|
|
|
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
|
|
|
|
"""
|
2020-08-30 21:06:51 +00:00
|
|
|
has_perm = PermissionBackend \
|
2021-06-15 12:40:32 +00:00
|
|
|
.check_perm(get_current_request(), "note.change_transaction_invalidity_reason", record)
|
2020-08-03 09:41:06 +00:00
|
|
|
|
2020-03-25 12:13:01 +00:00
|
|
|
val = "✔" if value else "✖"
|
2020-08-03 09:41:06 +00:00
|
|
|
|
|
|
|
if value and not has_perm:
|
2020-07-30 15:52:04 +00:00
|
|
|
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")))) \
|
2020-08-31 18:15:48 +00:00
|
|
|
+ "'" + ("" if value and record.source.is_active and record.destination.is_active 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;'>"
|
2020-03-25 12:13:01 +00:00
|
|
|
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
|
|
|
|
2021-09-05 19:55:22 +00:00
|
|
|
class TrustTable(tables.Table):
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
|
|
|
'class': 'table table condensed table-striped',
|
|
|
|
'id': "trust_table"
|
|
|
|
}
|
|
|
|
model = Trust
|
|
|
|
fields = ("trusted",)
|
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
|
|
|
|
|
|
|
show_header = False
|
2022-04-14 17:07:33 +00:00
|
|
|
trusted = tables.Column(attrs={'td': {'class': 'text-center'}})
|
2021-09-05 19:55:22 +00:00
|
|
|
|
2021-10-04 18:45:05 +00:00
|
|
|
delete_col = tables.TemplateColumn(
|
|
|
|
template_code=DELETE_TEMPLATE,
|
2022-10-07 07:13:35 +00:00
|
|
|
extra_context={"delete_trans": _('Delete')},
|
2021-10-04 18:45:05 +00:00
|
|
|
attrs={
|
|
|
|
'td': {
|
|
|
|
'class': lambda record: 'col-sm-1'
|
|
|
|
+ (' d-none' if not PermissionBackend.check_perm(
|
|
|
|
get_current_request(), "note.delete_trust", record)
|
|
|
|
else '')}},
|
|
|
|
verbose_name=_("Delete"),)
|
2021-09-05 19:55:22 +00:00
|
|
|
|
2022-09-28 10:50:11 +00:00
|
|
|
|
2022-04-14 17:07:33 +00:00
|
|
|
class TrustedTable(tables.Table):
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
|
|
|
'class': 'table table condensed table-striped',
|
|
|
|
'id': 'trusted_table'
|
|
|
|
}
|
|
|
|
Model = Trust
|
|
|
|
fields = ("trusting",)
|
|
|
|
template_name = "django_tables2/bootstrap4.html"
|
|
|
|
|
|
|
|
show_header = False
|
2022-09-28 08:28:47 +00:00
|
|
|
trusting = tables.Column(attrs={
|
2022-09-28 10:50:11 +00:00
|
|
|
'td': {'class': 'text-center', 'width': '100%'}})
|
2022-04-14 17:07:33 +00:00
|
|
|
|
2022-09-21 09:53:31 +00:00
|
|
|
trust_back = tables.Column(
|
|
|
|
verbose_name=_("Trust back"),
|
|
|
|
accessor="pk",
|
|
|
|
attrs={
|
|
|
|
'td': {
|
2022-09-28 08:28:47 +00:00
|
|
|
'class': '',
|
2022-09-21 09:53:31 +00:00
|
|
|
'id': lambda record: "trust_back_" + str(record.pk),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
def render_trust_back(self, record):
|
|
|
|
user_note = record.trusted
|
|
|
|
trusting_note = record.trusting
|
2022-09-28 10:50:11 +00:00
|
|
|
if Trust.objects.filter(trusted=trusting_note, trusting=user_note):
|
2022-09-21 09:53:31 +00:00
|
|
|
return ""
|
|
|
|
val = '<button id="'
|
|
|
|
val += str(record.pk)
|
2022-09-28 08:28:47 +00:00
|
|
|
val += '" class="btn btn-success btn-sm text-nowrap" \
|
2022-09-21 09:53:31 +00:00
|
|
|
onclick="create_trust(' + str(record.trusted.pk) + ',' + \
|
|
|
|
str(record.trusting.pk) + ')">'
|
2022-09-28 08:28:47 +00:00
|
|
|
val += str(_("Add back"))
|
2022-09-21 09:53:31 +00:00
|
|
|
val += '</button>'
|
|
|
|
return mark_safe(val)
|
|
|
|
|
2021-09-05 19:55:22 +00:00
|
|
|
|
2020-02-28 12:37:31 +00:00
|
|
|
class AliasTable(tables.Table):
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
2020-08-10 16:01:39 +00:00
|
|
|
'class': 'table table condensed table-striped',
|
2020-03-27 13:19:55 +00:00
|
|
|
'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-08-31 18:15:48 +00:00
|
|
|
attrs={'td': {'class': lambda record: 'col-sm-1' + (
|
|
|
|
' d-none' if not PermissionBackend.check_perm(
|
2021-06-15 12:40:32 +00:00
|
|
|
get_current_request(), "note.delete_alias",
|
2020-08-31 18:15:48 +00:00
|
|
|
record) else '')}}, verbose_name=_("Delete"), )
|
2020-03-24 22:14:13 +00:00
|
|
|
|
2020-03-24 23:12:56 +00:00
|
|
|
|
2020-03-09 19:59:04 +00:00
|
|
|
class ButtonTable(tables.Table):
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
2020-08-10 16:01:39 +00:00
|
|
|
'class': 'table table-bordered condensed'
|
2020-03-09 19:59:04 +00:00
|
|
|
}
|
2020-03-23 19:21:25 +00:00
|
|
|
row_attrs = {
|
2020-04-27 01:56:22 +00:00
|
|
|
'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
|
|
|
}
|
|
|
|
|
2020-03-09 19:59:04 +00:00
|
|
|
model = TransactionTemplate
|
2020-04-01 18:56:24 +00:00
|
|
|
exclude = ('id',)
|
2020-03-09 19:59:04 +00:00
|
|
|
|
2020-08-09 17:42:09 +00:00
|
|
|
edit = tables.LinkColumn(
|
|
|
|
'note:template_update',
|
|
|
|
args=[A('pk')],
|
|
|
|
attrs={
|
|
|
|
'td': {'class': 'col-sm-1'},
|
|
|
|
'a': {
|
|
|
|
'class': 'btn btn-sm btn-primary',
|
|
|
|
'data-turbolinks': 'false',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
text=_('edit'),
|
|
|
|
accessor='pk',
|
|
|
|
verbose_name=_("Edit"),
|
|
|
|
)
|
2020-03-24 23:03:48 +00:00
|
|
|
|
2021-10-07 20:54:01 +00:00
|
|
|
hideshow = tables.Column(
|
2021-12-13 17:33:18 +00:00
|
|
|
verbose_name=_("Hide/Show"),
|
|
|
|
accessor="pk",
|
|
|
|
attrs={
|
|
|
|
'td': {
|
|
|
|
'class': 'col-sm-1',
|
|
|
|
'id': lambda record: "hideshow_" + str(record.pk),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2021-10-07 20:54:01 +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'}},
|
2020-08-30 21:06:51 +00:00
|
|
|
verbose_name=_("Delete"), )
|
2020-03-24 23:03:48 +00:00
|
|
|
|
2020-03-09 19:59:04 +00:00
|
|
|
def render_amount(self, value):
|
|
|
|
return pretty_money(value)
|
2021-10-07 20:54:01 +00:00
|
|
|
|
2021-10-07 11:36:26 +00:00
|
|
|
def order_category(self, queryset, is_descending):
|
|
|
|
return queryset.order_by(f"{'-' if is_descending else ''}category__name"), True
|
|
|
|
|
2021-10-07 20:54:01 +00:00
|
|
|
def render_hideshow(self, record):
|
|
|
|
val = '<button id="'
|
|
|
|
val += str(record.pk)
|
|
|
|
val += '" class="btn btn-secondary btn-sm" \
|
|
|
|
onclick="hideshow(' + str(record.id) + ',' + \
|
|
|
|
str(record.display).lower() + ')">'
|
|
|
|
val += str(_("Hide/Show"))
|
|
|
|
val += '</button>'
|
|
|
|
return mark_safe(val)
|