All pastes #234083 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-11-02 08:54 UTC
rendered paste body
def thread(request, thread_id, page=1):
	page = int(page)
	current_site = Site.objects.get_current()
	filters = {'thread': thread_id, 'visible': True}

	thread = cache.get('thread_%s' % thread_id)
	if not thread:
		thread = get_object_or_404(Thread, pk=thread_id)
		if not thread.visible and not request.user.has_perm('can_edit'):
			raise Http404
		cache.set('thread_%d' % (thread.id), thread)

	category_list = cache.get('category_list_%d' % current_site.id)
	if not category_list:
		category_list = Category.on_site.all().order_by('name')
		cache.set('category_list_%d' % current_site.id, category_list)

	orig_post = cache.get('orig_post_%s' % thread_id)
	if not orig_post:
		orig_post = Post.objects.filter(**filters).order_by('post_date')[0]
		cache.set('orig_post_%s' % thread_id, orig_post)
		
	posts_total = cache.get('posts_total_%s' % thread_id)
	if not posts_total:
		posts_total = Post.objects.filter(**filters).order_by('post_date').count()
		cache.set('posts_total_%s' % thread_id, posts_total)

	request.session['threads'].update({int(thread_id): datetime.datetime.now()})

	total_pages = int(ceil(int(posts_total)/float(POSTS_PER_PAGE)))
	if total_pages == 0:
		total_pages = 1

	if page > total_pages and page > 1:
		raise Http404

	pages_to_show = []
	post_list = []
	if posts_total > 0:
		limit_start = (page-1)*POSTS_PER_PAGE
		limit_end = page*POSTS_PER_PAGE
		i = (page-5)
		if i > 2:
			start_page = True
		if (page+6) < total_pages:
			end_page = True
		while i <= (page+5):
			if i > 0 and i <= total_pages:
				pages_to_show.append(i)
			i += 1
		post_list = cache.get('posts_%s_%s' % (thread_id, page))
		if not post_list:
			post_list = Post.objects.filter(**filters).order_by('post_date')[limit_start:limit_end]
			for post in post_list:
				post.html = wiki_to_html(post.body, FormatterEnv(), request.LANGUAGE_CODE)
				if post.author.id == request.user.id or request.user.has_perm('can_edit'):
					post.canedit = True;
			cache.set('posts_%s_%s' % (thread_id, page), post_list)

	extra_context = {
		'thread': thread,
		'post_list': post_list,
		'category_list': category_list,
		'orig_post': orig_post,
		'pages_to_show': pages_to_show,
		'page': page,
		'total_pages': total_pages,
		'OBJECT': {'app': 'forums', 'view': 'thread', 'id': thread.id},
	}
	if request.user.has_perm('forums.can_edit'):
		extra_context.update({'moderator': True})

	return render_template(request, 'forums/thread.html', extra_context)