From 2491f076136ff3c9b244b5c69e3afe02d91222a4 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 7 Apr 2017 16:41:38 -0300 Subject: [PATCH] Made some updates in forms to include validations in fields --- news/forms.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+), 0 deletions(-) diff --git a/news/forms.py b/news/forms.py index cde2da0..8cf5b04 100644 --- a/news/forms.py +++ b/news/forms.py @@ -2,11 +2,39 @@ from django import forms from django.utils.translation import ugettext_lazy as _ from .models import News +from resubmit.widgets import ResubmitFileWidget + class NewsForm(forms.ModelForm): + MAX_UPLOAD_SIZE = 5*1024*1024 class Meta: model = News fields = ['title','image','content'] widgets = { 'content': forms.Textarea, + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}), + } + + def clean_title(self): + title = self.cleaned_data.get('title', '') + if same_name > 0: + self._errors['name'] = [_('This subject already has a pdf file with this name')] + + return ValueError + + return title + def clean_image(self): + image = self.cleaned_data.get('image', False) + + if image: + if hasattr(image, '_size'): + if image._size > self.MAX_UPLOAD_SIZE: + self._errors['image'] = [_("The image is too large. It should have less than 5MB.")] + return ValueError + else: + self._errors['image'] = [_("This field is required.")] + + return ValueError + + return image -- libgit2 0.21.2