nk20/apps/activity/tables.py

91 lines
2.9 KiB
Python
Raw Normal View History

2020-03-27 17:02:22 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2020-03-28 01:09:10 +00:00
2020-03-28 00:45:13 +00:00
from django.utils.html import format_html
2020-03-27 17:02:22 +00:00
from django.utils.translation import gettext_lazy as _
2020-03-27 20:18:27 +00:00
import django_tables2 as tables
from django_tables2 import A
2020-03-28 00:45:13 +00:00
from note.templatetags.pretty_money import pretty_money
2020-03-28 01:09:10 +00:00
2020-03-27 17:02:22 +00:00
from .models import Activity, Guest
class ActivityTable(tables.Table):
2020-03-27 20:18:27 +00:00
name = tables.LinkColumn(
'activity:activity_detail',
args=[A('pk'), ],
)
2020-03-27 17:02:22 +00:00
class Meta:
attrs = {
'class': 'table table-condensed table-striped table-hover'
}
model = Activity
template_name = 'django_tables2/bootstrap4.html'
2020-03-27 20:18:27 +00:00
fields = ('name', 'activity_type', 'organizer', 'attendees_club', 'date_start', 'date_end', )
2020-03-27 17:02:22 +00:00
class GuestTable(tables.Table):
2020-03-27 20:18:27 +00:00
inviter = tables.LinkColumn(
'member:user_detail',
args=[A('inviter.user.pk'), ],
)
entry = tables.Column(
empty_values=(),
attrs={
"td": {
2020-03-28 12:38:31 +00:00
"class": lambda record: "" if record.has_entry else "validate btn btn-danger",
"onclick": lambda record: "" if record.has_entry else "remove_guest(" + str(record.pk) + ")"
2020-03-27 20:18:27 +00:00
}
}
)
2020-03-27 17:02:22 +00:00
class Meta:
attrs = {
'class': 'table table-condensed table-striped table-hover'
}
model = Guest
template_name = 'django_tables2/bootstrap4.html'
2020-03-27 20:18:27 +00:00
fields = ("last_name", "first_name", "inviter", )
def render_entry(self, record):
2020-03-28 12:38:31 +00:00
if record.has_entry:
return str(_("Entered on ") + str(_("{:%Y-%m-%d %H:%M:%S}").format(record.entry.time, )))
2020-03-27 20:18:27 +00:00
return _("remove").capitalize()
2020-03-28 00:45:13 +00:00
class EntryTable(tables.Table):
2020-03-28 01:08:29 +00:00
type = tables.Column(verbose_name=_("Type"))
2020-03-28 00:45:13 +00:00
2020-03-28 01:08:29 +00:00
last_name = tables.Column(verbose_name=_("Last name"))
2020-03-28 00:45:13 +00:00
2020-03-28 01:08:29 +00:00
first_name = tables.Column(verbose_name=_("First name"))
2020-03-28 00:45:13 +00:00
2020-03-28 01:08:29 +00:00
note_name = tables.Column(verbose_name=_("Note"))
2020-03-28 00:45:13 +00:00
2020-03-28 01:08:29 +00:00
balance = tables.Column(verbose_name=_("Balance"))
2020-03-28 00:45:13 +00:00
def render_note_name(self, value, record):
if hasattr(record, 'username'):
username = record.username
if username != value:
return format_html(value + " <em>aka.</em> " + username)
return value
def render_balance(self, value):
return pretty_money(value)
class Meta:
attrs = {
'class': 'table table-condensed table-striped table-hover'
}
template_name = 'django_tables2/bootstrap4.html'
row_attrs = {
'class': 'table-row',
2020-03-28 12:38:31 +00:00
'id': lambda record: "row-" + ("guest-" if isinstance(record, Guest) else "membership-") + str(record.pk),
'data-type': lambda record: "guest" if isinstance(record, Guest) else "membership",
'data-id': lambda record: record.pk,
'data-inviter': lambda record: record.inviter.pk if isinstance(record, Guest) else "",
2020-03-28 00:45:13 +00:00
}