Skip to content

Commit

Permalink
Mark is_draft things as no cache by Cloudflare
Browse files Browse the repository at this point in the history
Refs #488
  • Loading branch information
simonw committed Sep 20, 2024
1 parent bf27c14 commit 9cb5f4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions blog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def test_draft_items_not_displayed(self):
response2 = self.client.get(obj.get_absolute_url())
self.assertContains(response2, robots_fragment)
self.assertContains(response2, draft_warning_fragment)
assert (
response2.headers["cache-control"]
== "private, no-cache, no-store, must-revalidate"
)

# Publish it
obj.is_draft = False
Expand All @@ -204,6 +208,7 @@ def test_draft_items_not_displayed(self):
response3 = self.client.get(obj.get_absolute_url())
self.assertNotContains(response3, robots_fragment)
self.assertNotContains(response3, draft_warning_fragment)
assert "cache-control" not in response3.headers

counts2 = json.loads(self.client.get("/tags-autocomplete/?q=testing").content)
assert counts2 == {
Expand Down
12 changes: 11 additions & 1 deletion blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
BLACKLISTED_TAGS = ("quora", "flash", "resolved", "recovered")


def set_no_cache(response):
response["Cache-Control"] = "private, no-cache, no-store, must-revalidate"
response["Pragma"] = "no-cache"
response["Expires"] = "0"
return response


def archive_item(request, year, month, day, slug):
if day.startswith("0"):
day = day.lstrip("0")
Expand Down Expand Up @@ -86,7 +93,7 @@ def archive_item(request, year, month, day, slug):
content_type
)

return render(
response = render(
request,
template,
{
Expand All @@ -101,6 +108,9 @@ def archive_item(request, year, month, day, slug):
"is_draft": obj.is_draft,
},
)
if obj.is_draft:
set_no_cache(response)
return response

# If we get here, non of the views matched
raise Http404
Expand Down

0 comments on commit 9cb5f4c

Please sign in to comment.