NDOJ/judge/views/internal.py

48 lines
1.5 KiB
Python
Raw Normal View History

2022-05-03 02:44:14 +00:00
from django.views.generic import ListView
from django.utils.translation import gettext as _, gettext_lazy
from django.db.models import Count
from django.http import HttpResponseForbidden
from judge.utils.diggpaginator import DiggPaginator
from judge.models import VolunteerProblemVote, Problem
2022-05-14 17:57:27 +00:00
2022-05-03 02:44:14 +00:00
class InternalProblem(ListView):
model = Problem
2022-05-14 17:57:27 +00:00
title = _("Internal problems")
template_name = "internal/base.html"
2022-05-03 02:44:14 +00:00
paginate_by = 100
2022-05-14 17:57:27 +00:00
context_object_name = "problems"
def get_paginator(
self, queryset, per_page, orphans=0, allow_empty_first_page=True, **kwargs
):
return DiggPaginator(
queryset,
per_page,
body=6,
padding=2,
orphans=orphans,
allow_empty_first_page=allow_empty_first_page,
**kwargs
)
2022-05-03 02:44:14 +00:00
def get_queryset(self):
2022-05-14 17:57:27 +00:00
queryset = (
Problem.objects.annotate(vote_count=Count("volunteer_user_votes"))
.filter(vote_count__gte=1)
.order_by("-vote_count")
)
2022-05-03 02:44:14 +00:00
return queryset
def get_context_data(self, **kwargs):
context = super(InternalProblem, self).get_context_data(**kwargs)
2022-05-14 17:57:27 +00:00
context["page_type"] = "problem"
context["title"] = self.title
2022-05-03 02:44:14 +00:00
return context
def get(self, request, *args, **kwargs):
if request.user.is_superuser:
return super(InternalProblem, self).get(request, *args, **kwargs)
2022-05-14 17:57:27 +00:00
return HttpResponseForbidden()