forms.py
354 lines
| 10.6 KiB
| text/x-python
|
PythonLexer
/ boards / forms.py
|
r69 | import re | |
|
r527 | import time | |
import hashlib | |||
|
r78 | from captcha.fields import CaptchaField | |
|
r14 | from django import forms | |
|
r76 | from django.forms.util import ErrorList | |
|
r205 | from django.utils.translation import ugettext_lazy as _ | |
|
r527 | ||
|
r438 | from boards.mdx_neboard import formatters | |
|
r386 | from boards.models.post import TITLE_MAX_LENGTH | |
|
r716 | from boards.models import User, PostImage | |
|
r35 | from neboard import settings | |
|
r95 | from boards import utils | |
|
r333 | import boards.settings as board_settings | |
|
r76 | ||
|
r642 | VETERAN_POSTING_DELAY = 5 | |
|
r517 | ATTRIBUTE_PLACEHOLDER = 'placeholder' | |
LAST_POST_TIME = 'last_post_time' | |||
LAST_LOGIN_TIME = 'last_login_time' | |||
TEXT_PLACEHOLDER = _('''Type message here. You can reply to message >>123 like | |||
this. 2 new lines are required to start new paragraph.''') | |||
|
r521 | TAGS_PLACEHOLDER = _('tag1 several_words_tag') | |
|
r211 | ||
|
r527 | ERROR_IMAGE_DUPLICATE = _('Such image was already posted') | |
LABEL_TITLE = _('Title') | |||
LABEL_TEXT = _('Text') | |||
|
r566 | LABEL_TAG = _('Tag') | |
|
r718 | LABEL_SEARCH = _('Search') | |
|
r566 | ||
TAG_MAX_LENGTH = 20 | |||
REGEX_TAG = ur'^[\w\d]+$' | |||
|
r527 | ||
|
r153 | ||
|
r438 | class FormatPanel(forms.Textarea): | |
def render(self, name, value, attrs=None): | |||
output = '<div id="mark-panel">' | |||
for formatter in formatters: | |||
output += u'<span class="mark_btn"' + \ | |||
u' onClick="addMarkToMsg(\'' + formatter.format_left + \ | |||
'\', \'' + formatter.format_right + '\')">' + \ | |||
formatter.preview_left + formatter.name + \ | |||
formatter.preview_right + u'</span>' | |||
output += '</div>' | |||
output += super(FormatPanel, self).render(name, value, attrs=None) | |||
return output | |||
|
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 | ||
|
r205 | class NeboardForm(forms.Form): | |
|
r426 | def as_div(self): | |
|
r438 | """ | |
Returns this form rendered as HTML <as_div>s. | |||
""" | |||
|
r205 | return self._html_output( | |
|
r425 | # TODO Do not show hidden rows in the list here | |
|
r680 | normal_row='<div class="form-row"><div class="form-label">' | |
|
r205 | '%(label)s' | |
|
r680 | '</div></div>' | |
'<div class="form-row"><div class="form-input">' | |||
|
r205 | '%(field)s' | |
|
r680 | '</div></div>' | |
'<div class="form-row">' | |||
|
r205 | '%(help_text)s' | |
'</div>', | |||
|
r426 | error_row='<div class="form-row">' | |
'<div class="form-label"></div>' | |||
'<div class="form-errors">%s</div>' | |||
'</div>', | |||
row_ender='</div>', | |||
help_text_html='%s', | |||
|
r205 | errors_on_separate_row=True) | |
|
r533 | def as_json_errors(self): | |
errors = [] | |||
for name, field in self.fields.items(): | |||
if self[name].errors: | |||
errors.append({ | |||
'field': name, | |||
'errors': self[name].errors.as_text(), | |||
}) | |||
return errors | |||
|
r438 | ||
|
r205 | class PostForm(NeboardForm): | |
|
r76 | ||
|
r232 | title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False, | |
|
r527 | label=LABEL_TITLE) | |
|
r517 | text = forms.CharField( | |
widget=FormatPanel(attrs={ATTRIBUTE_PLACEHOLDER: TEXT_PLACEHOLDER}), | |||
|
r527 | required=False, label=LABEL_TEXT) | |
|
r675 | image = forms.ImageField(required=False, label=_('Image'), | |
widget=forms.ClearableFileInput(attrs={'accept': 'image/*'})) | |||
|
r29 | ||
|
r207 | # This field is for spam prevention only | |
|
r232 | email = forms.CharField(max_length=100, required=False, label=_('e-mail'), | |
widget=forms.TextInput(attrs={ | |||
'class': 'form-email'})) | |||
|
r207 | ||
|
r153 | session = None | |
|
r271 | need_to_ban = False | |
|
r153 | ||
|
r76 | def clean_title(self): | |
title = self.cleaned_data['title'] | |||
if title: | |||
if len(title) > TITLE_MAX_LENGTH: | |||
|
r211 | raise forms.ValidationError(_('Title must have less than %s ' | |
'characters') % | |||
str(TITLE_MAX_LENGTH)) | |||
|
r76 | return title | |
|
r29 | def clean_text(self): | |
|
r678 | text = self.cleaned_data['text'].strip() | |
|
r29 | if text: | |
|
r333 | if len(text) > board_settings.MAX_TEXT_LENGTH: | |
|
r211 | raise forms.ValidationError(_('Text must have less than %s ' | |
'characters') % | |||
|
r333 | str(board_settings | |
.MAX_TEXT_LENGTH)) | |||
|
r29 | return text | |
def clean_image(self): | |||
image = self.cleaned_data['image'] | |||
if image: | |||
|
r333 | if image._size > board_settings.MAX_IMAGE_SIZE: | |
raise forms.ValidationError( | |||
_('Image must be less than %s bytes') | |||
% str(board_settings.MAX_IMAGE_SIZE)) | |||
|
r527 | ||
md5 = hashlib.md5() | |||
for chunk in image.chunks(): | |||
md5.update(chunk) | |||
image_hash = md5.hexdigest() | |||
|
r693 | if PostImage.objects.filter(hash=image_hash).exists(): | |
|
r527 | raise forms.ValidationError(ERROR_IMAGE_DUPLICATE) | |
|
r29 | return image | |
def clean(self): | |||
cleaned_data = super(PostForm, self).clean() | |||
|
r211 | if not self.session: | |
raise forms.ValidationError('Humans have sessions') | |||
if cleaned_data['email']: | |||
|
r271 | self.need_to_ban = True | |
|
r211 | raise forms.ValidationError('A human cannot enter a hidden field') | |
|
r207 | ||
|
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): | |||
|
r205 | error_message = _('Either text or image must be entered.') | |
|
r77 | self._errors['text'] = self.error_class([error_message]) | |
|
r153 | ||
def _validate_posting_speed(self): | |||
can_post = True | |||
|
r648 | # TODO Remove this, it's only for test | |
if not 'user_id' in self.session: | |||
return | |||
|
r642 | user = User.objects.get(id=self.session['user_id']) | |
if user.is_veteran(): | |||
posting_delay = VETERAN_POSTING_DELAY | |||
else: | |||
posting_delay = settings.POSTING_DELAY | |||
|
r153 | 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) | |||
|
r642 | if current_delay < posting_delay: | |
|
r211 | error_message = _('Wait %s seconds after last posting') % str( | |
|
r642 | posting_delay - current_delay) | |
|
r153 | 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): | |||
|
r232 | ||
|
r71 | regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE) | |
|
r232 | ||
|
r517 | tags = forms.CharField( | |
widget=forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER: TAGS_PLACEHOLDER}), | |||
|
r679 | max_length=100, label=_('Tags'), required=True) | |
|
r31 | ||
def clean_tags(self): | |||
|
r678 | tags = self.cleaned_data['tags'].strip() | |
|
r69 | ||
|
r679 | if not tags or not self.regex_tags.match(tags): | |
raise forms.ValidationError( | |||
_('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: | |||
|
r205 | raise forms.ValidationError(_("Captcha validation failed")) | |
|
r95 | ||
|
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: | |||
|
r205 | raise forms.ValidationError(_("Captcha validation failed")) | |
|
r95 | ||
|
r78 | ||
|
r205 | class SettingsForm(NeboardForm): | |
theme = forms.ChoiceField(choices=settings.THEMES, | |||
label=_('Theme')) | |||
|
r144 | ||
|
r205 | class ModeratorSettingsForm(SettingsForm): | |
moderate = forms.BooleanField(required=False, label=_('Enable moderation ' | |||
'panel')) | |||
class LoginForm(NeboardForm): | |||
|
r211 | ||
|
r144 | user_id = forms.CharField() | |
|
r211 | session = None | |
|
r144 | 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: | |||
|
r205 | raise forms.ValidationError(_('No such user found')) | |
|
r144 | ||
return user_id | |||
|
r211 | def _validate_login_speed(self): | |
can_post = True | |||
if LAST_LOGIN_TIME in self.session: | |||
now = time.time() | |||
last_login_time = self.session[LAST_LOGIN_TIME] | |||
current_delay = int(now - last_login_time) | |||
|
r333 | if current_delay < board_settings.LOGIN_TIMEOUT: | |
|
r211 | error_message = _('Wait %s minutes after last login') % str( | |
|
r333 | (board_settings.LOGIN_TIMEOUT - current_delay) / 60) | |
|
r211 | self._errors['user_id'] = self.error_class([error_message]) | |
can_post = False | |||
if can_post: | |||
self.session[LAST_LOGIN_TIME] = time.time() | |||
|
r144 | def clean(self): | |
|
r211 | if not self.session: | |
raise forms.ValidationError('Humans have sessions') | |||
self._validate_login_speed() | |||
|
r144 | cleaned_data = super(LoginForm, self).clean() | |
|
r271 | return cleaned_data | |
|
r566 | ||
class AddTagForm(NeboardForm): | |||
tag = forms.CharField(max_length=TAG_MAX_LENGTH, label=LABEL_TAG) | |||
method = forms.CharField(widget=forms.HiddenInput(), initial='add_tag') | |||
def clean_tag(self): | |||
tag = self.cleaned_data['tag'] | |||
regex_tag = re.compile(REGEX_TAG, re.UNICODE) | |||
if not regex_tag.match(tag): | |||
raise forms.ValidationError(_('Inappropriate characters in tags.')) | |||
return tag | |||
def clean(self): | |||
cleaned_data = super(AddTagForm, self).clean() | |||
return cleaned_data | |||
|
r718 | ||
class SearchForm(NeboardForm): | |||
query = forms.CharField(max_length=500, label=LABEL_SEARCH, required=False) |