Skip to content

Commit c2fffa3

Browse files
jalsolhieplpvip
authored andcommitted
Use .first() instead of [:1]
1 parent 51c0450 commit c2fffa3

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

judge/comments.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ def post(self, request, *args, **kwargs):
7575
return HttpResponseForbidden()
7676

7777
if not request.user.is_superuser:
78-
user_latest_comments = Comment.objects.filter(author=request.profile).order_by('-time')[:1]
78+
user_latest_comment = Comment.objects.filter(author=request.profile).order_by('-time').first()
7979

80-
if len(user_latest_comments) > 0:
81-
time_diff = (datetime.now(timezone.utc) - user_latest_comments[0].time).seconds
80+
if user_latest_comment is not None:
81+
time_diff = (datetime.now(timezone.utc) - user_latest_comment.time).seconds
8282
if time_diff < settings.VNOJ_COMMENT_COOLDOWN:
8383
remaining_minutes, remaining_seconds = divmod(settings.VNOJ_COMMENT_COOLDOWN - time_diff, 60)
8484
return HttpResponseBadRequest(_('You can only comment after {0} minutes and {1} seconds.')

judge/views/blog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ def dispatch(self, request, *args, **kwargs):
310310

311311
if not user.is_superuser:
312312
user_latest_blog = BlogPost.objects.filter(publish_on__lte=timezone.now(), authors__in=[user.profile]) \
313-
.order_by('-publish_on')[:1]
313+
.order_by('-publish_on').first()
314314

315-
if len(user_latest_blog) > 0:
316-
time_diff = (datetime.now(timezone.utc) - user_latest_blog[0].publish_on).seconds
315+
if user_latest_blog is not None:
316+
time_diff = (datetime.now(timezone.utc) - user_latest_blog.publish_on).seconds
317317
if time_diff < settings.VNOJ_BLOG_COOLDOWN:
318318
remaining_minutes, remaining_seconds = divmod(settings.VNOJ_BLOG_COOLDOWN - time_diff, 60)
319319
return HttpResponseBadRequest(_('You can only create a blog after {0} minutes and {1} seconds.')

0 commit comments

Comments
 (0)