File tree 4 files changed +37
-0
lines changed
4 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Intentionally left blank
Original file line number Diff line number Diff line change
1
+ import requests
2
+
3
+ from django .conf import settings
4
+
5
+
6
+ def purge_url (path ):
7
+ """
8
+ Purge a Fastly.com URL given a path. path argument must begin with a slash
9
+ """
10
+ if settings .DEBUG :
11
+ return
12
+
13
+ api_key = getattr (settings , 'FASTLY_API_KEY' , None )
14
+ if api_key :
15
+ response = requests .request (
16
+ 'PURGE' ,
17
+ 'https://www.python.org{}' .format (path ),
18
+ headers = {'Fastly-Key' : api_key },
19
+ )
20
+ return response
21
+
22
+ return None
Original file line number Diff line number Diff line change 8
8
9
9
import os
10
10
import re
11
+
11
12
from django .conf import settings
12
13
from django .core import validators
13
14
from django .db import models
15
+ from django .db .models .signals import post_save
16
+ from django .dispatch import receiver
17
+
14
18
from markupfield .fields import MarkupField
15
19
from cms .models import ContentManageable
20
+ from fastly .utils import purge_url
16
21
from .managers import PageManager
17
22
18
23
DEFAULT_MARKUP_TYPE = getattr (settings , 'DEFAULT_MARKUP_TYPE' , 'restructuredtext' )
@@ -73,6 +78,15 @@ def get_absolute_url(self):
73
78
return "/{}/" .format (self .path )
74
79
75
80
81
+ @receiver (post_save , sender = Page )
82
+ def purge_fastly_cache (sender , instance , ** kwargs ):
83
+ """
84
+ Purge fastly.com cache if in production and the page is published.
85
+ Requires settings.FASTLY_API_KEY being set
86
+ """
87
+ purge_url ('/{}' .format (instance .path ))
88
+
89
+
76
90
def page_image_path (instance , filename ):
77
91
return os .path .join (settings .MEDIA_ROOT , instance .page .path , filename )
78
92
You can’t perform that action at this time.
0 commit comments