##// END OF EJS Templates
Fixed validation of image without text. This refs #13
neko259 -
r77:30874aa0 default
parent child Browse files
Show More
@@ -33,8 +33,6 b' class PostForm(forms.Form):'
33 return title
33 return title
34
34
35 def clean_text(self):
35 def clean_text(self):
36 self._clean_text_image()
37
38 text = self.cleaned_data['text']
36 text = self.cleaned_data['text']
39 if text:
37 if text:
40 if len(text) > self.MAX_TEXT_LENGTH:
38 if len(text) > self.MAX_TEXT_LENGTH:
@@ -44,8 +42,6 b' class PostForm(forms.Form):'
44 return text
42 return text
45
43
46 def clean_image(self):
44 def clean_image(self):
47 self._clean_text_image()
48
49 image = self.cleaned_data['image']
45 image = self.cleaned_data['image']
50 if image:
46 if image:
51 if image._size > self.MAX_IMAGE_SIZE:
47 if image._size > self.MAX_IMAGE_SIZE:
@@ -57,6 +53,8 b' class PostForm(forms.Form):'
57 def clean(self):
53 def clean(self):
58 cleaned_data = super(PostForm, self).clean()
54 cleaned_data = super(PostForm, self).clean()
59
55
56 self._clean_text_image()
57
60 return cleaned_data
58 return cleaned_data
61
59
62 def _clean_text_image(self):
60 def _clean_text_image(self):
@@ -64,7 +62,9 b' class PostForm(forms.Form):'
64 image = self.cleaned_data.get('image')
62 image = self.cleaned_data.get('image')
65
63
66 if (not text) and (not image):
64 if (not text) and (not image):
67 raise forms.ValidationError('Either text or image must be entered.')
65 error_message = 'Either text or image must be entered.'
66 self._errors['text'] = self.error_class([error_message])
67 self._errors['image'] = self.error_class([error_message])
68
68
69
69
70 class ThreadForm(PostForm):
70 class ThreadForm(PostForm):
@@ -10,6 +10,7 b' from neboard import settings'
10 TEST_TEXT = 'test text'
10 TEST_TEXT = 'test text'
11
11
12 NEW_THREAD_PAGE = '/'
12 NEW_THREAD_PAGE = '/'
13 THREAD_PAGE = '/thread/1/'
13 HTTP_CODE_REDIRECT = 302
14 HTTP_CODE_REDIRECT = 302
14
15
15
16
@@ -159,4 +160,13 b' class BoardTests(TestCase):'
159 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
160 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
160 'tags': invalid_tags})
161 'tags': invalid_tags})
161 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
162 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
162 'where it should fail') No newline at end of file
163 'where it should fail')
164
165 response = client.post(THREAD_PAGE, {'text': TEST_TEXT,
166 'tags': valid_tags})
167 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
168 msg='Posting new message failed: got code ' +
169 str(response.status_code))
170
171 self.assertEqual(2, Post.objects.count(),
172 msg='No posts were created') No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now