1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-02-06 15:23:00 +00:00
nk20/apps/note/tables.py

78 lines
2.6 KiB
Python
Raw Normal View History

2020-02-18 21:30:26 +01:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-14 02:08:23 +01:00
import html
2019-08-15 23:08:15 +02:00
import django_tables2 as tables
2020-02-16 22:29:27 +01:00
from django.db.models import F
from django_tables2.utils import A
2020-03-16 12:11:16 +01:00
from django.utils.translation import gettext_lazy as _
2020-03-07 22:28:59 +01:00
2020-02-28 13:37:31 +01:00
from .models.notes import Alias
2020-03-10 08:07:09 +01:00
from .models.transactions import Transaction
2020-03-09 18:53:13 +01:00
from .templatetags.pretty_money import pretty_money
2019-08-15 23:08:15 +02:00
2020-03-09 19:19:56 +01:00
2019-08-15 23:08:15 +02:00
class HistoryTable(tables.Table):
class Meta:
2020-02-18 12:31:15 +01:00
attrs = {
'class':
2020-03-07 22:28:59 +01:00
'table table-condensed table-striped table-hover'
2020-02-18 12:31:15 +01:00
}
2019-08-15 23:08:15 +02:00
model = Transaction
2020-03-13 09:26:39 +01:00
exclude = ("id", "polymorphic_ctype", )
template_name = 'django_tables2/bootstrap4.html'
2020-03-16 12:11:16 +01:00
sequence = ('...', 'type', 'total', 'valid', )
2020-03-14 02:08:23 +01:00
orderable = False
2019-08-15 23:08:15 +02:00
2020-03-16 12:11:16 +01:00
type = tables.Column()
2020-02-18 21:14:29 +01:00
total = tables.Column() # will use Transaction.total() !!
2019-08-15 23:08:15 +02:00
2020-03-14 02:08:23 +01:00
valid = tables.Column(attrs={"td": {"id": lambda record: "validate_" + str(record.id),
2020-03-14 12:07:03 +01:00
"class": lambda record: str(record.valid).lower() + ' validate',
"onclick": lambda record: 'de_validate(' + str(record.id) + ', '
+ str(record.valid).lower() + ')'}})
2020-03-14 02:08:23 +01:00
2020-02-18 21:14:29 +01:00
def order_total(self, queryset, is_descending):
2019-08-15 23:08:15 +02:00
# needed for rendering
2020-02-18 21:14:29 +01:00
queryset = queryset.annotate(total=F('amount') * F('quantity')) \
.order_by(('-' if is_descending else '') + 'total')
2020-03-14 02:08:23 +01:00
return queryset, True
2020-03-09 19:19:56 +01:00
def render_amount(self, value):
2020-03-09 18:53:13 +01:00
return pretty_money(value)
2020-03-09 19:19:56 +01:00
def render_total(self, value):
2020-03-09 18:53:13 +01:00
return pretty_money(value)
2020-03-07 22:28:59 +01:00
2020-03-16 12:11:16 +01:00
def render_type(self, value):
return _(value)
2020-03-14 02:08:23 +01:00
# Django-tables escape strings. That's a wrong thing.
def render_reason(self, value):
return html.unescape(value)
def render_valid(self, value):
return "" if value else ""
2020-02-28 13:37:31 +01:00
class AliasTable(tables.Table):
class Meta:
attrs = {
'class':
2020-03-07 22:28:59 +01:00
'table table condensed table-striped table-hover'
2020-02-28 13:37:31 +01:00
}
model = Alias
2020-03-07 22:28:59 +01:00
fields = ('name',)
2020-02-28 13:37:31 +01:00
template_name = 'django_tables2/bootstrap4.html'
2020-03-01 13:42:22 +01:00
show_header = False
2020-03-07 22:28:59 +01:00
name = tables.Column(attrs={'td': {'class': 'text-center'}})
2020-03-01 13:42:22 +01:00
delete = tables.LinkColumn('member:user_alias_delete',
args=[A('pk')],
attrs={
2020-03-07 22:28:59 +01:00
'td': {'class': 'col-sm-2'},
'a': {'class': 'btn btn-danger'}},
text='delete', accessor='pk')