Skip to content

Commit

Permalink
Use .first() instead of [:1]
Browse files Browse the repository at this point in the history
  • Loading branch information
jalsol authored and hieplpvip committed May 14, 2023
1 parent 51c0450 commit c2fffa3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions judge/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
6 changes: 3 additions & 3 deletions judge/views/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down

0 comments on commit c2fffa3

Please sign in to comment.