Example #1
0
def topic_view(request, topic_id):
	topic = cache.get('forums_topic_%s' % topic_id)
	if topic == None:
		topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
		cache.set('forums_topic_%s' % topic_id, topic)
	can_access = can_access_forum(request, topic.forum)
	if not can_access == True:
		return can_access
	tracker = ObjectTracker(request.session)
	Topic.objects.filter(pk=topic_id).update(view_count=F('view_count') + 1) # FIXME ++ with cache
	posts = cache.get('forums_posts_%s' % topic_id)
	if posts == None:
		posts = list(Post.objects.filter(topic__pk=topic_id) \
			.select_related('author__profile__group', 'karma'))
		cache.set('forums_posts_%s' % topic_id, posts)
	for post in posts:
		post.is_unread = not tracker.has_viewed(post, 'created_at')
	tracker.bulk_mark_as_viewed(posts)
	return {'topic': topic, 'posts': posts, 'first_post_id': posts[0].id}