##// END OF EJS Templates
Use cache for thread list.
Use cache for thread list.

File last commit:

r271:ac659dae default
r318:eebfca22 default
Show More
forms.py
254 lines | 7.4 KiB | text/x-python | PythonLexer
neko259
Added a regex for validation of tags. This refs #10
r69 import re
Ilyas
Added django-simple-capthca support...
r78 from captcha.fields import CaptchaField
Ilyas
Added creating new thread form...
r14 from django import forms
neko259
Added form validation, added style for the tag panel. This fixes #13
r76 from django.forms.util import ErrorList
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 from django.utils.translation import ugettext_lazy as _
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153 import time
neko259
Added login functionality.
r144 from boards.models import TITLE_MAX_LENGTH, User
neko259
Added themes support. Added 'snow white' theme by Mystra_x64.
r35 from neboard import settings
wnc_21
Added captcha support
r95 from boards import utils
neko259
Added form validation, added style for the tag panel. This fixes #13
r76
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153 LAST_POST_TIME = "last_post_time"
neko259
Added localizations to forms. Do not allow users without session to post.
r211 LAST_LOGIN_TIME = "last_login_time"
LOGIN_DELAY = 60 * 60
MAX_TEXT_LENGTH = 30000
MAX_IMAGE_SIZE = 8 * 1024 * 1024
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153
neko259
Added form validation, added style for the tag panel. This fixes #13
r76 class PlainErrorList(ErrorList):
def __unicode__(self):
return self.as_text()
def as_text(self):
return ''.join([u'(!) %s ' % e for e in self])
neko259
Added a text area to the posting form. Added a basic CSS style. Changed threads list a bit.
r16
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 class NeboardForm(forms.Form):
def as_p(self):
"Returns this form rendered as HTML <p>s."
return self._html_output(
normal_row='<div class="form-row">'
'<div class="form-label">'
'%(label)s'
'</div>'
'<div class="form-input">'
'%(field)s'
'</div>'
'%(help_text)s'
'</div>',
error_row='<div class="form-errors">%s</div>',
row_ender='</p>',
help_text_html=' <span class="helptext">%s</span>',
errors_on_separate_row=True)
class PostForm(NeboardForm):
neko259
Added form validation, added style for the tag panel. This fixes #13
r76
neko259
Added some labels to the forms.
r232 title = forms.CharField(max_length=TITLE_MAX_LENGTH, required=False,
label=_('Title'))
text = forms.CharField(widget=forms.Textarea, required=False,
label=_('Text'))
image = forms.ImageField(required=False, label=_('Image'))
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29
neko259
Added a hidden antispam field to the forms.
r207 # This field is for spam prevention only
neko259
Added some labels to the forms.
r232 email = forms.CharField(max_length=100, required=False, label=_('e-mail'),
widget=forms.TextInput(attrs={
'class': 'form-email'}))
neko259
Added a hidden antispam field to the forms.
r207
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153 session = None
neko259
Added autoban by the hidden field (to ban spammers and prevent them from trying to post anything).
r271 need_to_ban = False
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153
neko259
Added form validation, added style for the tag panel. This fixes #13
r76 def clean_title(self):
title = self.cleaned_data['title']
if title:
if len(title) > TITLE_MAX_LENGTH:
neko259
Added localizations to forms. Do not allow users without session to post.
r211 raise forms.ValidationError(_('Title must have less than %s '
'characters') %
str(TITLE_MAX_LENGTH))
neko259
Added form validation, added style for the tag panel. This fixes #13
r76 return title
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29 def clean_text(self):
text = self.cleaned_data['text']
if text:
neko259
Added localizations to forms. Do not allow users without session to post.
r211 if len(text) > MAX_TEXT_LENGTH:
raise forms.ValidationError(_('Text must have less than %s '
'characters') %
str(MAX_TEXT_LENGTH))
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29 return text
def clean_image(self):
image = self.cleaned_data['image']
if image:
neko259
Added localizations to forms. Do not allow users without session to post.
r211 if image._size > MAX_IMAGE_SIZE:
raise forms.ValidationError(_('Image must be less than %s '
'bytes') % str(MAX_IMAGE_SIZE))
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29 return image
def clean(self):
cleaned_data = super(PostForm, self).clean()
neko259
Added localizations to forms. Do not allow users without session to post.
r211 if not self.session:
raise forms.ValidationError('Humans have sessions')
if cleaned_data['email']:
neko259
Added autoban by the hidden field (to ban spammers and prevent them from trying to post anything).
r271 self.need_to_ban = True
neko259
Added localizations to forms. Do not allow users without session to post.
r211 raise forms.ValidationError('A human cannot enter a hidden field')
neko259
Added a hidden antispam field to the forms.
r207
neko259
Fixed validation message. 'text or image must be entered' was shown instead of the read one.
r151 if not self.errors:
self._clean_text_image()
neko259
Fixed validation of image without text. This refs #13
r77
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153 if not self.errors and self.session:
self._validate_posting_speed()
neko259
Added form validation, added style for the tag panel. This fixes #13
r76 return cleaned_data
def _clean_text_image(self):
text = self.cleaned_data.get('text')
image = self.cleaned_data.get('image')
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29
if (not text) and (not image):
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 error_message = _('Either text or image must be entered.')
neko259
Fixed validation of image without text. This refs #13
r77 self._errors['text'] = self.error_class([error_message])
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
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:
neko259
Added localizations to forms. Do not allow users without session to post.
r211 error_message = _('Wait %s seconds after last posting') % str(
settings.POSTING_DELAY - current_delay)
neko259
Implemented posting delay. User must wait some time before posting (a measure against flood).
r153 self._errors['text'] = self.error_class([error_message])
can_post = False
if can_post:
self.session[LAST_POST_TIME] = time.time()
neko259
Implemented form validation. When the form fails validation, showing the index page.
r29
class ThreadForm(PostForm):
neko259
Added some labels to the forms.
r232
neko259
Fixed unicode tags validation. Moved constants from classes to modules. Added error 404 when trying to get the non-existent thread. This fixes #19
r71 regex_tags = re.compile(ur'^[\w\s\d]+$', re.UNICODE)
neko259
Added some labels to the forms.
r232
tags = forms.CharField(max_length=100, label=_('Tags'))
neko259
Changed metadata design. Make space split tags.
r31
def clean_tags(self):
tags = self.cleaned_data['tags']
neko259
Added a regex for validation of tags. This refs #10
r69
neko259
Changed metadata design. Make space split tags.
r31 if tags:
neko259
Added a regex for validation of tags. This refs #10
r69 if not self.regex_tags.match(tags):
raise forms.ValidationError(
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 _('Inappropriate characters in tags.'))
neko259
Changed metadata design. Make space split tags.
r31
return tags
def clean(self):
cleaned_data = super(ThreadForm, self).clean()
neko259
Added themes support. Added 'snow white' theme by Mystra_x64.
r35 return cleaned_data
wnc_21
Added captcha support
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:
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 raise forms.ValidationError(_("Captcha validation failed"))
wnc_21
Added captcha support
r95
Ilyas
Added django-simple-capthca support...
r78 class ThreadCaptchaForm(ThreadForm):
captcha = CaptchaField()
wnc_21
Added captcha support
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:
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 raise forms.ValidationError(_("Captcha validation failed"))
wnc_21
Added captcha support
r95
Ilyas
Added django-simple-capthca support...
r78
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 class SettingsForm(NeboardForm):
theme = forms.ChoiceField(choices=settings.THEMES,
label=_('Theme'))
neko259
Added login functionality.
r144
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 class ModeratorSettingsForm(SettingsForm):
moderate = forms.BooleanField(required=False, label=_('Enable moderation '
'panel'))
class LoginForm(NeboardForm):
neko259
Added localizations to forms. Do not allow users without session to post.
r211
neko259
Added login functionality.
r144 user_id = forms.CharField()
neko259
Added localizations to forms. Do not allow users without session to post.
r211 session = None
neko259
Added login functionality.
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:
neko259
Added ability to disable moderator panel. Refactored forms code. Translating forms.
r205 raise forms.ValidationError(_('No such user found'))
neko259
Added login functionality.
r144
return user_id
neko259
Added localizations to forms. Do not allow users without session to post.
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)
if current_delay < LOGIN_DELAY:
error_message = _('Wait %s minutes after last login') % str(
(LOGIN_DELAY - current_delay) / 60)
self._errors['user_id'] = self.error_class([error_message])
can_post = False
if can_post:
self.session[LAST_LOGIN_TIME] = time.time()
neko259
Added login functionality.
r144 def clean(self):
neko259
Added localizations to forms. Do not allow users without session to post.
r211 if not self.session:
raise forms.ValidationError('Humans have sessions')
self._validate_login_speed()
neko259
Added login functionality.
r144 cleaned_data = super(LoginForm, self).clean()
neko259
Added autoban by the hidden field (to ban spammers and prevent them from trying to post anything).
r271 return cleaned_data