# HG changeset patch # User neko259 # Date 2013-04-06 19:36:06 # Node ID 575870f69f4e3c06be7f7b977569b0dc39010a24 # Parent b39294a85d4c2660fbb016e4da1dd04b383fbf2f Implemented form validation. When the form fails validation, showing the index page. diff --git a/boards/forms.py b/boards/forms.py --- a/boards/forms.py +++ b/boards/forms.py @@ -1,8 +1,41 @@ from django import forms -class NewThreadForm(forms.Form): - title = forms.CharField(max_length=100) - text = forms.CharField(widget=forms.Textarea) - image = forms.ImageField() - tags = forms.CharField(max_length=100) +class PostForm(forms.Form): + MAX_TEXT_LENGTH = 10000 + MAX_IMAGE_SIZE = 8 * 1024 * 1024 + + title = forms.CharField(max_length=50, required=False) + text = forms.CharField(widget=forms.Textarea, required=False) + image = forms.ImageField(required=False) + tags = forms.CharField(max_length=100, required=False) + + def clean_text(self): + text = self.cleaned_data['text'] + if text: + if len(text) > self.MAX_TEXT_LENGTH: + raise forms.ValidationError('Too many text') + return text + + def clean_image(self): + image = self.cleaned_data['image'] + if image: + if image._size > self.MAX_IMAGE_SIZE: + raise forms.ValidationError('Too large image: more than ' + + str(self.MAX_IMAGE_SIZE) + ' bytes') + return image + + def clean(self): + cleaned_data = super(PostForm, self).clean() + + text = cleaned_data.get('text') + image = cleaned_data.get('image') + + if (not text) and (not image): + raise forms.ValidationError('Enter either text or image') + + return cleaned_data + + +class ThreadForm(PostForm): + tags = forms.CharField(max_length=100) \ No newline at end of file diff --git a/boards/models.py b/boards/models.py --- a/boards/models.py +++ b/boards/models.py @@ -1,5 +1,7 @@ +import os from django.db import models from django.utils import timezone +import time from neboard import settings @@ -10,6 +12,14 @@ NO_IP = '0.0.0.0' UNKNOWN_UA = '' +def update_image_filename(instance, filename): + """Get unique image filename""" + + path = 'images/' + new_name = str(int(time.mktime(time.gmtime()))) + '_' + filename + return os.path.join(path, new_name) + + class PostManager(models.Manager): def create_post(self, title, text, image=None, parent_id=NO_PARENT, ip=NO_IP, tags=None): @@ -108,10 +118,10 @@ class Post(models.Model): objects = PostManager() - title = models.CharField(max_length=100) + title = models.CharField(max_length=50) pub_time = models.DateTimeField() text = models.TextField() - image = thumbs.ImageWithThumbsField(upload_to='images/', + image = thumbs.ImageWithThumbsField(upload_to=update_image_filename, blank=True, sizes=((200, 150),)) poster_ip = models.IPAddressField() poster_user_agent = models.TextField() diff --git a/boards/static/css/base_page.css b/boards/static/css/base_page.css --- a/boards/static/css/base_page.css +++ b/boards/static/css/base_page.css @@ -1,5 +1,5 @@ html { - background: #333; + background: #444; color: #ffffff; } @@ -15,11 +15,11 @@ html { .post-form { text-align: left; - color: #ffffff; display: table; - border: dashed 1px; - padding: 3px; + border-radius: 5px; + padding: 5px; margin: 5px; + background: #334; } .form-row { @@ -60,10 +60,9 @@ html { } .post { - background: #222; + background: #333; margin: 5px; - padding: 5px; - border: solid 1px; + padding: 10px; border-radius: 5px; } diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -1,7 +1,7 @@ from django.template import RequestContext from boards import forms import boards -from boards.forms import NewThreadForm +from boards.forms import ThreadForm, PostForm from boards.models import Post, Admin, Tag from django.shortcuts import render, get_list_or_404, redirect from django.http import HttpResponseRedirect, Http404 @@ -16,7 +16,7 @@ def index(request): threads = Post.objects.get_threads() context['threads'] = None if len(threads) == 0 else threads - context['form'] = forms.NewThreadForm() + context['form'] = forms.ThreadForm() return render(request, 'posting_general.html', context) @@ -25,13 +25,21 @@ def index(request): def new_post(request, thread_id=boards.models.NO_PARENT): """Add a new post (in thread or as a reply).""" - form = NewThreadForm(request.POST, request.FILES) + if thread_id == boards.models.NO_PARENT: + form = ThreadForm(request.POST, request.FILES) + else: + form = PostForm(request.POST, request.FILES) - title = request.POST['title'] - text = request.POST['text'] + if form.is_valid(): + data = form.cleaned_data + else: + return redirect(index) - if 'image' in request.FILES.keys(): - image = request.FILES['image'] + title = data['title'] + text = data['text'] + + if 'image' in data.keys(): + image = data['image'] else: image = None @@ -39,7 +47,7 @@ def new_post(request, thread_id=boards.m tags = [] if thread_id == boards.models.NO_PARENT: - tag_strings = request.POST['tags'] + tag_strings = data['tags'] if tag_strings: tag_strings = tag_strings.split(',') @@ -74,7 +82,7 @@ def tag(request, tag_name): context['threads'] = None if len(threads) == 0 else threads context['tag'] = tag_name - context['form'] = forms.NewThreadForm(initial={'tags': tag_name}) + context['form'] = forms.ThreadForm(initial={'tags': tag_name}) return render(request, 'posting_general.html', context) @@ -92,7 +100,7 @@ def thread(request, post_id): context = RequestContext(request) context['posts'] = posts - context['form'] = forms.NewThreadForm() + context['form'] = forms.PostForm() return render(request, 'thread.html', context)