1
1
import functools
2
+ import datetime
2
3
import re
3
4
import os
4
5
7
8
from django .conf import settings
8
9
from django .core .exceptions import ImproperlyConfigured
9
10
from django .core .files import File
11
+ from django .db .models import Max
10
12
11
13
from pages .models import Page , Image
12
14
13
15
PEP_TEMPLATE = 'pages/pep-page.html'
14
16
pep_url = lambda num : 'dev/peps/pep-{}/' .format (num )
15
17
16
18
17
- def check_paths (func ):
18
- """Ensure that our PEP_REPO_PATH is setup correctly."""
19
- @functools .wraps (func )
20
- def wrapped (* args , ** kwargs ):
21
- if not hasattr (settings , 'PEP_REPO_PATH' ):
22
- raise ImproperlyConfigured ('No PEP_REPO_PATH in settings' )
23
- if not os .path .exists (settings .PEP_REPO_PATH ):
24
- raise ImproperlyConfigured ('Path set as PEP_REPO_PATH does not exist' )
25
- return func (* args , ** kwargs )
26
- return wrapped
19
+ def get_peps_last_updated ():
20
+ last_update = Page .objects .filter (
21
+ path__startswith = 'dev/peps' ,
22
+ ).aggregate (Max ('updated' )).get ('updated__max' )
23
+ if last_update is None :
24
+ return datetime .datetime (
25
+ 1970 , 1 , 1 , tzinfo = datetime .timezone (
26
+ datetime .timedelta (0 )
27
+ )
28
+ )
29
+ return last_update
27
30
28
31
29
- @check_paths
30
- def convert_pep0 ():
32
+ def convert_pep0 (artifact_path ):
31
33
"""
32
34
Take existing generated pep-0000.html and convert to something suitable
33
35
for a Python.org Page returns the core body HTML necessary only
34
36
"""
35
- pep0_path = os .path .join (settings . PEP_REPO_PATH , 'pep-0000.html' )
37
+ pep0_path = os .path .join (artifact_path , 'pep-0000.html' )
36
38
pep0_content = open (pep0_path ).read ()
37
39
data = convert_pep_page (0 , pep0_content )
38
40
if data is None :
39
41
return
40
42
return data ['content' ]
41
43
42
44
43
- def get_pep0_page (commit = True ):
45
+ def get_pep0_page (artifact_path , commit = True ):
44
46
"""
45
47
Using convert_pep0 above, create a CMS ready pep0 page and return it
46
48
47
49
pep0 is used as the directory index, but it's also an actual pep, so we
48
50
return both Page objects.
49
51
"""
50
- pep0_content = convert_pep0 ()
52
+ pep0_content = convert_pep0 (artifact_path )
51
53
if pep0_content is None :
52
54
return None , None
53
55
pep0_page , _ = Page .objects .get_or_create (path = 'dev/peps/' )
@@ -88,7 +90,6 @@ def fix_headers(soup, data):
88
90
return soup , data
89
91
90
92
91
- @check_paths
92
93
def convert_pep_page (pep_number , content ):
93
94
"""
94
95
Handle different formats that pep2html.py outputs
@@ -163,12 +164,12 @@ def convert_pep_page(pep_number, content):
163
164
return data
164
165
165
166
166
- def get_pep_page (pep_number , commit = True ):
167
+ def get_pep_page (artifact_path , pep_number , commit = True ):
167
168
"""
168
169
Given a pep_number retrieve original PEP source text, rst, or html.
169
170
Get or create the associated Page and return it
170
171
"""
171
- pep_path = os .path .join (settings . PEP_REPO_PATH , 'pep-{}.html' .format (pep_number ))
172
+ pep_path = os .path .join (artifact_path , 'pep-{}.html' .format (pep_number ))
172
173
if not os .path .exists (pep_path ):
173
174
print ("PEP Path '{}' does not exist, skipping" .format (pep_path ))
174
175
return
@@ -177,7 +178,7 @@ def get_pep_page(pep_number, commit=True):
177
178
if pep_content is None :
178
179
return None
179
180
pep_rst_source = os .path .join (
180
- settings . PEP_REPO_PATH , 'pep-{}.rst' .format (pep_number ),
181
+ artifact_path , 'pep-{}.rst' .format (pep_number ),
181
182
)
182
183
pep_ext = '.rst' if os .path .exists (pep_rst_source ) else '.txt'
183
184
source_link = 'https://github.com/python/peps/blob/master/pep-{}{}' .format (
@@ -198,8 +199,8 @@ def get_pep_page(pep_number, commit=True):
198
199
return pep_page
199
200
200
201
201
- def add_pep_image (pep_number , path ):
202
- image_path = os .path .join (settings . PEP_REPO_PATH , path )
202
+ def add_pep_image (artifact_path , pep_number , path ):
203
+ image_path = os .path .join (artifact_path , path )
203
204
if not os .path .exists (image_path ):
204
205
print ("Image Path '{}' does not exist, skipping" .format (image_path ))
205
206
return
@@ -213,26 +214,15 @@ def add_pep_image(pep_number, path):
213
214
# Find existing images, we have to loop here as we can't use the ORM
214
215
# to query against image__path
215
216
existing_images = Image .objects .filter (page = page )
216
- MISSING = False
217
- FOUND = False
218
217
218
+ FOUND = False
219
219
for image in existing_images :
220
- image_root_path = os .path .join (settings .MEDIA_ROOT , page .path , path )
221
-
222
- if image .image .path .endswith (path ):
220
+ if image .image .name .endswith (path ):
223
221
FOUND = True
224
- # File is missing on disk, recreate
225
- if not os .path .exists (image_root_path ):
226
- MISSING = image
227
-
228
222
break
229
223
230
- if not FOUND or MISSING :
231
- image = None
232
- if MISSING :
233
- image = MISSING
234
- else :
235
- image = Image (page = page )
224
+ if not FOUND :
225
+ image = Image (page = page )
236
226
237
227
with open (image_path , 'rb' ) as image_obj :
238
228
image .image .save (path , File (image_obj ))
@@ -243,17 +233,16 @@ def add_pep_image(pep_number, path):
243
233
soup = BeautifulSoup (page .content .raw , 'lxml' )
244
234
for img_tag in soup .findAll ('img' ):
245
235
if img_tag ['src' ] == path :
246
- img_tag ['src' ] = os . path . join ( settings . MEDIA_URL , page . path , path )
236
+ img_tag ['src' ] = image . image . url
247
237
248
238
page .content .raw = str (soup )
249
239
page .save ()
250
240
251
241
return image
252
242
253
243
254
- @check_paths
255
- def get_peps_rss ():
256
- rss_feed = os .path .join (settings .PEP_REPO_PATH , 'peps.rss' )
244
+ def get_peps_rss (artifact_path ):
245
+ rss_feed = os .path .join (artifact_path , 'peps.rss' )
257
246
if not os .path .exists (rss_feed ):
258
247
return
259
248
0 commit comments