From c2fffa364c1af57f036193740dc6850792e492fb Mon Sep 17 00:00:00 2001 From: jalsol Date: Sun, 14 May 2023 12:57:42 +0700 Subject: [PATCH] Use .first() instead of [:1] --- judge/comments.py | 6 +++--- judge/views/blog.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/judge/comments.py b/judge/comments.py index 1332b35e6..8d60ff841 100644 --- a/judge/comments.py +++ b/judge/comments.py @@ -75,10 +75,10 @@ def post(self, request, *args, **kwargs): return HttpResponseForbidden() if not request.user.is_superuser: - user_latest_comments = Comment.objects.filter(author=request.profile).order_by('-time')[:1] + user_latest_comment = Comment.objects.filter(author=request.profile).order_by('-time').first() - if len(user_latest_comments) > 0: - time_diff = (datetime.now(timezone.utc) - user_latest_comments[0].time).seconds + if user_latest_comment is not None: + time_diff = (datetime.now(timezone.utc) - user_latest_comment.time).seconds if time_diff < settings.VNOJ_COMMENT_COOLDOWN: remaining_minutes, remaining_seconds = divmod(settings.VNOJ_COMMENT_COOLDOWN - time_diff, 60) return HttpResponseBadRequest(_('You can only comment after {0} minutes and {1} seconds.') diff --git a/judge/views/blog.py b/judge/views/blog.py index 6e16ff451..f97bb75ef 100644 --- a/judge/views/blog.py +++ b/judge/views/blog.py @@ -310,10 +310,10 @@ def dispatch(self, request, *args, **kwargs): if not user.is_superuser: user_latest_blog = BlogPost.objects.filter(publish_on__lte=timezone.now(), authors__in=[user.profile]) \ - .order_by('-publish_on')[:1] + .order_by('-publish_on').first() - if len(user_latest_blog) > 0: - time_diff = (datetime.now(timezone.utc) - user_latest_blog[0].publish_on).seconds + if user_latest_blog is not None: + time_diff = (datetime.now(timezone.utc) - user_latest_blog.publish_on).seconds if time_diff < settings.VNOJ_BLOG_COOLDOWN: remaining_minutes, remaining_seconds = divmod(settings.VNOJ_BLOG_COOLDOWN - time_diff, 60) return HttpResponseBadRequest(_('You can only create a blog after {0} minutes and {1} seconds.')