nk20/apps/activity/tables.py

121 lines
4.3 KiB
Python
Raw Normal View History

# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
2020-03-27 17:02:22 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
2022-04-01 06:53:18 +00:00
import datetime
2020-08-06 15:41:30 +00:00
from django.utils import timezone
from django.utils.html import escape
from django.utils.safestring import mark_safe
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
from .models import Activity, Entry, Guest
2020-03-27 17:02:22 +00:00
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'
}
2020-08-30 21:54:54 +00:00
row_attrs = {
'class': lambda record: 'bg-success' if record.open else ('' if record.valid else 'bg-warning'),
'title': lambda record: _("The activity is currently open.") if record.open else
('' if record.valid else _("The validation of the activity is pending.")),
}
2020-03-27 17:02:22 +00:00
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'), ],
2020-03-27 20:18:27 +00:00
)
entry = tables.Column(
empty_values=(),
2020-08-30 21:54:54 +00:00
verbose_name=_("Remove"),
2020-03-27 20:18:27 +00:00
)
2020-03-27 17:02:22 +00:00
class Meta:
attrs = {
2020-08-30 21:54:54 +00:00
'class': 'table table-condensed table-striped'
2020-03-27 17:02:22 +00:00
}
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, )))
return mark_safe('<button id="{id}" class="btn btn-danger btn-sm" onclick="remove_guest(this.id)"> '
'{delete_trans}</button>'.format(id=record.id, delete_trans=_("remove").capitalize()))
2020-03-28 00:45:13 +00:00
def get_row_class(record):
c = "table-row"
if isinstance(record, Guest):
if record.has_entry:
c += " table-success"
else:
c += " table-warning"
else:
qs = Entry.objects.filter(note=record.note, activity=record.activity, guest=None)
if qs.exists():
c += " table-success"
2020-08-06 15:41:30 +00:00
elif not record.note.user.memberships.filter(club=record.activity.attendees_club,
date_start__lte=timezone.now(),
date_end__gte=timezone.now()).exists():
c += " table-info"
elif record.note.balance < 0:
c += " table-danger"
2022-04-01 06:53:18 +00:00
#MODE VIEUXCON=ON
2023-01-29 20:18:13 +00:00
if (datetime.datetime.utcnow().timestamp() - record.note.created_at.timestamp()) > 3600*24*365*3:
2022-04-01 06:53:18 +00:00
c += " font-weight-bold"
return c
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 mark_safe(escape(value) + " <em>aka.</em> " + escape(username))
2020-03-28 00:45:13 +00:00
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': lambda record: get_row_class(record),
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 if isinstance(record, Guest) else record.note.pk,
2020-03-28 12:38:31 +00:00
'data-inviter': lambda record: record.inviter.pk if isinstance(record, Guest) else "",
'data-last-name': lambda record: record.last_name,
'data-first-name': lambda record: record.first_name,
2020-03-28 00:45:13 +00:00
}