Paginate contest summary
This commit is contained in:
parent
77b441eb5e
commit
159b2b4cc0
3 changed files with 68 additions and 46 deletions
|
@ -518,9 +518,8 @@ urlpatterns = [
|
||||||
),
|
),
|
||||||
url(r"^contests/", paged_list_view(contests.ContestList, "contest_list")),
|
url(r"^contests/", paged_list_view(contests.ContestList, "contest_list")),
|
||||||
url(
|
url(
|
||||||
r"^contests/summary/(?P<key>\w+)$",
|
r"^contests/summary/(?P<key>\w+)/",
|
||||||
contests.contests_summary_view,
|
paged_list_view(contests.ContestsSummaryView, "contests_summary"),
|
||||||
name="contests_summary",
|
|
||||||
),
|
),
|
||||||
url(r"^course/", paged_list_view(course.CourseList, "course_list")),
|
url(r"^course/", paged_list_view(course.CourseList, "course_list")),
|
||||||
url(
|
url(
|
||||||
|
|
|
@ -1426,55 +1426,73 @@ ContestsSummaryData = namedtuple(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def contests_summary_view(request, key):
|
class ContestsSummaryView(DiggPaginatorMixin, ListView):
|
||||||
try:
|
paginate_by = 50
|
||||||
contests_summary = ContestsSummary.objects.get(key=key)
|
template_name = "contest/contests_summary.html"
|
||||||
except:
|
|
||||||
raise Http404()
|
|
||||||
|
|
||||||
cache_key = "csv:" + key
|
def get(self, *args, **kwargs):
|
||||||
context = cache.get(cache_key)
|
try:
|
||||||
if context:
|
self.contests_summary = ContestsSummary.objects.get(key=kwargs["key"])
|
||||||
return render(request, "contest/contests_summary.html", context)
|
except:
|
||||||
|
raise Http404()
|
||||||
|
return super().get(*args, **kwargs)
|
||||||
|
|
||||||
scores_system = contests_summary.scores
|
def _get_contests_and_ranking(self):
|
||||||
contests = contests_summary.contests.all()
|
contests_summary = self.contests_summary
|
||||||
total_points = defaultdict(int)
|
cache_key = "csv:" + contests_summary.key
|
||||||
result_per_contest = defaultdict(lambda: [(0, 0)] * len(contests))
|
result = cache.get(cache_key)
|
||||||
user_css_class = {}
|
if result:
|
||||||
|
return result
|
||||||
|
|
||||||
for i in range(len(contests)):
|
scores_system = contests_summary.scores
|
||||||
contest = contests[i]
|
contests = contests_summary.contests.all()
|
||||||
users, problems = get_contest_ranking_list(request, contest)
|
total_points = defaultdict(int)
|
||||||
for rank, user in users:
|
result_per_contest = defaultdict(lambda: [(0, 0)] * len(contests))
|
||||||
curr_score = 0
|
user_css_class = {}
|
||||||
if rank - 1 < len(scores_system):
|
|
||||||
curr_score = scores_system[rank - 1]
|
|
||||||
total_points[user.user] += curr_score
|
|
||||||
result_per_contest[user.user][i] = (curr_score, rank)
|
|
||||||
user_css_class[user.user] = user.css_class
|
|
||||||
|
|
||||||
sorted_total_points = [
|
for i in range(len(contests)):
|
||||||
ContestsSummaryData(
|
contest = contests[i]
|
||||||
user=user,
|
users, problems = get_contest_ranking_list(self.request, contest)
|
||||||
points=total_points[user],
|
for rank, user in users:
|
||||||
point_contests=result_per_contest[user],
|
curr_score = 0
|
||||||
css_class=user_css_class[user],
|
if rank - 1 < len(scores_system):
|
||||||
)
|
curr_score = scores_system[rank - 1]
|
||||||
for user in total_points
|
total_points[user.user] += curr_score
|
||||||
]
|
result_per_contest[user.user][i] = (curr_score, rank)
|
||||||
|
user_css_class[user.user] = user.css_class
|
||||||
|
|
||||||
sorted_total_points.sort(key=lambda x: x.points, reverse=True)
|
sorted_total_points = [
|
||||||
total_rank = ranker(sorted_total_points)
|
ContestsSummaryData(
|
||||||
|
user=user,
|
||||||
|
points=total_points[user],
|
||||||
|
point_contests=result_per_contest[user],
|
||||||
|
css_class=user_css_class[user],
|
||||||
|
)
|
||||||
|
for user in total_points
|
||||||
|
]
|
||||||
|
|
||||||
context = {
|
sorted_total_points.sort(key=lambda x: x.points, reverse=True)
|
||||||
"total_rank": list(total_rank),
|
total_rank = ranker(sorted_total_points)
|
||||||
"title": _("Contests Summary"),
|
|
||||||
"contests": contests,
|
|
||||||
}
|
|
||||||
cache.set(cache_key, context)
|
|
||||||
|
|
||||||
return render(request, "contest/contests_summary.html", context)
|
result = {
|
||||||
|
"total_rank": list(total_rank),
|
||||||
|
"contests": contests,
|
||||||
|
}
|
||||||
|
cache.set(cache_key, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
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)
|
@receiver([post_save, post_delete], sender=ContestsSummary)
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for rank, item in total_rank %}
|
{% for rank, item in page_obj %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
{{ rank }}
|
{{ rank }}
|
||||||
|
@ -68,4 +68,9 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
{% if page_obj and page_obj.num_pages > 1 %}
|
||||||
|
<div style="margin-top: 10px;">
|
||||||
|
{% include "list-pages.html" %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in a new issue