|
1 | 1 | import hashlib
|
2 | 2 | import pickle
|
3 | 3 | import re
|
| 4 | +from functools import WRAPPER_ASSIGNMENTS, wraps |
4 | 5 |
|
5 | 6 | import markdown
|
6 | 7 | from django.core.cache import cache
|
7 | 8 | from django.db import connections, router
|
8 | 9 | from django.http import HttpResponse
|
9 | 10 | from django.template.defaultfilters import slugify
|
10 | 11 | from django.utils.timezone import now
|
| 12 | +from django.views.decorators.cache import cache_page |
11 | 13 | from markdown.extensions import Extension
|
12 | 14 | from pgpdump.packet import SignaturePacket
|
13 | 15 |
|
@@ -62,6 +64,22 @@ def empty_response():
|
62 | 64 | make_choice = lambda l: [(str(m), str(m)) for m in l] # noqa E741
|
63 | 65 |
|
64 | 66 |
|
| 67 | +def cache_user_page(timeout): |
| 68 | + '''Cache the page only for non-logged in users''' |
| 69 | + |
| 70 | + def decorator(view_func): |
| 71 | + @wraps(view_func, assigned=WRAPPER_ASSIGNMENTS) |
| 72 | + def _wrapped_view(request, *args, **kwargs): |
| 73 | + if request.user.is_authenticated: |
| 74 | + return view_func(request, *args, **kwargs) |
| 75 | + result = cache_page( |
| 76 | + timeout, |
| 77 | + key_prefix=(f"_auth_{request.user.is_authenticated}_")) |
| 78 | + return result(view_func)(request, *args, **kwargs) |
| 79 | + return _wrapped_view |
| 80 | + return decorator |
| 81 | + |
| 82 | + |
65 | 83 | def set_created_field(sender, **kwargs):
|
66 | 84 | '''This will set the 'created' field on any object to the current UTC time
|
67 | 85 | if it is unset.
|
|
0 commit comments