forms.py
90 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
/ boards / forms.py
neko259
|
r69 | import re | |
Ilyas
|
r14 | from django import forms | |
neko259
|
r76 | from django.forms.util import ErrorList | |
neko259
|
r35 | from neboard import settings | |
Ilyas
|
r14 | ||
neko259
|
r76 | TITLE_MAX_LENGTH = 50 | |
class PlainErrorList(ErrorList): | |||
def __unicode__(self): | |||
return self.as_text() | |||
def as_text(self): | |||
return ''.join([u'(!) %s ' % e for e in self]) | |||
neko259
|
r16 | ||
neko259
|
r29 | class PostForm(forms.Form): | |
neko259
|
r76 | ||
neko259
|
r29 | MAX_TEXT_LENGTH = 10000 | |
MAX_IMAGE_SIZE = 8 * 1024 * 1024 | |||
neko259
|
r76 | title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False) | |
neko259
|
r29 | text = forms.CharField(widget=forms.Textarea, required=False) | |
Ilyas
|
r18 | image = forms.ImageField(required=False) | |
neko259
|
r29 | ||
neko259
|
r76 | 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 | |||
neko259
|
r29 | def clean_text(self): | |
text = self.cleaned_data['text'] | |||
if text: | |||
if len(text) > self.MAX_TEXT_LENGTH: | |||
neko259
|
r76 | raise forms.ValidationError('Text must have less than ' + | |
str(self.MAX_TEXT_LENGTH) + | |||
' characters.') | |||
neko259
|
r29 | return text | |
def clean_image(self): | |||
image = self.cleaned_data['image'] | |||
if image: | |||
if image._size > self.MAX_IMAGE_SIZE: | |||
neko259
|
r76 | raise forms.ValidationError('Image must be less than ' + | |
str(self.MAX_IMAGE_SIZE) + | |||
' bytes.') | |||
neko259
|
r29 | return image | |
def clean(self): | |||
cleaned_data = super(PostForm, self).clean() | |||
neko259
|
r77 | self._clean_text_image() | |
neko259
|
r76 | return cleaned_data | |
def _clean_text_image(self): | |||
text = self.cleaned_data.get('text') | |||
image = self.cleaned_data.get('image') | |||
neko259
|
r29 | ||
if (not text) and (not image): | |||
neko259
|
r77 | 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]) | |||
neko259
|
r29 | ||
class ThreadForm(PostForm): | |||
neko259
|
r71 | regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE) | |
Ilyas
|
r14 | tags = forms.CharField(max_length=100) | |
neko259
|
r31 | ||
def clean_tags(self): | |||
tags = self.cleaned_data['tags'] | |||
neko259
|
r69 | ||
neko259
|
r31 | if tags: | |
neko259
|
r69 | if not self.regex_tags.match(tags): | |
raise forms.ValidationError( | |||
neko259
|
r76 | 'Inappropriate characters in tags.') | |
neko259
|
r31 | ||
return tags | |||
def clean(self): | |||
cleaned_data = super(ThreadForm, self).clean() | |||
neko259
|
r35 | return cleaned_data | |
class SettingsForm(forms.Form): | |||
theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect) |