nk20/apps/member/tables.py

72 lines
2.3 KiB
Python
Raw Normal View History

2020-02-18 20:30:26 +00:00
# Copyright (C) 2018-2020 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
2019-08-15 19:52:10 +00:00
import django_tables2 as tables
2019-09-23 10:50:14 +00:00
from django.contrib.auth.models import User
2020-04-01 01:42:19 +00:00
from django.urls import reverse_lazy
from django.utils.html import format_html
from django_tables2 import A
2019-08-15 19:52:10 +00:00
2020-04-01 01:42:19 +00:00
from note.templatetags.pretty_money import pretty_money
from note_kfet.middlewares import get_current_authenticated_user
from permission.backends import PermissionBackend
from .models import Club, Membership
2020-02-18 20:14:29 +00:00
2020-02-18 11:31:15 +00:00
2019-08-15 19:52:10 +00:00
class ClubTable(tables.Table):
class Meta:
2020-02-18 11:31:15 +00:00
attrs = {
'class': 'table table-condensed table-striped table-hover'
2020-02-18 11:31:15 +00:00
}
2019-08-15 19:52:10 +00:00
model = Club
template_name = 'django_tables2/bootstrap4.html'
2020-02-18 11:31:15 +00:00
fields = ('id', 'name', 'email')
row_attrs = {
'class': 'table-row',
'id': lambda record: "row-" + str(record.pk),
2020-02-18 11:31:15 +00:00
'data-href': lambda record: record.pk
}
2019-09-23 10:50:14 +00:00
class UserTable(tables.Table):
section = tables.Column(accessor='profile.section')
solde = tables.Column(accessor='note.balance')
class Meta:
2020-02-18 11:31:15 +00:00
attrs = {
'class': 'table table-condensed table-striped table-hover'
2020-02-18 11:31:15 +00:00
}
template_name = 'django_tables2/bootstrap4.html'
2020-02-18 11:31:15 +00:00
fields = ('last_name', 'first_name', 'username', 'email')
2019-09-23 10:50:14 +00:00
model = User
2020-04-01 01:42:19 +00:00
class MembershipTable(tables.Table):
roles = tables.Column(
attrs={
"td": {
"class": "text-truncate",
}
}
)
def render_fee(self, value):
return pretty_money(value)
def render_roles(self, record):
roles = record.roles.all()
s = ", ".join(str(role) for role in roles)
if PermissionBackend().has_perm(get_current_authenticated_user(), "member.change_membership_roles", record):
s = format_html("<a href='" + str(reverse_lazy("member:club_manage_roles", kwargs={"pk": record.pk}))
+ "'>" + s + "</a>")
return s
class Meta:
attrs = {
'class': 'table table-condensed table-striped table-hover',
'style': 'table-layout: fixed;'
}
template_name = 'django_tables2/bootstrap4.html'
fields = ('user', 'club', 'date_start', 'date_end', 'roles', 'fee', )
model = Membership