##// END OF EJS Templates
Added error 404 for opening not existing thread or tag. This fixes #8
neko259 -
r79:b95f96a3 default
parent child Browse files
Show More
@@ -1,10 +1,9 b''
1 import re
1 import re
2 from django import forms
2 from django import forms
3 from django.forms.util import ErrorList
3 from django.forms.util import ErrorList
4 from boards.models import TITLE_MAX_LENGTH
4 from neboard import settings
5 from neboard import settings
5
6
6 TITLE_MAX_LENGTH = 50
7
8
7
9 class PlainErrorList(ErrorList):
8 class PlainErrorList(ErrorList):
10 def __unicode__(self):
9 def __unicode__(self):
@@ -12,6 +12,11 b' from markupfield.fields import MarkupFie'
12 from neboard import settings
12 from neboard import settings
13 import thumbs
13 import thumbs
14
14
15 IMAGE_THUMB_SIZE = (200, 150)
16
17 TITLE_MAX_LENGTH = 50
18
19 DEFAULT_MARKUP_TYPE = 'markdown'
15
20
16 NO_PARENT = -1
21 NO_PARENT = -1
17 NO_IP = '0.0.0.0'
22 NO_IP = '0.0.0.0'
@@ -38,8 +43,7 b' class PostManager(models.Manager):'
38 last_edit_time=timezone.now())
43 last_edit_time=timezone.now())
39
44
40 if tags:
45 if tags:
41 for tag in tags:
46 map(post.tags.add, tags)
42 post.tags.add(tag)
43
47
44 if parent_id != NO_PARENT:
48 if parent_id != NO_PARENT:
45 self._bump_thread(parent_id)
49 self._bump_thread(parent_id)
@@ -64,6 +68,10 b' class PostManager(models.Manager):'
64 threads = self.filter(parent=NO_PARENT, tags=tag)
68 threads = self.filter(parent=NO_PARENT, tags=tag)
65 else:
69 else:
66 threads = self.filter(parent=NO_PARENT)
70 threads = self.filter(parent=NO_PARENT)
71
72 if not threads:
73 raise Http404
74
67 threads = threads.order_by('-last_edit_time')
75 threads = threads.order_by('-last_edit_time')
68
76
69 if page != ALL_PAGES:
77 if page != ALL_PAGES:
@@ -78,9 +86,9 b' class PostManager(models.Manager):'
78 return threads
86 return threads
79
87
80 def get_thread(self, opening_post_id):
88 def get_thread(self, opening_post_id):
89 try:
81 opening_post = self.get(id=opening_post_id)
90 opening_post = self.get(id=opening_post_id)
82
91 except Post.DoesNotExist:
83 if not opening_post:
84 raise Http404
92 raise Http404
85
93
86 if opening_post.parent == NO_PARENT:
94 if opening_post.parent == NO_PARENT:
@@ -200,11 +208,12 b' class Post(models.Model):'
200
208
201 return os.path.join(path, new_name)
209 return os.path.join(path, new_name)
202
210
203 title = models.CharField(max_length=50)
211 title = models.CharField(max_length=TITLE_MAX_LENGTH)
204 pub_time = models.DateTimeField()
212 pub_time = models.DateTimeField()
205 text = MarkupField(default_markup_type='markdown', escape_html=True)
213 text = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE,
214 escape_html=True)
206 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
215 image = thumbs.ImageWithThumbsField(upload_to=_update_image_filename,
207 blank=True, sizes=((200, 150),))
216 blank=True, sizes=(IMAGE_THUMB_SIZE,))
208 poster_ip = models.IPAddressField()
217 poster_ip = models.IPAddressField()
209 poster_user_agent = models.TextField()
218 poster_user_agent = models.TextField()
210 parent = models.BigIntegerField()
219 parent = models.BigIntegerField()
@@ -212,7 +221,8 b' class Post(models.Model):'
212 last_edit_time = models.DateTimeField()
221 last_edit_time = models.DateTimeField()
213
222
214 def __unicode__(self):
223 def __unicode__(self):
215 return self.title + ' (' + self.text.raw + ')'
224 return '#' + str(self.id) + ' ' + self.title + ' (' + self.text.raw + \
225 ')'
216
226
217 def _get_replies(self):
227 def _get_replies(self):
218 return Post.objects.filter(parent=self.id)
228 return Post.objects.filter(parent=self.id)
@@ -10,8 +10,12 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 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE = '/thread/'
15 TAG_PAGE = '/tag/'
14 HTTP_CODE_REDIRECT = 302
16 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_OK = 200
18 HTTP_CODE_NOT_FOUND = 404
15
19
16
20
17 class BoardTests(TestCase):
21 class BoardTests(TestCase):
@@ -162,11 +166,45 b' class BoardTests(TestCase):'
162 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
166 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
163 'where it should fail')
167 'where it should fail')
164
168
165 response = client.post(THREAD_PAGE, {'text': TEST_TEXT,
169 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
166 'tags': valid_tags})
170 'tags': valid_tags})
167 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
171 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
168 msg='Posting new message failed: got code ' +
172 msg=u'Posting new message failed: got code ' +
169 str(response.status_code))
173 str(response.status_code))
170
174
171 self.assertEqual(2, Post.objects.count(),
175 self.assertEqual(2, Post.objects.count(),
172 msg='No posts were created') No newline at end of file
176 msg=u'No posts were created')
177
178 def test_404(self):
179 """Test receiving error 404 when opening a non-existent page"""
180
181 Post.objects.all().delete()
182 Tag.objects.all().delete()
183
184 tag_name = u'test_tag'
185 tags, = [Tag.objects.get_or_create(name=tag_name)]
186 client = Client()
187
188 Post.objects.create_post('title', TEST_TEXT, tags=tags)
189
190 existing_post_id = Post.objects.all()[0].id
191 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
192 '/')
193 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
194 u'Cannot open existing thread')
195
196 response_not_existing = client.get(THREAD_PAGE + str(
197 existing_post_id + 1) + '/')
198 self.assertEqual(HTTP_CODE_NOT_FOUND,
199 response_not_existing.status_code,
200 u'Not existing thread is opened')
201
202 response_existing = client.get(TAG_PAGE + tag_name + '/')
203 self.assertEqual(HTTP_CODE_OK,
204 response_existing.status_code,
205 u'Cannot open existing tag')
206
207 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
208 self.assertEqual(HTTP_CODE_NOT_FOUND,
209 response_not_existing.status_code,
210 u'Not existing tag is opened') No newline at end of file
@@ -1,6 +1,6 b''
1 from django.core.urlresolvers import reverse
1 from django.core.urlresolvers import reverse
2 from django.template import RequestContext
2 from django.template import RequestContext
3 from django.shortcuts import render, redirect
3 from django.shortcuts import render, redirect, get_object_or_404
4 from django.http import HttpResponseRedirect
4 from django.http import HttpResponseRedirect
5
5
6 from boards import forms
6 from boards import forms
@@ -83,7 +83,7 b' def _new_post(request, form, thread_id=b'
83 def tag(request, tag_name, page=0):
83 def tag(request, tag_name, page=0):
84 """Get all tag threads (posts without a parent)."""
84 """Get all tag threads (posts without a parent)."""
85
85
86 tag = Tag.objects.get(name=tag_name)
86 tag = get_object_or_404(Tag, name=tag_name)
87 threads = Post.objects.get_threads(tag=tag, page=int(page))
87 threads = Post.objects.get_threads(tag=tag, page=int(page))
88
88
89 if request.method == 'POST':
89 if request.method == 'POST':
General Comments 0
You need to be logged in to leave comments. Login now