Clean up more sql queries

This commit is contained in:
cuom1999 2024-04-26 20:37:35 -05:00
parent 571596dcbf
commit bf5514032b
16 changed files with 356 additions and 358 deletions

View file

@ -399,7 +399,6 @@ urlpatterns = [
name="submission_status",
),
url(r"^/abort$", submission.abort_submission, name="submission_abort"),
url(r"^/html$", submission.single_submission),
]
),
),

View file

@ -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}

View file

@ -73,12 +73,7 @@ class Comment(MPTTModel):
@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())

View file

@ -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 = (

View file

@ -11,6 +11,7 @@ from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _
from judge.judgeapi import disconnect_judge
from judge.caching import cache_wrapper
__all__ = ["Language", "RuntimeVersion", "Judge"]
@ -147,14 +148,11 @@ class Language(models.Model):
@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"]
@ -162,6 +160,14 @@ class Language(models.Model):
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,

View file

@ -220,7 +220,7 @@ class Submission(models.Model):
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
@ -239,9 +239,10 @@ class Submission(models.Model):
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,

View file

@ -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):

View file

@ -130,7 +130,6 @@ class TicketFeed(HomeFeedView):
)
.order_by("-id")
.prefetch_related("linked_item")
.select_related("user__user")
)
else:
return []
@ -141,7 +140,6 @@ class TicketFeed(HomeFeedView):
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:
@ -157,7 +155,7 @@ class TicketFeed(HomeFeedView):
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):

View file

@ -438,22 +438,13 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
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

View file

@ -469,6 +469,9 @@ class SubmissionsListBase(DiggPaginatorMixin, TitleMixin, ListView):
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
@ -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()
@ -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,
},
)

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@
</div>
<div class="detail">
<div class="header">
{{ link_user(node.author, show_image=True) }}&nbsp;&nbsp;
{{ link_user(node.author_id, show_image=True) }}&nbsp;&nbsp;
{{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }}
<span class="comment-spacer"></span>
<span class="comment-operation">
@ -48,7 +48,7 @@
<i class="fa fa-link fa-fw"></i>
</a>
{% if profile and not comment_lock %}
{% set can_edit = node.author.id == profile.id and not profile.mute %}
{% set can_edit = node.author_id == profile.id and not profile.mute %}
{% if can_edit %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
<i class="fa fa-pencil fa-fw"></i>

View file

@ -5,11 +5,11 @@
{{ comment.page_title }}
</a>
</h3>
{% with author=comment.author %}
{% if author %}
{% with author_id=comment.author_id %}
{% if author_id %}
<div class="problem-feed-info-entry">
<i class="fa fa-pencil-square-o fa-fw"></i>
<span class="pi-value">{{ link_user(author) }}</span>
<span class="pi-value">{{ link_user(author_id) }}</span>
</div>
{% endif %}
{% endwith %}

View file

@ -9,7 +9,7 @@
<div class="left-sidebar">
{% if can_access %}
{{ make_tab_item('detail', 'fa fa-info-circle', url('contest_view', contest.key), _('Info')) }}
{% if contest.ended or can_edit %}
{% if (contest.ended and not contest.is_in_contest(request.user)) or can_edit %}
{{ make_tab_item('stats', 'fa fa-pie-chart', url('contest_stats', contest.key), _('Statistics')) }}
{% endif %}
{% endif %}

View file

@ -1,4 +1,4 @@
{% set can_view = submission.is_accessible_by(profile) %}
{% set can_view = submission.is_accessible_by(profile, check_contest=False) or is_in_editable_contest %}
<div class="sub-user-img user-img">
<img loading="lazy" src="{{gravatar(submission.user)}}">
</div>

View file

@ -8,20 +8,21 @@
{{ ticket.title }}
</a>
</h3>
{% with author=ticket.user %}
{% if author %}
{% with author_id = ticket.user_id %}
{% if author_id %}
<div class="problem-feed-info-entry">
<i class="fa fa-pencil-square-o fa-fw"></i>
<span class="pi-value">{{ link_user(author) }}</span>
<span class="pi-value">{{ link_user(author_id) }}</span>
</div>
{% endif %}
{% endwith %}
{% set last_message = ticket.messages.last() %}
<div class="problem-feed-types">
<i class="fa fa-tag"></i>
{{link_user(ticket.messages.last().user)}} {{_(' replied')}}
{{link_user(last_message.user_id)}} {{_('replied')}}
</div>
<div class='blog-description content-description'>
{{ ticket.messages.last().body|markdown(lazy_load=True)|reference|str|safe }}
{{ last_message.body|markdown(lazy_load=True)|reference|str|safe }}
<div class="show-more"> {{_("...More")}} </div>
</div>
</div>