# HG changeset patch # User neko259 # Date 2015-01-18 15:18:53 # Node ID eaceb3a3fd4659c1c2a74fd0f18bc9894ebf7d84 # Parent f79c88624c28e07ed9bd474d0a5c7a637212f1d4 Removed old forms, refactored some code for forms and images. Use the same code to compute hash of an image in form and when saving image diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -1,6 +1,5 @@ import re import time -import hashlib from django import forms from django.forms.util import ErrorList @@ -10,16 +9,17 @@ from boards.mdx_neboard import formatter from boards.models.post import TITLE_MAX_LENGTH from boards.models import PostImage, Tag from neboard import settings -from boards import utils import boards.settings as board_settings +REGEX_TAGS = re.compile(r'^[\w\s\d]+$', re.UNICODE) + VETERAN_POSTING_DELAY = 5 ATTRIBUTE_PLACEHOLDER = 'placeholder' LAST_POST_TIME = 'last_post_time' LAST_LOGIN_TIME = 'last_login_time' -TEXT_PLACEHOLDER = _('''Type message here. Use formatting panel for more advanced usage.''') +TEXT_PLACEHOLDER = _('Type message here. Use formatting panel for more advanced usage.') TAGS_PLACEHOLDER = _('tag1 several_words_tag') ERROR_IMAGE_DUPLICATE = _('Such image was already posted') @@ -31,10 +31,13 @@ LABEL_SEARCH = _('Search') TAG_MAX_LENGTH = 20 -REGEX_TAG = r'^[\w\d]+$' - class FormatPanel(forms.Textarea): + """ + Panel for text formatting. Consists of buttons to add different tags to the + form text area. + """ + def render(self, name, value, attrs=None): output = '
' for formatter in formatters: @@ -59,6 +62,9 @@ class PlainErrorList(ErrorList): class NeboardForm(forms.Form): + """ + Form with neboard-specific formatting. + """ def as_div(self): """ @@ -143,10 +149,7 @@ class PostForm(NeboardForm): _('Image must be less than %s bytes') % str(board_settings.MAX_IMAGE_SIZE)) - md5 = hashlib.md5() - for chunk in image.chunks(): - md5.update(chunk) - image_hash = md5.hexdigest() + image_hash = PostImage.get_hash(image) if PostImage.objects.filter(hash=image_hash).exists(): raise forms.ValidationError(ERROR_IMAGE_DUPLICATE) @@ -203,8 +206,6 @@ class PostForm(NeboardForm): class ThreadForm(PostForm): - regex_tags = re.compile(r'^[\w\s\d]+$', re.UNICODE) - tags = forms.CharField( widget=forms.TextInput(attrs={ATTRIBUTE_PLACEHOLDER: TAGS_PLACEHOLDER}), max_length=100, label=_('Tags'), required=True) @@ -212,17 +213,17 @@ class ThreadForm(PostForm): def clean_tags(self): tags = self.cleaned_data['tags'].strip() - if not tags or not self.regex_tags.match(tags): + if not tags or not REGEX_TAGS.match(tags): raise forms.ValidationError( _('Inappropriate characters in tags.')) - tag_models = [] required_tag_exists = False for tag in tags.split(): tag_model = Tag.objects.filter(name=tag.strip().lower(), - required=True) + required=True) if tag_model.exists(): required_tag_exists = True + break if not required_tag_exists: raise forms.ValidationError(_('Need at least 1 required tag.')) @@ -241,65 +242,5 @@ class SettingsForm(NeboardForm): label=_('Theme')) -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 - - class SearchForm(NeboardForm): query = forms.CharField(max_length=500, label=LABEL_SEARCH, required=False) - - -class LoginForm(NeboardForm): - - password = forms.CharField() - - session = None - - def clean_password(self): - password = self.cleaned_data['password'] - if board_settings.MASTER_PASSWORD != password: - raise forms.ValidationError(_('Invalid master password')) - - return password - - 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 < board_settings.LOGIN_TIMEOUT: - error_message = _('Wait %s minutes after last login') % str( - (board_settings.LOGIN_TIMEOUT - current_delay) / 60) - self._errors['password'] = self.error_class([error_message]) - - can_post = False - - if can_post: - self.session[LAST_LOGIN_TIME] = time.time() - - def clean(self): - self._validate_login_speed() - - cleaned_data = super(LoginForm, self).clean() - - return cleaned_data diff --git a/boards/models/image.py b/boards/models/image.py --- a/boards/models/image.py +++ b/boards/models/image.py @@ -56,10 +56,7 @@ class PostImage(models.Model, Viewable): """ if not self.pk and self.image: - md5 = hashlib.md5() - for chunk in self.image.chunks(): - md5.update(chunk) - self.hash = md5.hexdigest() + self.hash = PostImage.get_hash(self.image) super(PostImage, self).save(*args, **kwargs) def __str__(self): @@ -81,3 +78,13 @@ class PostImage(models.Model, Viewable): self.image.url_200x150, str(self.hash), str(self.pre_width), str(self.pre_height), str(self.width), str(self.height)) + + @staticmethod + def get_hash(image): + """ + Gets hash of an image. + """ + md5 = hashlib.md5() + for chunk in image.chunks(): + md5.update(chunk) + return md5.hexdigest()