Commit 2491f076136ff3c9b244b5c69e3afe02d91222a4
1 parent
bc51a1f9
Exists in
master
and in
2 other branches
Made some updates in forms to include validations in fields
Showing
1 changed file
with
28 additions
and
0 deletions
Show diff stats
news/forms.py
@@ -2,11 +2,39 @@ from django import forms | @@ -2,11 +2,39 @@ from django import forms | ||
2 | from django.utils.translation import ugettext_lazy as _ | 2 | from django.utils.translation import ugettext_lazy as _ |
3 | 3 | ||
4 | from .models import News | 4 | from .models import News |
5 | +from resubmit.widgets import ResubmitFileWidget | ||
6 | + | ||
5 | 7 | ||
6 | class NewsForm(forms.ModelForm): | 8 | class NewsForm(forms.ModelForm): |
9 | + MAX_UPLOAD_SIZE = 5*1024*1024 | ||
7 | class Meta: | 10 | class Meta: |
8 | model = News | 11 | model = News |
9 | fields = ['title','image','content'] | 12 | fields = ['title','image','content'] |
10 | widgets = { | 13 | widgets = { |
11 | 'content': forms.Textarea, | 14 | 'content': forms.Textarea, |
15 | + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}), | ||
16 | + | ||
12 | } | 17 | } |
18 | + | ||
19 | + def clean_title(self): | ||
20 | + title = self.cleaned_data.get('title', '') | ||
21 | + if same_name > 0: | ||
22 | + self._errors['name'] = [_('This subject already has a pdf file with this name')] | ||
23 | + | ||
24 | + return ValueError | ||
25 | + | ||
26 | + return title | ||
27 | + def clean_image(self): | ||
28 | + image = self.cleaned_data.get('image', False) | ||
29 | + | ||
30 | + if image: | ||
31 | + if hasattr(image, '_size'): | ||
32 | + if image._size > self.MAX_UPLOAD_SIZE: | ||
33 | + self._errors['image'] = [_("The image is too large. It should have less than 5MB.")] | ||
34 | + return ValueError | ||
35 | + else: | ||
36 | + self._errors['image'] = [_("This field is required.")] | ||
37 | + | ||
38 | + return ValueError | ||
39 | + | ||
40 | + return image |