diff --git a/dmoj/settings.py b/dmoj/settings.py index afd870930..5c35440e5 100755 --- a/dmoj/settings.py +++ b/dmoj/settings.py @@ -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 diff --git a/judge/comments.py b/judge/comments.py index 4e8d88dcc..8d60ff841 100644 --- a/judge/comments.py +++ b/judge/comments.py @@ -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 @@ -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: diff --git a/judge/views/blog.py b/judge/views/blog.py index 4b25b6ec6..f97bb75ef 100644 --- a/judge/views/blog.py +++ b/judge/views/blog.py @@ -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 @@ -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)