news: cache the news details page for non-logged in users
Logged in users can edit the page so the content should not be cached, for non-logged in users we are happy to cache this page in nginx.
This commit is contained in:
parent
b8a6a360db
commit
87c6325675
@ -1,6 +1,7 @@
|
||||
import hashlib
|
||||
import pickle
|
||||
import re
|
||||
from functools import WRAPPER_ASSIGNMENTS, wraps
|
||||
|
||||
import markdown
|
||||
from django.core.cache import cache
|
||||
@ -8,6 +9,7 @@
|
||||
from django.http import HttpResponse
|
||||
from django.template.defaultfilters import slugify
|
||||
from django.utils.timezone import now
|
||||
from django.views.decorators.cache import cache_page
|
||||
from markdown.extensions import Extension
|
||||
from pgpdump.packet import SignaturePacket
|
||||
|
||||
@ -62,6 +64,22 @@ def empty_response():
|
||||
make_choice = lambda l: [(str(m), str(m)) for m in l] # noqa E741
|
||||
|
||||
|
||||
def cache_user_page(timeout):
|
||||
'''Cache the page only for non-logged in users'''
|
||||
|
||||
def decorator(view_func):
|
||||
@wraps(view_func, assigned=WRAPPER_ASSIGNMENTS)
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated:
|
||||
return view_func(request, *args, **kwargs)
|
||||
result = cache_page(
|
||||
timeout,
|
||||
key_prefix=(f"_auth_{request.user.is_authenticated}_"))
|
||||
return result(view_func)(request, *args, **kwargs)
|
||||
return _wrapped_view
|
||||
return decorator
|
||||
|
||||
|
||||
def set_created_field(sender, **kwargs):
|
||||
'''This will set the 'created' field on any object to the current UTC time
|
||||
if it is unset.
|
||||
|
@ -1,6 +1,8 @@
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
from django.urls import path, re_path
|
||||
|
||||
from main.utils import cache_user_page
|
||||
|
||||
from .views import (
|
||||
NewsCreateView,
|
||||
NewsDeleteView,
|
||||
@ -21,7 +23,7 @@
|
||||
path('add/',
|
||||
permission_required('news.add_news')(NewsCreateView.as_view())),
|
||||
re_path(r'^(?P<slug>[-\w]+)/$',
|
||||
NewsDetailView.as_view()),
|
||||
cache_user_page(317)(NewsDetailView.as_view())),
|
||||
re_path(r'^(?P<slug>[-\w]+)/edit/$',
|
||||
permission_required('news.change_news')(NewsEditView.as_view())),
|
||||
re_path(r'^(?P<slug>[-\w]+)/delete/$',
|
||||
|
Loading…
Reference in New Issue
Block a user