diff --git a/boards/cache.py b/boards/cache.py new file mode 100644 index 0000000..c5c2962 --- /dev/null +++ b/boards/cache.py @@ -0,0 +1,5 @@ +from boards.models import Board + + +def board_last_modified_at(request, board_slug): + return Board.objects.filter(slug=board_slug).first().refreshed_at diff --git a/boards/views.py b/boards/views.py index 48d3c36..d577453 100644 --- a/boards/views.py +++ b/boards/views.py @@ -4,8 +4,10 @@ from django.conf import settings from django.core.cache import cache from django.shortcuts import render, get_object_or_404 from django.views.decorators.cache import cache_page +from django.views.decorators.http import last_modified from auth.helpers import authorized_user +from boards.cache import board_last_modified_at from boards.models import Board, BoardBlock, BoardFeed @@ -17,6 +19,7 @@ def index(request): }) +@last_modified(board_last_modified_at) def board(request, board_slug): board = get_object_or_404(Board, slug=board_slug) @@ -28,7 +31,8 @@ def board(request, board_slug): }, status=401) cached_page = cache.get(f"board_{board.slug}") - if cached_page and board.refreshed_at <= datetime.utcnow() - timedelta(seconds=settings.BOARD_CACHE_SECONDS): + if cached_page and board.refreshed_at <= \ + datetime.utcnow() - timedelta(seconds=settings.BOARD_CACHE_SECONDS): return cached_page blocks = BoardBlock.objects.filter(board=board) @@ -59,4 +63,3 @@ def what(request): @cache_page(settings.STATIC_PAGE_CACHE_SECONDS) def privacy_policy(request): return render(request, "docs/privacy_policy.html") -