From 5e037636ea0af136098e839e2dbbe8ea5beb1b2d Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 1 Oct 2022 11:54:37 +0200 Subject: [PATCH] Add last-modified to article view --- src/articles/tests/test_html_views.py | 1 + src/articles/views/html/single_article.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/articles/tests/test_html_views.py b/src/articles/tests/test_html_views.py index 1ece30f..415b053 100644 --- a/src/articles/tests/test_html_views.py +++ b/src/articles/tests/test_html_views.py @@ -52,6 +52,7 @@ def _assert_article_is_rendered(item: Article, res: HttpResponse) -> None: assert item.title in content html = item.get_formatted_content assert html in content + assert "Last-Modified" in res.headers @pytest.mark.django_db() diff --git a/src/articles/views/html/single_article.py b/src/articles/views/html/single_article.py index 7b2d104..e1bb409 100644 --- a/src/articles/views/html/single_article.py +++ b/src/articles/views/html/single_article.py @@ -1,10 +1,24 @@ +import datetime + from django.core.handlers.wsgi import WSGIRequest from django.http import HttpResponse from django.shortcuts import get_object_or_404, render +from django.views.decorators.http import last_modified from articles.models import Article +def get_article_last_modified(request: WSGIRequest, slug: str) -> datetime.datetime: + key = request.GET.get("draft_key") + qs = Article.objects.all().only("updated_at") + if key: + return get_object_or_404(qs, draft_key=key, slug=slug).updated_at + if not request.user.is_authenticated: + qs = qs.filter(status=Article.PUBLISHED) + return get_object_or_404(qs, slug=slug).updated_at + + +@last_modified(get_article_last_modified) def view_article(request: WSGIRequest, slug: str) -> HttpResponse: article = get_article(request, slug) if not request.user.is_authenticated: