Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cooldown time between blogs/comments creation #297

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dmoj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@

VNOJ_TAG_PROBLEM_MIN_RATING = 1900 # Minimum rating to be able to tag a problem

# Cooldown time (seconds) between each comment
VNOJ_COMMENT_COOLDOWN = 60

# Cooldown time (seconds) between each blog post
VNOJ_BLOG_COOLDOWN = 3 * 60 * 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
13 changes: 13 additions & 0 deletions judge/comments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from django import forms
from django.conf import settings
from django.contrib.auth.decorators import login_required
Expand Down Expand Up @@ -72,6 +74,17 @@ def post(self, request, *args, **kwargs):
if self.is_comment_locked():
return HttpResponseForbidden()

if not request.user.is_superuser:
user_latest_comment = Comment.objects.filter(author=request.profile).order_by('-time').first()

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.')
.format(remaining_minutes, remaining_seconds),
content_type='text/plain')

parent = request.POST.get('parent')
if parent:
if len(parent) > 10:
Expand Down
21 changes: 19 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,28 @@ 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').first()

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.')
.format(remaining_minutes, remaining_seconds),
content_type='text/plain')
return super().dispatch(request, *args, **kwargs)


Expand Down