forms.py
181 lines
| 5.2 KiB
| text/x-python
|
PythonLexer
/ boards / forms.py
|
r69 | import re | |
|
r78 | from captcha.fields import CaptchaField | |
|
r14 | from django import forms | |
|
r76 | from django.forms.util import ErrorList | |
|
r153 | from django.utils import timezone | |
import time | |||
import math | |||
|
r144 | from boards.models import TITLE_MAX_LENGTH, User | |
|
r35 | from neboard import settings | |
|
r95 | from boards import utils | |
|
r76 | ||
|
r153 | LAST_POST_TIME = "last_post_time" | |
|
r76 | class PlainErrorList(ErrorList): | |
def __unicode__(self): | |||
return self.as_text() | |||
def as_text(self): | |||
return ''.join([u'(!) %s ' % e for e in self]) | |||
|
r16 | ||
|
r29 | class PostForm(forms.Form): | |
|
r76 | ||
|
r151 | MAX_TEXT_LENGTH = 30000 | |
|
r29 | MAX_IMAGE_SIZE = 8 * 1024 * 1024 | |
|
r76 | title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False) | |
|
r29 | text = forms.CharField(widget=forms.Textarea, required=False) | |
|
r18 | image = forms.ImageField(required=False) | |
|
r29 | ||
|
r153 | session = None | |
|
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 | |||
|
r29 | def clean_text(self): | |
text = self.cleaned_data['text'] | |||
if text: | |||
if len(text) > self.MAX_TEXT_LENGTH: | |||
|
r76 | raise forms.ValidationError('Text must have less than ' + | |
str(self.MAX_TEXT_LENGTH) + | |||
' characters.') | |||
|
r29 | return text | |
def clean_image(self): | |||
image = self.cleaned_data['image'] | |||
if image: | |||
if image._size > self.MAX_IMAGE_SIZE: | |||
|
r76 | raise forms.ValidationError('Image must be less than ' + | |
str(self.MAX_IMAGE_SIZE) + | |||
' bytes.') | |||
|
r29 | return image | |
def clean(self): | |||
cleaned_data = super(PostForm, self).clean() | |||
|
r151 | if not self.errors: | |
self._clean_text_image() | |||
|
r77 | ||
|
r153 | if not self.errors and self.session: | |
self._validate_posting_speed() | |||
|
r76 | return cleaned_data | |
def _clean_text_image(self): | |||
text = self.cleaned_data.get('text') | |||
image = self.cleaned_data.get('image') | |||
|
r29 | ||
if (not text) and (not image): | |||
|
r77 | error_message = 'Either text or image must be entered.' | |
self._errors['text'] = self.error_class([error_message]) | |||
|
r153 | ||
def _validate_posting_speed(self): | |||
can_post = True | |||
if LAST_POST_TIME in self.session: | |||
now = time.time() | |||
last_post_time = self.session[LAST_POST_TIME] | |||
current_delay = int(now - last_post_time) | |||
if current_delay < settings.POSTING_DELAY: | |||
error_message = 'Wait ' + str(settings.POSTING_DELAY - | |||
current_delay)\ | |||
+ ' seconds after last posting' | |||
self._errors['text'] = self.error_class([error_message]) | |||
can_post = False | |||
if can_post: | |||
self.session[LAST_POST_TIME] = time.time() | |||
|
r29 | ||
class ThreadForm(PostForm): | |||
|
r71 | regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE) | |
|
r14 | tags = forms.CharField(max_length=100) | |
|
r31 | ||
def clean_tags(self): | |||
tags = self.cleaned_data['tags'] | |||
|
r69 | ||
|
r31 | if tags: | |
|
r69 | if not self.regex_tags.match(tags): | |
raise forms.ValidationError( | |||
|
r76 | 'Inappropriate characters in tags.') | |
|
r31 | ||
return tags | |||
def clean(self): | |||
cleaned_data = super(ThreadForm, self).clean() | |||
|
r35 | return cleaned_data | |
|
r95 | 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") | |||
|
r78 | class ThreadCaptchaForm(ThreadForm): | |
captcha = CaptchaField() | |||
|
r95 | 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") | |||
|
r78 | ||
|
r35 | class SettingsForm(forms.Form): | |
|
r95 | theme = forms.ChoiceField(choices=settings.THEMES, widget=forms.RadioSelect) | |
|
r144 | ||
class LoginForm(forms.Form): | |||
user_id = forms.CharField() | |||
def clean_user_id(self): | |||
user_id = self.cleaned_data['user_id'] | |||
if user_id: | |||
users = User.objects.filter(user_id=user_id) | |||
if len(users) == 0: | |||
raise forms.ValidationError('No such user found') | |||
return user_id | |||
def clean(self): | |||
cleaned_data = super(LoginForm, self).clean() | |||
return cleaned_data |