usergraphtheory's picture
Sync from erdostack/bench at a7805c45d77258d316383c92bdc9f472498d255f (part 11)
e2344df verified
Raw History Blame Contribute Delete
936 Bytes
from django.core.paginator import Paginator
from django.shortcuts import get_object_or_404, render
from articles.models import Article
from .models import Comment
PAGE_SIZE = 20
def thread(request, article_id):
article = get_object_or_404(Article, pk=article_id)
top_level = Comment.objects.filter(article=article, parent__isnull=True, hidden=False).order_by("-created_at")
page = Paginator(top_level, PAGE_SIZE).get_page(request.GET.get("page"))
rows = []
for comment in page:
replies = comment.replies.filter(hidden=False).order_by("created_at")
rows.append(
{
"comment": comment,
"author_name": comment.author.display_name,
"replies": [{"reply": r, "author_name": r.author.display_name} for r in replies],
}
)
return render(request, "comments/thread.html", {"article": article, "page": page, "rows": rows})