|
|
from django import forms
|
|
|
from neboard import settings
|
|
|
|
|
|
|
|
|
class PostForm(forms.Form):
|
|
|
MAX_TEXT_LENGTH = 10000
|
|
|
MAX_IMAGE_SIZE = 8 * 1024 * 1024
|
|
|
|
|
|
title = forms.CharField(max_length=50, required=False)
|
|
|
text = forms.CharField(widget=forms.Textarea, required=False)
|
|
|
image = forms.ImageField(required=False)
|
|
|
|
|
|
def clean_text(self):
|
|
|
text = self.cleaned_data['text']
|
|
|
if text:
|
|
|
if len(text) > self.MAX_TEXT_LENGTH:
|
|
|
raise forms.ValidationError('Too many text')
|
|
|
return text
|
|
|
|
|
|
def clean_image(self):
|
|
|
image = self.cleaned_data['image']
|
|
|
if image:
|
|
|
if image._size > self.MAX_IMAGE_SIZE:
|
|
|
raise forms.ValidationError('Too large image: more than ' +
|
|
|
str(self.MAX_IMAGE_SIZE) + ' bytes')
|
|
|
return image
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(PostForm, self).clean()
|
|
|
|
|
|
text = cleaned_data.get('text')
|
|
|
image = cleaned_data.get('image')
|
|
|
|
|
|
if (not text) and (not image):
|
|
|
raise forms.ValidationError('Enter either text or image')
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
class ThreadForm(PostForm):
|
|
|
INVALID_TAG_CHARACTERS = ['+', '/', '&', '=', '?', '-', '.', ',']
|
|
|
|
|
|
tags = forms.CharField(max_length=100)
|
|
|
|
|
|
def clean_tags(self):
|
|
|
tags = self.cleaned_data['tags']
|
|
|
if tags:
|
|
|
for character in tags:
|
|
|
if character in self.INVALID_TAG_CHARACTERS:
|
|
|
raise forms.ValidationError(
|
|
|
'Inappropriate characters in tags')
|
|
|
|
|
|
return tags
|
|
|
|
|
|
def clean(self):
|
|
|
cleaned_data = super(ThreadForm, self).clean()
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
class SettingsForm(forms.Form):
|
|
|
theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect)
|