comparison gpp/forums/views/main.py @ 374:dd673fae508d

Fixing #156. Making it possible to goto the first unread post in a thread. Made the 'New' icon a link; when displayed on a topic, takes you to the first unread post.
author Brian Neal <bgneal@gmail.com>
date Mon, 07 Mar 2011 01:22:49 +0000
parents a43add8af83d
children 6f963e5e7b03
comparison
equal deleted inserted replaced
373:54cbdef18cf4 374:dd673fae508d
240 'is_subscribed': is_subscribed, 240 'is_subscribed': is_subscribed,
241 }, 241 },
242 context_instance=RequestContext(request)) 242 context_instance=RequestContext(request))
243 243
244 244
245 def topic_unread(request, id):
246 """
247 This view redirects to the first post the user hasn't read, if we can
248 figure that out. Otherwise we redirect to the topic.
249
250 """
251 topic_url = reverse('forums-topic_index', kwargs={'id': id})
252
253 if request.user.is_authenticated():
254 try:
255 tlv = TopicLastVisit.objects.get(user=request.user, topic=id)
256 except TopicLastVisit.DoesNotExist:
257 return HttpResponseRedirect(topic_url)
258
259 posts = Post.objects.filter(topic=id, creation_date__gt=tlv.last_visit)
260 if posts:
261 return _goto_post(posts[0])
262 else:
263 # just go to the last post in the topic
264 topic = get_object_or_404(Topic.objects.select_related('last_post'), pk=id)
265 return _goto_post(topic.last_post)
266
267 # user isn't authenticated, just go to the topic
268 return HttpResponseRedirect(topic_url)
269
270
245 @login_required 271 @login_required
246 def new_topic(request, slug): 272 def new_topic(request, slug):
247 """ 273 """
248 This view handles the creation of new topics. 274 This view handles the creation of new topics.
249 """ 275 """
318 context_instance=RequestContext(request)) 344 context_instance=RequestContext(request))
319 345
320 return HttpResponseBadRequest("Oops, did you forget some text?"); 346 return HttpResponseBadRequest("Oops, did you forget some text?");
321 347
322 348
349 def _goto_post(post):
350 """
351 Calculate what page the given post is on in its parent topic, then
352 return a redirect to it.
353
354 """
355 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
356 page = count / POSTS_PER_PAGE + 1
357 url = (reverse('forums-topic_index', kwargs={'id': post.topic.id}) +
358 '?page=%s#p%s' % (page, post.id))
359 return HttpResponseRedirect(url)
360
361
323 def goto_post(request, post_id): 362 def goto_post(request, post_id):
324 """ 363 """
325 This function calculates what page a given post is on, then redirects 364 This function calculates what page a given post is on, then redirects
326 to that URL. This function is the target of get_absolute_url() for 365 to that URL. This function is the target of get_absolute_url() for
327 Post objects. 366 Post objects.
328 """ 367 """
329 post = get_object_or_404(Post.objects.select_related(), pk=post_id) 368 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
330 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count() 369 return _goto_post(post)
331 page = count / POSTS_PER_PAGE + 1
332 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
333 '?page=%s#p%s' % (page, post.id)
334 return HttpResponseRedirect(url)
335 370
336 371
337 @require_POST 372 @require_POST
338 def flag_post(request): 373 def flag_post(request):
339 """ 374 """
766 801
767 return render_to_response('forums/topic_list.html', { 802 return render_to_response('forums/topic_list.html', {
768 'title': 'Topics With Unread Posts', 803 'title': 'Topics With Unread Posts',
769 'page': page, 804 'page': page,
770 'page_nav': page_nav, 805 'page_nav': page_nav,
806 'unread': True,
771 }, 807 },
772 context_instance=RequestContext(request)) 808 context_instance=RequestContext(request))
773 809
774 810
775 def unanswered_topics(request): 811 def unanswered_topics(request):
794 830
795 return render_to_response('forums/topic_list.html', { 831 return render_to_response('forums/topic_list.html', {
796 'title': 'Unanswered Topics', 832 'title': 'Unanswered Topics',
797 'page': page, 833 'page': page,
798 'page_nav': page_nav, 834 'page_nav': page_nav,
835 'unread': False,
799 }, 836 },
800 context_instance=RequestContext(request)) 837 context_instance=RequestContext(request))
801 838
802 839
803 @login_required 840 @login_required