mirror of
https://gitlab.crans.org/bde/nk20
synced 2024-11-27 02:43:01 +00:00
a6b479db19
- /apps/activity/api/serializers.py - /apps/activity/api/urls.py - /apps/activity/api/views.py - /apps/activity/tests/test_activities.py - /apps/activity/__init__.py - /apps/activity/admin.py - /apps/activity/apps.py - /apps/activity/forms.py - /apps/activity/tables.py - /apps/activity/urls.py - /apps/activity/views.py - /apps/api/__init__.py - /apps/api/apps.py - /apps/api/serializers.py - /apps/api/tests.py - /apps/api/urls.py - /apps/api/views.py - /apps/api/viewsets.py - /apps/logs/signals.py - /apps/logs/apps.py - /apps/logs/__init__.py - /apps/logs/api/serializers.py - /apps/logs/api/urls.py - /apps/logs/api/views.py - /apps/member/api/serializers.py - /apps/member/api/urls.py - /apps/member/api/views.py - /apps/member/templatetags/memberinfo.py - /apps/member/__init__.py - /apps/member/admin.py - /apps/member/apps.py - /apps/member/auth.py - /apps/member/forms.py - /apps/member/hashers.py - /apps/member/signals.py - /apps/member/tables.py - /apps/member/urls.py - /apps/member/views.py - /apps/note/api/serializers.py - /apps/note/api/urls.py - /apps/note/api/views.py - /apps/note/models/__init__.py - /apps/note/static/note/js/consos.js - /apps/note/templates/note/mails/negative_balance.txt - /apps/note/templatetags/getenv.py - /apps/note/templatetags/pretty_money.py - /apps/note/tests/test_transactions.py - /apps/note/__init__.py - /apps/note/admin.py - /apps/note/apps.py - /apps/note/forms.py - /apps/note/signals.py - /apps/note/tables.py - /apps/note/urls.py - /apps/note/views.py - /apps/permission/api/serializers.py - /apps/permission/api/urls.py - /apps/permission/api/views.py - /apps/permission/templatetags/perms.py - /apps/permission/tests/test_oauth2.py - /apps/permission/tests/test_permission_denied.py - /apps/permission/tests/test_permission_queries.py - /apps/permission/tests/test_rights_page.py - /apps/permission/__init__.py - /apps/permission/admin.py - /apps/permission/backends.py - /apps/permission/apps.py - /apps/permission/decorators.py - /apps/permission/permissions.py - /apps/permission/scopes.py - /apps/permission/signals.py - /apps/permission/tables.py - /apps/permission/urls.py - /apps/permission/views.py - /apps/registration/tests/test_registration.py - /apps/registration/__init__.py - /apps/registration/apps.py - /apps/registration/forms.py - /apps/registration/tables.py - /apps/registration/tokens.py - /apps/registration/urls.py - /apps/registration/views.py - /apps/treasury/api/serializers.py - /apps/treasury/api/urls.py - /apps/treasury/api/views.py - /apps/treasury/templatetags/escape_tex.py - /apps/treasury/tests/test_treasury.py - /apps/treasury/__init__.py - /apps/treasury/admin.py - /apps/treasury/apps.py - /apps/treasury/forms.py - /apps/treasury/signals.py - /apps/treasury/tables.py - /apps/treasury/urls.py - /apps/treasury/views.py - /apps/wei/api/serializers.py - /apps/wei/api/urls.py - /apps/wei/api/views.py - /apps/wei/forms/surveys/__init__.py - /apps/wei/forms/surveys/base.py - /apps/wei/forms/surveys/wei2021.py - /apps/wei/forms/surveys/wei2022.py - /apps/wei/forms/surveys/wei2023.py - /apps/wei/forms/__init__.py - /apps/wei/forms/registration.py - /apps/wei/management/commands/export_wei_registrations.py - /apps/wei/management/commands/import_scores.py - /apps/wei/management/commands/wei_algorithm.py - /apps/wei/templates/wei/weilist_sample.tex - /apps/wei/tests/test_wei_algorithm_2021.py - /apps/wei/tests/test_wei_algorithm_2022.py - /apps/wei/tests/test_wei_algorithm_2023.py - /apps/wei/tests/test_wei_registration.py - /apps/wei/__init__.py - /apps/wei/admin.py - /apps/wei/apps.py - /apps/wei/tables.py - /apps/wei/urls.py - /apps/wei/views.py - /note_kfet/settings/__init__.py - /note_kfet/settings/base.py - /note_kfet/settings/development.py - /note_kfet/settings/secrets_example.py - /note_kfet/static/js/base.js - /note_kfet/admin.py - /note_kfet/inputs.py - /note_kfet/middlewares.py - /note_kfet/urls.py - /note_kfet/views.py - /note_kfet/wsgi.py - /entrypoint.sh
296 lines
10 KiB
Python
296 lines
10 KiB
Python
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
import html
|
|
|
|
import django_tables2 as tables
|
|
from django.utils.html import format_html, mark_safe
|
|
from django_tables2.utils import A
|
|
from django.utils.translation import gettext_lazy as _
|
|
from note_kfet.middlewares import get_current_request
|
|
from permission.backends import PermissionBackend
|
|
|
|
from .models.notes import Alias, Trust
|
|
from .models.transactions import Transaction, TransactionTemplate
|
|
from .templatetags.pretty_money import pretty_money
|
|
|
|
|
|
class HistoryTable(tables.Table):
|
|
class Meta:
|
|
attrs = {
|
|
'class': 'table table-condensed table-striped'
|
|
}
|
|
model = Transaction
|
|
exclude = ("id", "polymorphic_ctype", "invalidity_reason", "source_alias", "destination_alias",)
|
|
template_name = 'django_tables2/bootstrap4.html'
|
|
sequence = ('...', 'type', 'total', 'valid',)
|
|
orderable = False
|
|
|
|
source = tables.Column(
|
|
attrs={
|
|
"td": {
|
|
"class": "text-nowrap",
|
|
"data-toggle": "tooltip",
|
|
"title": lambda record: _("used alias").capitalize() + " : " + record.source_alias,
|
|
}
|
|
}
|
|
)
|
|
|
|
destination = tables.Column(
|
|
attrs={
|
|
"td": {
|
|
"class": "text-nowrap",
|
|
"data-toggle": "tooltip",
|
|
"title": lambda record: _("used alias").capitalize() + " : " + record.destination_alias,
|
|
}
|
|
}
|
|
)
|
|
|
|
created_at = tables.DateTimeColumn(
|
|
format='Y-m-d H:i:s',
|
|
attrs={
|
|
"td": {
|
|
"class": "text-nowrap",
|
|
},
|
|
}
|
|
)
|
|
|
|
amount = tables.Column(
|
|
attrs={
|
|
"td": {
|
|
"class": "text-nowrap",
|
|
},
|
|
}
|
|
)
|
|
|
|
reason = tables.Column(
|
|
attrs={
|
|
"td": {
|
|
"class": "text-break",
|
|
},
|
|
}
|
|
)
|
|
|
|
type = tables.Column()
|
|
|
|
total = tables.Column( # will use Transaction.total() !!
|
|
attrs={
|
|
"td": {
|
|
"class": "text-nowrap",
|
|
},
|
|
}
|
|
)
|
|
|
|
valid = tables.Column(
|
|
attrs={
|
|
"td": {
|
|
"id": lambda record: "validate_" + str(record.id),
|
|
"class": lambda record:
|
|
str(record.valid).lower()
|
|
+ (' validate' if record.source.is_active and record.destination.is_active and PermissionBackend
|
|
.check_perm(get_current_request(), "note.change_transaction_invalidity_reason", record)
|
|
else ''),
|
|
"data-toggle": "tooltip",
|
|
"title": lambda record: (_("Click to invalidate") if record.valid else _("Click to validate"))
|
|
if PermissionBackend.check_perm(get_current_request(),
|
|
"note.change_transaction_invalidity_reason", record)
|
|
and record.source.is_active and record.destination.is_active else None,
|
|
"onclick": lambda record: 'de_validate(' + str(record.id) + ', ' + str(record.valid).lower()
|
|
+ ', "' + str(record.__class__.__name__) + '")'
|
|
if PermissionBackend.check_perm(get_current_request(),
|
|
"note.change_transaction_invalidity_reason", record)
|
|
and record.source.is_active and record.destination.is_active else None,
|
|
"onmouseover": lambda record: '$("#invalidity_reason_'
|
|
+ str(record.id) + '").show();$("#invalidity_reason_'
|
|
+ str(record.id) + '").focus();',
|
|
"onmouseout": lambda record: '$("#invalidity_reason_' + str(record.id) + '").hide()',
|
|
}
|
|
}
|
|
)
|
|
|
|
def render_amount(self, value):
|
|
return pretty_money(value)
|
|
|
|
def render_total(self, value):
|
|
return pretty_money(value)
|
|
|
|
def render_type(self, value):
|
|
return _(value)
|
|
|
|
# Django-tables escape strings. That's a wrong thing.
|
|
def render_reason(self, value):
|
|
return html.unescape(value)
|
|
|
|
def render_valid(self, value, record):
|
|
"""
|
|
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_request(), "note.change_transaction_invalidity_reason", record)
|
|
|
|
val = "✔" if value else "✖"
|
|
|
|
if value and not has_perm:
|
|
return val
|
|
|
|
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 record.source.is_active and record.destination.is_active else " disabled") \
|
|
+ " placeholder='" + html.escape(_("invalidity reason").capitalize()) + "'" \
|
|
+ " style='position: absolute; width: 15em; margin-left: -15.5em; margin-top: -2em; display: none;'>"
|
|
return format_html(val)
|
|
|
|
|
|
# function delete_button(id) provided in template file
|
|
DELETE_TEMPLATE = """
|
|
<button id="{{ record.pk }}" class="btn btn-danger btn-sm" onclick="delete_button(this.id)"> {{ delete_trans }}</button>
|
|
"""
|
|
|
|
|
|
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
|
|
trusted = tables.Column(attrs={'td': {'class': 'text-center'}})
|
|
|
|
delete_col = tables.TemplateColumn(
|
|
template_code=DELETE_TEMPLATE,
|
|
extra_context={"delete_trans": _('Delete')},
|
|
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"),)
|
|
|
|
|
|
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
|
|
trusting = tables.Column(attrs={
|
|
'td': {'class': 'text-center', 'width': '100%'}})
|
|
|
|
trust_back = tables.Column(
|
|
verbose_name=_("Trust back"),
|
|
accessor="pk",
|
|
attrs={
|
|
'td': {
|
|
'class': '',
|
|
'id': lambda record: "trust_back_" + str(record.pk),
|
|
}
|
|
},
|
|
)
|
|
|
|
def render_trust_back(self, record):
|
|
user_note = record.trusted
|
|
trusting_note = record.trusting
|
|
if Trust.objects.filter(trusted=trusting_note, trusting=user_note):
|
|
return ""
|
|
val = '<button id="'
|
|
val += str(record.pk)
|
|
val += '" class="btn btn-success btn-sm text-nowrap" \
|
|
onclick="create_trust(' + str(record.trusted.pk) + ',' + \
|
|
str(record.trusting.pk) + ')">'
|
|
val += str(_("Add back"))
|
|
val += '</button>'
|
|
return mark_safe(val)
|
|
|
|
|
|
class AliasTable(tables.Table):
|
|
class Meta:
|
|
attrs = {
|
|
'class': 'table table condensed table-striped',
|
|
'id': "alias_table"
|
|
}
|
|
model = Alias
|
|
fields = ('name',)
|
|
template_name = 'django_tables2/bootstrap4.html'
|
|
|
|
show_header = False
|
|
name = tables.Column(attrs={'td': {'class': 'text-center'}})
|
|
|
|
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
|
|
extra_context={"delete_trans": _('delete')},
|
|
attrs={'td': {'class': lambda record: 'col-sm-1' + (
|
|
' d-none' if not PermissionBackend.check_perm(
|
|
get_current_request(), "note.delete_alias",
|
|
record) else '')}}, verbose_name=_("Delete"), )
|
|
|
|
|
|
class ButtonTable(tables.Table):
|
|
class Meta:
|
|
attrs = {
|
|
'class': 'table table-bordered condensed'
|
|
}
|
|
row_attrs = {
|
|
'class': lambda record: 'table-row ' + ('table-success' if record.display else 'table-danger'),
|
|
'id': lambda record: "row-" + str(record.pk),
|
|
}
|
|
|
|
model = TransactionTemplate
|
|
exclude = ('id',)
|
|
|
|
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"),
|
|
)
|
|
|
|
hideshow = tables.Column(
|
|
verbose_name=_("Hide/Show"),
|
|
accessor="pk",
|
|
attrs={
|
|
'td': {
|
|
'class': 'col-sm-1',
|
|
'id': lambda record: "hideshow_" + str(record.pk),
|
|
}
|
|
},
|
|
)
|
|
|
|
delete_col = tables.TemplateColumn(template_code=DELETE_TEMPLATE,
|
|
extra_context={"delete_trans": _('delete')},
|
|
attrs={'td': {'class': 'col-sm-1'}},
|
|
verbose_name=_("Delete"), )
|
|
|
|
def render_amount(self, value):
|
|
return pretty_money(value)
|
|
|
|
def order_category(self, queryset, is_descending):
|
|
return queryset.order_by(f"{'-' if is_descending else ''}category__name"), True
|
|
|
|
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)
|