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", name="submission_status",
), ),
url(r"^/abort$", submission.abort_submission, name="submission_abort"), 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 profile = user
elif isinstance(user, AbstractUser): elif isinstance(user, AbstractUser):
profile = user.profile profile = user.profile
elif isinstance(user, int):
profile = Profile(id=user)
else: else:
raise ValueError("Expected profile or user, got %s" % (type(user),)) raise ValueError("Expected profile or user, got %s" % (type(user),))
return {"profile": profile, "show_image": show_image} return {"profile": profile, "show_image": show_image}

View file

@ -73,12 +73,7 @@ class Comment(MPTTModel):
@classmethod @classmethod
def most_recent(cls, user, n, batch=None, organization=None): def most_recent(cls, user, n, batch=None, organization=None):
queryset = ( queryset = cls.objects.filter(hidden=False).order_by("-id")
cls.objects.filter(hidden=False)
.select_related("author__user")
.defer("author__about")
.order_by("-id")
)
if organization: if organization:
queryset = queryset.filter(author__in=organization.members.all()) queryset = queryset.filter(author__in=organization.members.all())

View file

@ -547,15 +547,6 @@ def on_user_save(sender, instance, **kwargs):
pass 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") @cache_wrapper(prefix="Pgbi3")
def _get_basic_info(profile_id): def _get_basic_info(profile_id):
profile = ( profile = (

View file

@ -11,6 +11,7 @@ from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from judge.judgeapi import disconnect_judge from judge.judgeapi import disconnect_judge
from judge.caching import cache_wrapper
__all__ = ["Language", "RuntimeVersion", "Judge"] __all__ = ["Language", "RuntimeVersion", "Judge"]
@ -147,14 +148,11 @@ class Language(models.Model):
@classmethod @classmethod
def get_default_language(cls): def get_default_language(cls):
try: return _get_default_language()
return Language.objects.get(key=settings.DEFAULT_USER_LANGUAGE)
except Language.DoesNotExist:
return cls.get_python3()
@classmethod @classmethod
def get_default_language_pk(cls): def get_default_language_pk(cls):
return cls.get_default_language().pk return _get_default_language().pk
class Meta: class Meta:
ordering = ["key"] ordering = ["key"]
@ -162,6 +160,14 @@ class Language(models.Model):
verbose_name_plural = _("languages") 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): class RuntimeVersion(models.Model):
language = models.ForeignKey( language = models.ForeignKey(
Language, Language,

View file

@ -220,7 +220,7 @@ class Submission(models.Model):
def id_secret(self): def id_secret(self):
return self.get_id_secret(self.id) 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: if not profile:
return False return False
@ -239,6 +239,7 @@ class Submission(models.Model):
if self.problem.is_public and user.has_perm("judge.view_public_submission"): if self.problem.is_public and user.has_perm("judge.view_public_submission"):
return True return True
if check_contest:
contest = self.contest_object contest = self.contest_object
if contest and contest.is_editable_by(user): if contest and contest.is_editable_by(user):
return True return True

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) @receiver(post_save, sender=Contest)
def contest_update(sender, instance, **kwargs): def contest_update(sender, instance, **kwargs):

View file

@ -130,7 +130,6 @@ class TicketFeed(HomeFeedView):
) )
.order_by("-id") .order_by("-id")
.prefetch_related("linked_item") .prefetch_related("linked_item")
.select_related("user__user")
) )
else: else:
return [] return []
@ -141,7 +140,6 @@ class TicketFeed(HomeFeedView):
Ticket.objects.order_by("-id") Ticket.objects.order_by("-id")
.filter(is_open=True) .filter(is_open=True)
.prefetch_related("linked_item") .prefetch_related("linked_item")
.select_related("user__user")
) )
return filter_visible_tickets(tickets, self.request.user, profile) return filter_visible_tickets(tickets, self.request.user, profile)
else: else:
@ -157,7 +155,7 @@ class TicketFeed(HomeFeedView):
class CommentFeed(HomeFeedView): class CommentFeed(HomeFeedView):
model = Comment model = Comment
context_object_name = "comments" context_object_name = "comments"
paginate_by = 15 paginate_by = 10
feed_content_template_name = "comments/feed.html" feed_content_template_name = "comments/feed.html"
def get_queryset(self): def get_queryset(self):

View file

@ -438,22 +438,13 @@ class CommentedDetailView(TemplateResponseMixin, SingleObjectMixin, View):
def _get_queryset(self, target_comment): def _get_queryset(self, target_comment):
if target_comment: if target_comment:
queryset = target_comment.get_descendants(include_self=True) queryset = target_comment.get_descendants(include_self=True)
queryset = ( queryset = queryset.filter(hidden=False)
queryset.select_related("author__user")
.filter(hidden=False)
.defer("author__about")
)
else: else:
queryset = self.object.comments queryset = self.object.comments
queryset = queryset.filter(parent=None, hidden=False) queryset = queryset.filter(parent=None, hidden=False)
queryset = ( queryset = queryset.filter(hidden=False).annotate(
queryset.select_related("author__user")
.defer("author__about")
.filter(hidden=False)
.annotate(
count_replies=Count("replies", distinct=True), count_replies=Count("replies", distinct=True),
)[:DEFAULT_OFFSET] )[:DEFAULT_OFFSET]
)
if self.request.user.is_authenticated: if self.request.user.is_authenticated:
profile = self.request.profile profile = self.request.profile

View file

@ -469,6 +469,9 @@ class SubmissionsListBase(DiggPaginatorMixin, TitleMixin, ListView):
if context["in_hidden_subtasks_contest"]: if context["in_hidden_subtasks_contest"]:
for submission in context["submissions"]: for submission in context["submissions"]:
self.modify_attrs(submission) self.modify_attrs(submission)
context[
"is_in_editable_contest"
] = self.in_contest and self.contest.is_editable_by(self.request.user)
return context return context
@ -721,6 +724,11 @@ def single_submission(request, submission_id, show_problem=True):
submission_related(Submission.objects.all()), id=int(submission_id) 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): if not submission.problem.is_accessible_by(request.user):
raise Http404() raise Http404()
@ -733,6 +741,7 @@ def single_submission(request, submission_id, show_problem=True):
"problem_name": show_problem "problem_name": show_problem
and submission.problem.translated_name(request.LANGUAGE_CODE), and submission.problem.translated_name(request.LANGUAGE_CODE),
"profile": request.profile if authenticated else None, "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>
<div class="detail"> <div class="detail">
<div class="header"> <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}')) }} {{ relative_time(node.time, abs=_('{time}'), rel=_('{time}')) }}
<span class="comment-spacer"></span> <span class="comment-spacer"></span>
<span class="comment-operation"> <span class="comment-operation">
@ -48,7 +48,7 @@
<i class="fa fa-link fa-fw"></i> <i class="fa fa-link fa-fw"></i>
</a> </a>
{% if profile and not comment_lock %} {% 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 %} {% if can_edit %}
<a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link"> <a data-featherlight="{{ url('comment_edit_ajax', node.id) }}" href="#" title="{{ _('Edit') }}" class="edit-link">
<i class="fa fa-pencil fa-fw"></i> <i class="fa fa-pencil fa-fw"></i>

View file

@ -5,11 +5,11 @@
{{ comment.page_title }} {{ comment.page_title }}
</a> </a>
</h3> </h3>
{% with author=comment.author %} {% with author_id=comment.author_id %}
{% if author %} {% if author_id %}
<div class="problem-feed-info-entry"> <div class="problem-feed-info-entry">
<i class="fa fa-pencil-square-o fa-fw"></i> <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> </div>
{% endif %} {% endif %}
{% endwith %} {% endwith %}

View file

@ -9,7 +9,7 @@
<div class="left-sidebar"> <div class="left-sidebar">
{% if can_access %} {% if can_access %}
{{ make_tab_item('detail', 'fa fa-info-circle', url('contest_view', contest.key), _('Info')) }} {{ 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')) }} {{ make_tab_item('stats', 'fa fa-pie-chart', url('contest_stats', contest.key), _('Statistics')) }}
{% endif %} {% endif %}
{% 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"> <div class="sub-user-img user-img">
<img loading="lazy" src="{{gravatar(submission.user)}}"> <img loading="lazy" src="{{gravatar(submission.user)}}">
</div> </div>

View file

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