throttle submissions

pull/1/head
nathaniel smith 2016-12-19 15:22:06 -08:00
parent 7516e6736c
commit 50d34f28e8
2 changed files with 20 additions and 1 deletions

View File

@ -1,10 +1,25 @@
from datetime import datetime, timedelta
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms import Form, CharField, EmailField, Textarea, ChoiceField from django.forms import Form, CharField, EmailField, Textarea, ChoiceField
from common.forms import CaptchaField from common.forms import CaptchaField
from .models import ISSUE_TYPE_CHOICES from .models import ISSUE_TYPE_CHOICES
# this should go in something like redis. I refuse, however, to involve redis
# in all of this until i have 2-3 more usecases.
submission_throttle = {}
def throttle_submission(email):
last_submission = submission_throttle.get(email)
now = datetime.now()
if last_submission is None\
or now - last_submission > timedelta(minutes=30):
submission_throttle[email] = datetime.now()
else:
raise ValidationError('you have submitted pretty recently. try again in a bit.')
def validate_issue_text(text): def validate_issue_text(text):
if len(text) == 0: if len(text) == 0:
raise ValidationError('please describe yr issue') raise ValidationError('please describe yr issue')
@ -35,6 +50,9 @@ class TicketForm(Form):
def clean(self): def clean(self):
result = super().clean() result = super().clean()
throttle_submission(result['email'])
if self.errors: if self.errors:
raise ValidationError('oops, looks like there were some problems below.') raise ValidationError('oops, looks like there were some problems below.')

View File

@ -14,6 +14,7 @@ USERNAME_MIN_LENGTH = 4
DISPLAY_NAME_RE = re.compile(r"[a-zA-Z0-9_\-']+") DISPLAY_NAME_RE = re.compile(r"[a-zA-Z0-9_\-']+")
DISPLAY_MIN_LENGTH = 2 DISPLAY_MIN_LENGTH = 2
def validate_username(username): def validate_username(username):
if len(username) < USERNAME_MIN_LENGTH: if len(username) < USERNAME_MIN_LENGTH:
raise ValidationError('Username too short.') raise ValidationError('Username too short.')