diff --git a/tournaments/admin.py b/tournaments/admin.py index 8aef1c3..45da03e 100644 --- a/tournaments/admin.py +++ b/tournaments/admin.py @@ -8,7 +8,7 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm from .forms import CustomUserCreationForm, CustomUserChangeForm -from .filters import TeamScoreTournamentListFilter, MatchTournamentListFilter +from .filters import TeamScoreTournamentListFilter, MatchTournamentListFilter, SimpleTournamentListFilter, MatchTypeListFilter, SimpleIndexListFilter class CustomUserAdmin(UserAdmin): @@ -47,7 +47,7 @@ class TeamScoreAdmin(admin.ModelAdmin): class RoundAdmin(admin.ModelAdmin): list_display = ['tournament', 'name', 'index', 'parent'] - list_filter = ['tournament'] + list_filter = [SimpleTournamentListFilter, SimpleIndexListFilter] class PlayerRegistrationAdmin(admin.ModelAdmin): list_display = ['first_name', 'last_name', 'licence_id', 'rank'] @@ -55,11 +55,11 @@ class PlayerRegistrationAdmin(admin.ModelAdmin): class MatchAdmin(admin.ModelAdmin): list_display = ['__str__', 'round', 'group_stage', 'start_date', 'index'] - list_filter = [MatchTournamentListFilter] + list_filter = [MatchTournamentListFilter, MatchTypeListFilter, SimpleIndexListFilter] class GroupStageAdmin(admin.ModelAdmin): list_display = ['tournament', 'index', 'start_date'] - list_filter = ['tournament'] + list_filter = [SimpleTournamentListFilter] class EventAdmin(admin.ModelAdmin): list_display = ['creation_date', 'name', 'club', 'creator'] diff --git a/tournaments/filters.py b/tournaments/filters.py index d5fdcbc..167915f 100644 --- a/tournaments/filters.py +++ b/tournaments/filters.py @@ -3,6 +3,25 @@ from .models import Club, TeamScore, Tournament, CustomUser, Event, Round, Group from django.db.models import Q from django.utils.translation import gettext_lazy as _ import uuid +from enum import Enum + +class SimpleTournamentListFilter(admin.SimpleListFilter): + # Human-readable title which will be displayed in the + # right admin sidebar just above the filter options. + title = _("tournoi") + + # Parameter for the filter that will be used in the URL query. + parameter_name = "tournament" + + def lookups(self, request, model_admin): + tournaments = Tournament.objects.order_by('-start_date') + return [(t.id, t.filter_name()) for t in tournaments] + + def queryset(self, request, queryset): + if self.value(): + return queryset.filter(tournament__id=self.value()) + else: + return queryset class TeamScoreTournamentListFilter(admin.SimpleListFilter): # Human-readable title which will be displayed in the @@ -41,3 +60,44 @@ class MatchTournamentListFilter(admin.SimpleListFilter): return queryset.filter(query_round | query_group_stage) else: return queryset + +class MatchType(Enum): + GROUP_STAGE = 'Poule' + BRACKET = 'Tableau' + +class MatchTypeListFilter(admin.SimpleListFilter): + # Human-readable title which will be displayed in the + # right admin sidebar just above the filter options. + title = _("Type") + + # Parameter for the filter that will be used in the URL query. + parameter_name = "name" + + def lookups(self, request, model_admin): + return [(tag.name, tag.value) for tag in MatchType] + + def queryset(self, request, queryset): + if self.value() == MatchType.GROUP_STAGE.name: + return queryset.filter(group_stage__isnull=False) + elif self.value() == MatchType.BRACKET.name: + return queryset.filter(round__isnull=False) + else: + return queryset + +class SimpleIndexListFilter(admin.SimpleListFilter): + # Human-readable title which will be displayed in the + # right admin sidebar just above the filter options. + title = _("Index") + + # Parameter for the filter that will be used in the URL query. + parameter_name = "index" + + def lookups(self, request, model_admin): + indexes = Match.objects.values_list('index', flat=True).distinct() + return [(i, i) for i in sorted(indexes)] + + def queryset(self, request, queryset): + if self.value(): + return queryset.filter(index=self.value()) + else: + return queryset