Skip to content

Commit

Permalink
Clean up more sql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
cuom1999 committed Apr 27, 2024
1 parent 571596d commit bf55140
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 358 deletions.
1 change: 0 additions & 1 deletion dmoj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ def paged_list_view(view, name, **kwargs):
name="submission_status",
),
url(r"^/abort$", submission.abort_submission, name="submission_abort"),
url(r"^/html$", submission.single_submission),
]
),
),
Expand Down
2 changes: 2 additions & 0 deletions judge/jinja2/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ def link_user(user, show_image=False):
profile = user
elif isinstance(user, AbstractUser):
profile = user.profile
elif isinstance(user, int):
profile = Profile(id=user)
else:
raise ValueError("Expected profile or user, got %s" % (type(user),))
return {"profile": profile, "show_image": show_image}
Expand Down
7 changes: 1 addition & 6 deletions judge/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ class MPTTMeta:

@classmethod
def most_recent(cls, user, n, batch=None, organization=None):
queryset = (
cls.objects.filter(hidden=False)
.select_related("author__user")
.defer("author__about")
.order_by("-id")
)
queryset = cls.objects.filter(hidden=False).order_by("-id")

if organization:
queryset = queryset.filter(author__in=organization.members.all())
Expand Down
9 changes: 0 additions & 9 deletions judge/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,15 +547,6 @@ def on_user_save(sender, instance, **kwargs):
pass


@receiver([pre_save], sender=Profile)
def on_profile_save(sender, instance, **kwargs):
if instance.id is None:
return
prev = sender.objects.get(id=instance.id)
if prev.mute != instance.mute or prev.profile_image != instance.profile_image:
_get_basic_info.dirty(instance.id)


@cache_wrapper(prefix="Pgbi3")
def _get_basic_info(profile_id):
profile = (
Expand Down
16 changes: 11 additions & 5 deletions judge/models/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.translation import gettext_lazy as _

from judge.judgeapi import disconnect_judge
from judge.caching import cache_wrapper

__all__ = ["Language", "RuntimeVersion", "Judge"]

Expand Down Expand Up @@ -147,21 +148,26 @@ def get_absolute_url(self):

@classmethod
def get_default_language(cls):
try:
return Language.objects.get(key=settings.DEFAULT_USER_LANGUAGE)
except Language.DoesNotExist:
return cls.get_python3()
return _get_default_language()

@classmethod
def get_default_language_pk(cls):
return cls.get_default_language().pk
return _get_default_language().pk

class Meta:
ordering = ["key"]
verbose_name = _("language")
verbose_name_plural = _("languages")


@cache_wrapper(prefix="gdl")
def _get_default_language():
try:
return Language.objects.get(key=settings.DEFAULT_USER_LANGUAGE)
except Language.DoesNotExist:
return cls.get_python3()


class RuntimeVersion(models.Model):
language = models.ForeignKey(
Language,
Expand Down
9 changes: 5 additions & 4 deletions judge/models/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def get_id_secret(cls, sub_id):
def id_secret(self):
return self.get_id_secret(self.id)

def is_accessible_by(self, profile):
def is_accessible_by(self, profile, check_contest=True):
if not profile:
return False

Expand All @@ -239,9 +239,10 @@ def is_accessible_by(self, profile):
if self.problem.is_public and user.has_perm("judge.view_public_submission"):
return True

contest = self.contest_object
if contest and contest.is_editable_by(user):
return True
if check_contest:
contest = self.contest_object
if contest and contest.is_editable_by(user):
return True

from judge.utils.problems import (
user_completed_ids,
Expand Down
2 changes: 2 additions & 0 deletions judge/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def profile_update(sender, instance, **kwargs):
]
)

judge.models.profile._get_basic_info.dirty(instance.id)


@receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs):
Expand Down
4 changes: 1 addition & 3 deletions judge/views/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def get_queryset(self, is_own=True):
)
.order_by("-id")
.prefetch_related("linked_item")
.select_related("user__user")
)
else:
return []
Expand All @@ -141,7 +140,6 @@ def get_queryset(self, is_own=True):
Ticket.objects.order_by("-id")
.filter(is_open=True)
.prefetch_related("linked_item")
.select_related("user__user")
)
return filter_visible_tickets(tickets, self.request.user, profile)
else:
Expand All @@ -157,7 +155,7 @@ def get_context_data(self, **kwargs):
class CommentFeed(HomeFeedView):
model = Comment
context_object_name = "comments"
paginate_by = 15
paginate_by = 10
feed_content_template_name = "comments/feed.html"

def get_queryset(self):
Expand Down
17 changes: 4 additions & 13 deletions judge/views/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,22 +438,13 @@ def get(self, request, *args, **kwargs):
def _get_queryset(self, target_comment):
if target_comment:
queryset = target_comment.get_descendants(include_self=True)
queryset = (
queryset.select_related("author__user")
.filter(hidden=False)
.defer("author__about")
)
queryset = queryset.filter(hidden=False)
else:
queryset = self.object.comments
queryset = queryset.filter(parent=None, hidden=False)
queryset = (
queryset.select_related("author__user")
.defer("author__about")
.filter(hidden=False)
.annotate(
count_replies=Count("replies", distinct=True),
)[:DEFAULT_OFFSET]
)
queryset = queryset.filter(hidden=False).annotate(
count_replies=Count("replies", distinct=True),
)[:DEFAULT_OFFSET]

if self.request.user.is_authenticated:
profile = self.request.profile
Expand Down
9 changes: 9 additions & 0 deletions judge/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ def get_context_data(self, **kwargs):
if context["in_hidden_subtasks_contest"]:
for submission in context["submissions"]:
self.modify_attrs(submission)
context[
"is_in_editable_contest"
] = self.in_contest and self.contest.is_editable_by(self.request.user)

return context

Expand Down Expand Up @@ -721,6 +724,11 @@ def single_submission(request, submission_id, show_problem=True):
submission_related(Submission.objects.all()), id=int(submission_id)
)

is_in_editable_contest = False
if authenticated and request.in_contest_mode:
contest = request.profile.current_contest.contest
is_in_editable_contest = contest.is_editable_by(request.user)

if not submission.problem.is_accessible_by(request.user):
raise Http404()

Expand All @@ -733,6 +741,7 @@ def single_submission(request, submission_id, show_problem=True):
"problem_name": show_problem
and submission.problem.translated_name(request.LANGUAGE_CODE),
"profile": request.profile if authenticated else None,
"is_in_editable_contest": is_in_editable_contest,
},
)

Expand Down
Loading

0 comments on commit bf55140

Please sign in to comment.