2021-06-14 19:45:36 +00:00
|
|
|
# Copyright (C) 2018-2021 by BDE ENS Paris-Saclay
|
2020-08-05 19:07:31 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
import django_tables2 as tables
|
2020-08-31 19:11:00 +00:00
|
|
|
from django.contrib.auth.models import User
|
2020-08-31 19:49:02 +00:00
|
|
|
from django.db.models import Q
|
2020-08-05 19:07:31 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.html import format_html
|
2020-08-31 19:11:00 +00:00
|
|
|
from django_tables2 import A
|
2020-08-05 19:07:31 +00:00
|
|
|
from member.models import Membership
|
2021-06-15 12:40:32 +00:00
|
|
|
from note_kfet.middlewares import get_current_request
|
2020-08-05 19:07:31 +00:00
|
|
|
from permission.backends import PermissionBackend
|
|
|
|
|
|
|
|
|
|
|
|
class RightsTable(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
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), "auth.view_user", value):
|
2020-08-05 19:07:31 +00:00
|
|
|
s = format_html("<a href={url}>{name}</a>",
|
|
|
|
url=reverse_lazy('member:user_detail', kwargs={"pk": value.pk}), name=s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
def render_club(self, value):
|
|
|
|
# If the user has the right, link the displayed user with the page of its detail.
|
|
|
|
s = value.name
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), "member.view_club", value):
|
2020-08-05 19:07:31 +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_roles(self, record):
|
|
|
|
# If the user has the right to manage the roles, display the link to manage them
|
2022-08-29 11:19:19 +00:00
|
|
|
roles = record.roles.filter((~(Q(name="Adhérent⋅e BDE")
|
|
|
|
| Q(name="Adhérent⋅e Kfet")
|
2020-08-31 19:49:02 +00:00
|
|
|
| Q(name="Membre de club")
|
|
|
|
| Q(name="Bureau de club"))
|
|
|
|
& Q(weirole__isnull=True))).all()
|
2020-08-05 19:07:31 +00:00
|
|
|
s = ", ".join(str(role) for role in roles)
|
2021-06-15 12:40:32 +00:00
|
|
|
if PermissionBackend.check_perm(get_current_request(), "member.change_membership_roles", record):
|
2020-08-05 19:07:31 +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 = {
|
|
|
|
'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__last_name', 'user__first_name', 'user', 'club', 'roles', )
|
2020-08-05 19:07:31 +00:00
|
|
|
model = Membership
|
2020-08-31 19:11:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SuperuserTable(tables.Table):
|
|
|
|
username = tables.LinkColumn(
|
|
|
|
"member:user_detail",
|
|
|
|
args=[A("pk")],
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ('last_name', 'first_name', 'username', )
|
|
|
|
attrs = {
|
|
|
|
'class': 'table table-condensed table-striped table-hover',
|
|
|
|
'style': 'table-layout: fixed;'
|
|
|
|
}
|