tildetown-admin/ttadmin/guestbook/forms.py

38 lines
920 B
Python
Raw Normal View History

2016-12-20 06:00:00 +00:00
from datetime import datetime, timedelta
from django.core.exceptions import ValidationError
from django.forms import Form, CharField, EmailField, Textarea, ChoiceField
2016-12-20 07:03:36 +00:00
from common.forms import CaptchaField, throttler
2016-12-20 06:00:00 +00:00
submission_throttle = {}
2016-12-20 07:03:36 +00:00
throttle_submission = throttler(submission_throttle)
2016-12-20 06:00:00 +00:00
def validate_msg_text(msg):
if len(msg) == 0:
raise ValidationError('message cannot be empty')
if len(msg) > 500:
raise ValidationError('too long')
class GuestbookForm(Form):
name = CharField(label='name!')
msg = CharField(
widget=Textarea,
label="message!",
validators=(validate_msg_text,),
)
captcha = CaptchaField()
def clean(self):
result = super().clean()
if self.errors:
raise ValidationError('oops, looks like there were some problems below.')
throttle_submission(result['name'])
2016-12-20 06:00:00 +00:00
return result