from django.http import Http404
from django.shortcuts import get_object_or_404, render

from accounts.models import User
from reviews.models import Review


def public_review(request, username, review_slug):
    owner = get_object_or_404(User, username=username)
    review = get_object_or_404(Review, owner_id=owner.id, slug=review_slug)
    if review.publish_status != "P" or not review.is_public:
        raise Http404()
    articles = list(review.articles.all())
    stats = {
        "total": len(articles),
        "accepted": sum(1 for a in articles if a.status == "A"),
        "final": len(review.get_final_selection_articles()),
    }
    return render(
        request,
        "public/review.html",
        {"review": review, "owner": owner, "stats": stats},
    )
