From 70d81ad664d191640eb49c1654351d8366b46410 Mon Sep 17 00:00:00 2001 From: cuom1999 Date: Thu, 9 Dec 2021 13:31:08 -0600 Subject: [PATCH] Add contest sort (DMOJ) --- judge/views/contests.py | 17 ++++++++++++----- templates/contest/list.html | 8 ++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/judge/views/contests.py b/judge/views/contests.py index d926d37..ee5f313 100644 --- a/judge/views/contests.py +++ b/judge/views/contests.py @@ -39,7 +39,8 @@ from judge.utils.opengraph import generate_opengraph from judge.utils.problems import _get_result_data from judge.utils.ranker import ranker from judge.utils.stats import get_bar_chart, get_pie_chart, get_histogram -from judge.utils.views import DiggPaginatorMixin, SingleObjectFormView, TitleMixin, generic_message +from judge.utils.views import DiggPaginatorMixin, QueryStringSortMixin, SingleObjectFormView, TitleMixin, \ + generic_message from judge.widgets import HeavyPreviewPageDownWidget __all__ = ['ContestList', 'ContestDetail', 'ContestRanking', 'ContestJoin', 'ContestLeave', 'ContestCalendar', @@ -64,12 +65,15 @@ class ContestListMixin(object): return Contest.get_visible_contests(self.request.user) -class ContestList(DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): +class ContestList(QueryStringSortMixin, DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): model = Contest paginate_by = 20 template_name = 'contest/list.html' title = gettext_lazy('Contests') context_object_name = 'past_contests' + all_sorts = frozenset(('name', 'user_count', 'start_time')) + default_desc = frozenset(('name', 'user_count')) + default_sort = '-start_time' first_page_href = None @cached_property @@ -90,7 +94,7 @@ class ContestList(DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): def _get_queryset(self): queryset = super(ContestList, self).get_queryset() \ - .order_by('-start_time', 'key').prefetch_related('tags', 'organizations', 'authors', 'curators', 'testers') + .prefetch_related('tags', 'organizations', 'authors', 'curators', 'testers') if 'contest' in self.request.GET: self.contest_query = query = ' '.join(self.request.GET.getlist('contest')).strip() @@ -103,7 +107,7 @@ class ContestList(DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): return queryset def get_queryset(self): - return self._get_queryset().filter(end_time__lt=self._now) + return self._get_queryset().order_by(self.order, 'key').filter(end_time__lt=self._now) def get_context_data(self, **kwargs): context = super(ContestList, self).get_context_data(**kwargs) @@ -124,7 +128,8 @@ class ContestList(DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): active.append(participation) present.remove(participation.contest) - active.sort(key=attrgetter('end_time')) + active.sort(key=attrgetter('end_time', 'key')) + present.sort(key=attrgetter('end_time', 'key')) future.sort(key=attrgetter('start_time')) context['active_participations'] = active context['current_contests'] = present @@ -137,6 +142,8 @@ class ContestList(DiggPaginatorMixin, TitleMixin, ContestListMixin, ListView): context['page_suffix'] = suffix = ( '?' + self.request.GET.urlencode()) if self.request.GET else '' context['first_page_href'] = (self.first_page_href or '.') + suffix + context.update(self.get_sort_context()) + context.update(self.get_sort_paginate_context()) return context diff --git a/templates/contest/list.html b/templates/contest/list.html index 9cfbc5c..2b7abb5 100644 --- a/templates/contest/list.html +++ b/templates/contest/list.html @@ -362,8 +362,12 @@ - - + + {% if not request.in_contest %} {% endif %}
{{ _('Contest') }}{{ _('Users') }} + {{ _('Contest') }}{{ sort_order.name }} + + {{ _('Users') }}{{ sort_order.user_count }} +