From 50d34f28e8b1f9b85b52fb684ae017b2c427adbf Mon Sep 17 00:00:00 2001 From: nathaniel smith Date: Mon, 19 Dec 2016 15:22:06 -0800 Subject: [PATCH] throttle submissions --- ttadmin/help/forms.py | 20 +++++++++++++++++++- ttadmin/users/forms.py | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/ttadmin/help/forms.py b/ttadmin/help/forms.py index a043fbb..760f7a5 100644 --- a/ttadmin/help/forms.py +++ b/ttadmin/help/forms.py @@ -1,10 +1,25 @@ +from datetime import datetime, timedelta + from django.core.exceptions import ValidationError from django.forms import Form, CharField, EmailField, Textarea, ChoiceField from common.forms import CaptchaField - 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): if len(text) == 0: raise ValidationError('please describe yr issue') @@ -35,6 +50,9 @@ class TicketForm(Form): def clean(self): result = super().clean() + + throttle_submission(result['email']) + if self.errors: raise ValidationError('oops, looks like there were some problems below.') diff --git a/ttadmin/users/forms.py b/ttadmin/users/forms.py index 2efeead..d7123bb 100644 --- a/ttadmin/users/forms.py +++ b/ttadmin/users/forms.py @@ -14,6 +14,7 @@ USERNAME_MIN_LENGTH = 4 DISPLAY_NAME_RE = re.compile(r"[a-zA-Z0-9_\-']+") DISPLAY_MIN_LENGTH = 2 + def validate_username(username): if len(username) < USERNAME_MIN_LENGTH: raise ValidationError('Username too short.')