|
|
import re
|
|
|
from captcha.fields import CaptchaField
|
|
|
from django import forms
|
|
|
from django.forms.util import ErrorList
|
|
|
from boards.models import TITLE_MAX_LENGTH
|
|
|
from neboard import settings
|
|
|
from boards import utils
|
|
|
|
|
|
class PlainErrorList(ErrorList):
|
|
|
def __unicode__(self):
|
|
|
return self.as_text()
|
|
|
|
|
|
def as_text(self):
|
|
|
return ''.join([u'(!) %s ' % e for e in self])
|
|
|
|
|
|
|
|
|
class PostForm(forms.Form):
|
|
|
|
|
|
MAX_TEXT_LENGTH = 10000
|
|
|
MAX_IMAGE_SIZE = 8 * 1024 * 1024
|
|
|
|
|
|
title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False)
|
|
|
text = forms.CharField(widget=forms.Textarea, required=False)
|
|
|
image = forms.ImageField(required=False)
|
|
|
|
|
|
def clean_title(self):
|
|
|
title = self.cleaned_data['title']
|
|
|
if title:
|
|
|
if len(title) > TITLE_MAX_LENGTH:
|
|
|
raise forms.ValidationError('Title must have less than' +
|
|
|
str(TITLE_MAX_LENGTH) +
|
|
|
' characters.')
|
|
|
return title
|
|
|
|
|
|
def clean_text(self):
|
|
|
text = self.cleaned_data['text']
|
|
|
if text:
|
|
|
if len(text) > self.MAX_TEXT_LENGTH:
|
|
|
raise forms.ValidationError('Text must have less than ' +
|
|
|
str(self.MAX_TEXT_LENGTH) +
|
|
|
' characters.')
|
|
|
return text
|
|
|
|
|
|
def clean_image(self):
|
|
|
image = self.cleaned_data['image']
|
|
|
if image:
|
|
|
if image._size > self.MAX_IMAGE_SIZE:
|
|
|
raise forms.ValidationError('Image must be less than ' +
|
|
|
str(self.MAX_IMAGE_SIZE) +
|
|
|
' bytes.')
|
|
|
return image
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(PostForm, self).clean()
|
|
|
|
|
|
self._clean_text_image()
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
def _clean_text_image(self):
|
|
|
text = self.cleaned_data.get('text')
|
|
|
image = self.cleaned_data.get('image')
|
|
|
|
|
|
if (not text) and (not image):
|
|
|
error_message = 'Either text or image must be entered.'
|
|
|
self._errors['text'] = self.error_class([error_message])
|
|
|
self._errors['image'] = self.error_class([error_message])
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadForm(PostForm):
|
|
|
regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE)
|
|
|
tags = forms.CharField(max_length=100)
|
|
|
|
|
|
def clean_tags(self):
|
|
|
tags = self.cleaned_data['tags']
|
|
|
|
|
|
if tags:
|
|
|
if not self.regex_tags.match(tags):
|
|
|
raise forms.ValidationError(
|
|
|
'Inappropriate characters in tags.')
|
|
|
|
|
|
return tags
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(ThreadForm, self).clean()
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
class PostCaptchaForm(PostForm):
|
|
|
captcha = CaptchaField()
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
self.request = kwargs['request']
|
|
|
del kwargs['request']
|
|
|
|
|
|
super(PostCaptchaForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(PostCaptchaForm, self).clean()
|
|
|
|
|
|
success = self.is_valid()
|
|
|
utils.update_captcha_access(self.request, success)
|
|
|
|
|
|
if success:
|
|
|
return cleaned_data
|
|
|
else:
|
|
|
raise forms.ValidationError("captcha validation failed")
|
|
|
|
|
|
|
|
|
class ThreadCaptchaForm(ThreadForm):
|
|
|
captcha = CaptchaField()
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
self.request = kwargs['request']
|
|
|
del kwargs['request']
|
|
|
|
|
|
super(ThreadCaptchaForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(ThreadCaptchaForm, self).clean()
|
|
|
|
|
|
success = self.is_valid()
|
|
|
utils.update_captcha_access(self.request, success)
|
|
|
|
|
|
if success:
|
|
|
return cleaned_data
|
|
|
else:
|
|
|
raise forms.ValidationError("captcha validation failed")
|
|
|
|
|
|
|
|
|
class SettingsForm(forms.Form):
|
|
|
theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect)
|
|
|
|