Add last-modified to article view

This commit is contained in:
Gabriel Augendre 2022-10-01 11:54:37 +02:00
parent 936513d1f2
commit 5e037636ea
2 changed files with 15 additions and 0 deletions

View file

@ -52,6 +52,7 @@ def _assert_article_is_rendered(item: Article, res: HttpResponse) -> None:
assert item.title in content assert item.title in content
html = item.get_formatted_content html = item.get_formatted_content
assert html in content assert html in content
assert "Last-Modified" in res.headers
@pytest.mark.django_db() @pytest.mark.django_db()

View file

@ -1,10 +1,24 @@
import datetime
from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.views.decorators.http import last_modified
from articles.models import Article 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: def view_article(request: WSGIRequest, slug: str) -> HttpResponse:
article = get_article(request, slug) article = get_article(request, slug)
if not request.user.is_authenticated: if not request.user.is_authenticated: