Skip to content

Commit

Permalink
(no-ticket) Optimize some redundant db queries in views
Browse files Browse the repository at this point in the history
Change-Id: Ib9500d17e370cf361b616174bc85c4a9477c96c7
  • Loading branch information
teqwve authored and DietPawel committed Aug 23, 2023
1 parent 517cccd commit 85ddc74
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
12 changes: 8 additions & 4 deletions oioioi/clock/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from oioioi.base.utils import request_cached
from oioioi.base.utils.redirect import safe_redirect
from oioioi.contests.models import Round
from oioioi.status.registry import status_registry
from oioioi.su.utils import is_real_superuser


@request_cached
def get_round_times(request):
contest = getattr(request, 'contest', None)
return [(contest.controller.get_round_times(request, round), round)
for round in Round.objects.filter(contest=contest)]

@status_registry.register
def get_times_status(request, response):
"""Extends the response dictionary with rounds times.
Expand Down Expand Up @@ -46,10 +53,7 @@ def get_times_status(request, response):
current_rounds_times = None

if timestamp and contest:
rtimes = [
(contest.controller.get_round_times(request, round), round)
for round in Round.objects.filter(contest=contest)
]
rtimes = get_round_times(request)
next_rounds_times = [
(rt, round) for (rt, round) in rtimes if rt.is_future(timestamp)
]
Expand Down
15 changes: 6 additions & 9 deletions oioioi/problems/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ def dangling_problems_processor(request):
if not is_contest_basicadmin(request):
return {}

dangling_pis = ProblemInstance.objects.filter(contest=request.contest,
round__isnull=True)
count = dangling_pis.count()
def generator():
dangling_pis = ProblemInstance.objects.filter(
contest=request.contest,
round__isnull=True,
)
count = dangling_pis.count()
if not count:
return ''
elif count == 1:
Expand Down Expand Up @@ -53,11 +51,10 @@ def problems_need_rejudge_processor(request):
if not is_contest_basicadmin(request):
return {}

pis = ProblemInstance.objects.filter(contest=request.contest,
needs_rejudge=True)
count = pis.count()
def generator():
pis = ProblemInstance.objects.filter(
contest=request.contest, needs_rejudge=True
)
count = pis.count()
if not count:
return ''
else:
Expand Down
3 changes: 2 additions & 1 deletion oioioi/questions/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.functional import lazy
from django.utils.translation import ngettext

from oioioi.base.utils import make_navbar_badge
from oioioi.base.utils import make_navbar_badge, request_cached
from oioioi.contests.utils import can_enter_contest, is_contest_basicadmin
from oioioi.questions.utils import unanswered_questions
from oioioi.questions.views import new_messages, visible_messages
Expand All @@ -29,6 +29,7 @@ def get_messages(request, response):
return response


@request_cached
def navbar_messages_generator(request):
if request.contest is None:
return {}
Expand Down
3 changes: 3 additions & 0 deletions oioioi/statistics/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.template.loader import render_to_string

from oioioi.base.utils import request_cached
from oioioi.base.permissions import make_request_condition
from oioioi.programs.controllers import ProgrammingContestController

Expand Down Expand Up @@ -30,6 +31,7 @@ def render_head(requirements):


@make_request_condition
@request_cached
def any_statistics_avaiable(request):
controller = request.contest.controller
if not isinstance(controller, ProgrammingContestController):
Expand All @@ -38,5 +40,6 @@ def any_statistics_avaiable(request):


@make_request_condition
@request_cached
def can_see_stats(request):
return request.contest.controller.can_see_stats(request)

0 comments on commit 85ddc74

Please sign in to comment.