You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
7.6 KiB
216 lines
7.6 KiB
from django.contrib import admin
|
|
from .models import Tournament, Match, Round
|
|
from django.db.models import Q, Count
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils import timezone
|
|
from datetime import timedelta
|
|
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
|
|
# 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(team_registration__tournament__id=self.value())
|
|
else:
|
|
return queryset
|
|
|
|
class MatchTournamentListFilter(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():
|
|
query_round = Q(round__tournament__id=self.value())
|
|
query_group_stage = Q(group_stage__tournament__id=self.value())
|
|
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
|
|
|
|
class StartDateRangeFilter(admin.SimpleListFilter):
|
|
title = 'tournament time range' # displayed in the admin UI
|
|
parameter_name = 'date_range' # URL parameter
|
|
|
|
def lookups(self, request, model_admin):
|
|
return (
|
|
('upcoming', 'Next 30 days'),
|
|
('recent', 'Last 30 days'),
|
|
('current', 'Current (±3 days)'),
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
if not self.value():
|
|
return queryset
|
|
|
|
today = timezone.now().date()
|
|
|
|
if self.value() == 'upcoming':
|
|
return queryset.filter(
|
|
start_date__gte=today,
|
|
start_date__lte=today + timedelta(days=30)
|
|
)
|
|
elif self.value() == 'recent':
|
|
return queryset.filter(
|
|
start_date__gte=today - timedelta(days=30),
|
|
start_date__lte=today
|
|
)
|
|
elif self.value() == 'current':
|
|
return queryset.filter(
|
|
start_date__gte=today - timedelta(days=3),
|
|
start_date__lte=today + timedelta(days=3)
|
|
)
|
|
|
|
class UserWithEventsFilter(admin.SimpleListFilter):
|
|
title = _('has events')
|
|
parameter_name = 'has_events'
|
|
|
|
def lookups(self, request, model_admin):
|
|
return (
|
|
('yes', _('Has events')),
|
|
('no', _('No events')),
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
if self.value() == 'yes':
|
|
return queryset.annotate(events_count=Count('events')).filter(events_count__gt=0)
|
|
elif self.value() == 'no':
|
|
return queryset.annotate(events_count=Count('events')).filter(events_count=0)
|
|
return queryset
|
|
|
|
class UserWithPurchasesFilter(admin.SimpleListFilter):
|
|
title = _('has purchases')
|
|
parameter_name = 'has_purchases'
|
|
|
|
def lookups(self, request, model_admin):
|
|
return (
|
|
('yes', _('Has purchases')),
|
|
('no', _('No purchases')),
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
if self.value() == 'yes':
|
|
return queryset.annotate(purchases_count=Count('purchases')).filter(purchases_count__gt=0)
|
|
elif self.value() == 'no':
|
|
return queryset.annotate(purchases_count=Count('purchases')).filter(purchases_count=0)
|
|
return queryset
|
|
|
|
class UserWithProspectFilter(admin.SimpleListFilter):
|
|
title = _('has prospect')
|
|
parameter_name = 'has_prospect'
|
|
|
|
def lookups(self, request, model_admin):
|
|
return (
|
|
('yes', _('Has prospect')),
|
|
('no', _('No prospect')),
|
|
)
|
|
|
|
def queryset(self, request, queryset):
|
|
from biz.models import Prospect
|
|
if self.value() == 'yes':
|
|
prospect_emails = Prospect.objects.values_list('email', flat=True).filter(email__isnull=False)
|
|
return queryset.filter(email__in=prospect_emails)
|
|
elif self.value() == 'no':
|
|
prospect_emails = Prospect.objects.values_list('email', flat=True).filter(email__isnull=False)
|
|
return queryset.exclude(email__in=prospect_emails)
|
|
return queryset
|
|
|
|
class TeamScoreRoundIndexFilter(admin.SimpleListFilter):
|
|
title = _("Round Index")
|
|
parameter_name = "round_index"
|
|
|
|
def lookups(self, request, model_admin):
|
|
# Get distinct round indexes from matches that have team scores
|
|
round_indexes = Round.objects.filter(
|
|
matches__team_scores__isnull=False
|
|
).values_list('index', flat=True).distinct().order_by('index')
|
|
|
|
# Create lookup tuples with round names
|
|
lookups = []
|
|
for index in round_indexes:
|
|
round_obj = Round.objects.filter(index=index, parent__isnull=True).first()
|
|
if round_obj:
|
|
lookups.append((index, round_obj.name()))
|
|
else:
|
|
lookups.append((index, f"Index {index}"))
|
|
return lookups
|
|
|
|
def queryset(self, request, queryset):
|
|
if self.value():
|
|
return queryset.filter(match__round__index=self.value())
|
|
return queryset
|
|
|