Skip to content

Commit

Permalink
Set cooldown for non-superusers between blogs
Browse files Browse the repository at this point in the history
  • Loading branch information
jalsol committed Apr 10, 2023
1 parent 3d9e8d1 commit d65ede2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions dmoj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
# Cooldown time (minutes) between each comment
VNOJ_COMMENT_COOLDOWN = 10

# Cooldown time (minutes) between each blog post
VNOJ_BLOG_COOLDOWN = 3 * 60

# Some problems have a lot of testcases, and each testcase
# has about 5~6 fields, so we need to raise this
DATA_UPLOAD_MAX_NUMBER_FIELDS = 3000
Expand Down
20 changes: 18 additions & 2 deletions judge/views/blog.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.exceptions import PermissionDenied
Expand Down Expand Up @@ -295,13 +297,27 @@ def form_valid(self, form):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
raise PermissionDenied()

user = request.user

# hasattr(self, 'organization') -> admin org
if request.official_contest_mode or request.user.profile.problem_count < settings.VNOJ_BLOG_MIN_PROBLEM_COUNT \
and not request.user.is_superuser and not hasattr(self, 'organization'):
if request.official_contest_mode or user.profile.problem_count < settings.VNOJ_BLOG_MIN_PROBLEM_COUNT \
and not user.is_superuser and not hasattr(self, 'organization'):
return generic_message(request, _('Permission denied'),
_('You cannot create blog post.\n'
'Note: You need to solve at least %d problems to create new blog post.')
% settings.VNOJ_BLOG_MIN_PROBLEM_COUNT)

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]

if len(user_latest_blog) > 0:
time_diff = (datetime.now(timezone.utc) - user_latest_blog[0].publish_on).seconds
if time_diff < settings.VNOJ_BLOG_COOLDOWN * 60:
return HttpResponseBadRequest(_('You can only create a blog after {0} minutes '
'since your latest blog')
.format(settings.VNOJ_BLOG_COOLDOWN), content_type='text/plain')
return super().dispatch(request, *args, **kwargs)


Expand Down

0 comments on commit d65ede2

Please sign in to comment.