Paginate contest summary

This commit is contained in:
cuom1999 2023-11-23 23:16:01 -06:00
parent 77b441eb5e
commit 159b2b4cc0
3 changed files with 68 additions and 46 deletions

View file

@ -518,9 +518,8 @@ urlpatterns = [
),
url(r"^contests/", paged_list_view(contests.ContestList, "contest_list")),
url(
r"^contests/summary/(?P<key>\w+)$",
contests.contests_summary_view,
name="contests_summary",
r"^contests/summary/(?P<key>\w+)/",
paged_list_view(contests.ContestsSummaryView, "contests_summary"),
),
url(r"^course/", paged_list_view(course.CourseList, "course_list")),
url(

View file

@ -1426,16 +1426,23 @@ ContestsSummaryData = namedtuple(
)
def contests_summary_view(request, key):
class ContestsSummaryView(DiggPaginatorMixin, ListView):
paginate_by = 50
template_name = "contest/contests_summary.html"
def get(self, *args, **kwargs):
try:
contests_summary = ContestsSummary.objects.get(key=key)
self.contests_summary = ContestsSummary.objects.get(key=kwargs["key"])
except:
raise Http404()
return super().get(*args, **kwargs)
cache_key = "csv:" + key
context = cache.get(cache_key)
if context:
return render(request, "contest/contests_summary.html", context)
def _get_contests_and_ranking(self):
contests_summary = self.contests_summary
cache_key = "csv:" + contests_summary.key
result = cache.get(cache_key)
if result:
return result
scores_system = contests_summary.scores
contests = contests_summary.contests.all()
@ -1445,7 +1452,7 @@ def contests_summary_view(request, key):
for i in range(len(contests)):
contest = contests[i]
users, problems = get_contest_ranking_list(request, contest)
users, problems = get_contest_ranking_list(self.request, contest)
for rank, user in users:
curr_score = 0
if rank - 1 < len(scores_system):
@ -1467,14 +1474,25 @@ def contests_summary_view(request, key):
sorted_total_points.sort(key=lambda x: x.points, reverse=True)
total_rank = ranker(sorted_total_points)
context = {
result = {
"total_rank": list(total_rank),
"title": _("Contests Summary"),
"contests": contests,
}
cache.set(cache_key, context)
cache.set(cache_key, result)
return result
return render(request, "contest/contests_summary.html", context)
def get_queryset(self):
rank_and_contests = self._get_contests_and_ranking()
total_rank = rank_and_contests["total_rank"]
self.contests = rank_and_contests["contests"]
return total_rank
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["contests"] = self.contests
context["title"] = _("Contests")
context["first_page_href"] = "."
return context
@receiver([post_save, post_delete], sender=ContestsSummary)

View file

@ -42,7 +42,7 @@
</tr>
</thead>
<tbody>
{% for rank, item in total_rank %}
{% for rank, item in page_obj %}
<tr>
<td>
{{ rank }}
@ -68,4 +68,9 @@
{% endfor %}
</tbody>
</table>
{% if page_obj and page_obj.num_pages > 1 %}
<div style="margin-top: 10px;">
{% include "list-pages.html" %}
</div>
{% endif %}
{% endblock %}