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
|
|
|
|
2020-08-15 22:35:13 +00:00
|
|
|
from datetime import date
|
|
|
|
|
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 16:47:56 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-04-01 01:42:19 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.html import format_html
|
|
|
|
from note.templatetags.pretty_money import pretty_money
|
|
|
|
from note_kfet.middlewares import get_current_authenticated_user
|
|
|
|
from permission.backends import PermissionBackend
|
2020-04-01 02:07:55 +00:00
|
|
|
|
2020-04-01 01:42:19 +00:00
|
|
|
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):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
List all clubs.
|
|
|
|
"""
|
2019-08-15 19:52:10 +00:00
|
|
|
class Meta:
|
2020-02-18 11:31:15 +00:00
|
|
|
attrs = {
|
2020-02-21 11:29:11 +00:00
|
|
|
'class': 'table table-condensed table-striped table-hover'
|
2020-02-18 11:31:15 +00:00
|
|
|
}
|
2020-06-02 07:37:25 +00:00
|
|
|
order_by = ('id',)
|
2019-08-15 19:52:10 +00:00
|
|
|
model = Club
|
2020-02-21 11:29:11 +00:00
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
2020-06-21 20:27:32 +00:00
|
|
|
fields = ('name', 'email',)
|
|
|
|
order_by = ('name',)
|
2020-02-18 11:31:15 +00:00
|
|
|
row_attrs = {
|
|
|
|
'class': 'table-row',
|
2020-03-25 15:11:44 +00:00
|
|
|
'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):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
List all users.
|
|
|
|
"""
|
2020-08-06 11:07:22 +00:00
|
|
|
alias = tables.Column()
|
|
|
|
|
2020-09-01 15:58:58 +00:00
|
|
|
section = tables.Column(accessor='profile__section')
|
2020-04-01 18:14:16 +00:00
|
|
|
|
2020-10-25 20:29:44 +00:00
|
|
|
# Override the column to let replace the URL
|
|
|
|
email = tables.EmailColumn(linkify=lambda record: "mailto:{}".format(record.email))
|
|
|
|
|
2020-09-01 15:58:58 +00:00
|
|
|
balance = tables.Column(accessor='note__balance', verbose_name=_("Balance"))
|
2020-04-01 18:14:16 +00:00
|
|
|
|
2020-10-25 20:29:44 +00:00
|
|
|
def render_email(self, record, value):
|
|
|
|
# Replace the email by a dash if the user can't see the profile detail
|
|
|
|
# Replace also the URL
|
|
|
|
if not PermissionBackend.check_perm(get_current_authenticated_user(), "member.view_profile", record.profile):
|
|
|
|
value = "—"
|
|
|
|
record.email = value
|
|
|
|
return value
|
|
|
|
|
|
|
|
def render_section(self, record, value):
|
|
|
|
return value \
|
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "member.view_profile", record.profile) \
|
|
|
|
else "—"
|
|
|
|
|
2020-08-10 14:32:45 +00:00
|
|
|
def render_balance(self, record, value):
|
|
|
|
return pretty_money(value)\
|
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "note.view_note", record.note) else "—"
|
2019-09-23 10:50:14 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-02-18 11:31:15 +00:00
|
|
|
attrs = {
|
2020-02-21 11:29:11 +00:00
|
|
|
'class': 'table table-condensed table-striped table-hover'
|
2020-02-18 11:31:15 +00:00
|
|
|
}
|
2020-02-21 11:29:11 +00:00
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
2020-08-06 11:07:22 +00:00
|
|
|
fields = ('last_name', 'first_name', 'username', 'alias', 'email')
|
2019-09-23 10:50:14 +00:00
|
|
|
model = User
|
2020-04-01 18:14:16 +00:00
|
|
|
row_attrs = {
|
|
|
|
'class': 'table-row',
|
|
|
|
'data-href': lambda record: record.pk
|
|
|
|
}
|
2020-04-01 01:42:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MembershipTable(tables.Table):
|
2020-04-06 06:58:39 +00:00
|
|
|
"""
|
|
|
|
List all memberships.
|
|
|
|
"""
|
2020-04-01 01:42:19 +00:00
|
|
|
roles = tables.Column(
|
|
|
|
attrs={
|
|
|
|
"td": {
|
|
|
|
"class": "text-truncate",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-04-06 06:58:39 +00:00
|
|
|
def render_user(self, value):
|
|
|
|
# If the user has the right, link the displayed user with the page of its detail.
|
|
|
|
s = value.username
|
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "auth.view_user", value):
|
|
|
|
s = format_html("<a href={url}>{name}</a>",
|
|
|
|
url=reverse_lazy('member:user_detail', kwargs={"pk": value.pk}), name=s)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
2020-04-01 16:47:56 +00:00
|
|
|
def render_club(self, value):
|
2020-04-06 06:58:39 +00:00
|
|
|
# If the user has the right, link the displayed club with the page of its detail.
|
2020-04-01 16:47:56 +00:00
|
|
|
s = value.name
|
2020-04-02 12:50:28 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "member.view_club", value):
|
2020-04-01 16:47:56 +00:00
|
|
|
s = format_html("<a href={url}>{name}</a>",
|
|
|
|
url=reverse_lazy('member:club_detail', kwargs={"pk": value.pk}), name=s)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
def render_fee(self, value, record):
|
|
|
|
t = pretty_money(value)
|
|
|
|
|
|
|
|
# If it is required and if the user has the right, the renew button is displayed.
|
2020-08-25 14:44:01 +00:00
|
|
|
if record.club.membership_start is not None \
|
|
|
|
and record.date_start < record.club.membership_start:
|
|
|
|
if not Membership.objects.filter(
|
|
|
|
club=record.club,
|
|
|
|
user=record.user,
|
|
|
|
date_start__gte=record.club.membership_start,
|
|
|
|
date_end__lte=record.club.membership_end,
|
|
|
|
).exists(): # If the renew is not yet performed
|
|
|
|
empty_membership = Membership(
|
|
|
|
club=record.club,
|
|
|
|
user=record.user,
|
|
|
|
date_start=date.today(),
|
|
|
|
date_end=date.today(),
|
|
|
|
fee=0,
|
|
|
|
)
|
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(),
|
2020-09-23 19:36:04 +00:00
|
|
|
"member.add_membership", empty_membership): # If the user has right
|
2020-08-25 14:44:01 +00:00
|
|
|
renew_url = reverse_lazy('member:club_renew_membership',
|
|
|
|
kwargs={"pk": record.pk})
|
|
|
|
t = format_html(
|
|
|
|
t + ' <a class="btn btn-sm btn-warning" title="{text}"'
|
|
|
|
' href="{renew_url}"><i class="fa fa-repeat"></i></a>',
|
|
|
|
renew_url=renew_url, text=_("Renew")
|
2020-04-01 16:47:56 +00:00
|
|
|
)
|
|
|
|
return t
|
2020-04-01 01:42:19 +00:00
|
|
|
|
|
|
|
def render_roles(self, record):
|
2020-04-06 06:58:39 +00:00
|
|
|
# If the user has the right to manage the roles, display the link to manage them
|
2020-04-01 01:42:19 +00:00
|
|
|
roles = record.roles.all()
|
|
|
|
s = ", ".join(str(role) for role in roles)
|
2020-04-02 12:50:28 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "member.change_membership_roles", record):
|
2020-04-01 01:42:19 +00:00
|
|
|
s = format_html("<a href='" + str(reverse_lazy("member:club_manage_roles", kwargs={"pk": record.pk}))
|
|
|
|
+ "'>" + s + "</a>")
|
|
|
|
return s
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
2020-08-10 16:01:39 +00:00
|
|
|
'class': 'table table-condensed table-striped',
|
2020-04-01 01:42:19 +00:00
|
|
|
'style': 'table-layout: fixed;'
|
|
|
|
}
|
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
|
|
|
fields = ('user', 'club', 'date_start', 'date_end', 'roles', 'fee', )
|
|
|
|
model = Membership
|
2020-07-31 15:01:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ClubManagerTable(tables.Table):
|
|
|
|
"""
|
|
|
|
List managers of a club.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def render_user(self, value):
|
|
|
|
# If the user has the right, link the displayed user with the page of its detail.
|
|
|
|
s = value.username
|
|
|
|
if PermissionBackend.check_perm(get_current_authenticated_user(), "auth.view_user", value):
|
|
|
|
s = format_html("<a href={url}>{name}</a>",
|
|
|
|
url=reverse_lazy('member:user_detail', kwargs={"pk": value.pk}), name=s)
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
def render_roles(self, record):
|
|
|
|
roles = record.roles.all()
|
|
|
|
return ", ".join(str(role) for role in roles)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
attrs = {
|
|
|
|
'class': 'table table-condensed table-striped table-hover',
|
|
|
|
'style': 'table-layout: fixed;'
|
|
|
|
}
|
|
|
|
template_name = 'django_tables2/bootstrap4.html'
|
2020-09-01 17:04:35 +00:00
|
|
|
fields = ('user', 'user__first_name', 'user__last_name', 'roles', )
|
2020-07-31 15:01:52 +00:00
|
|
|
model = Membership
|