1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
import os
from math import ceil
from decimal import Decimal
from django.test.client import Client
from django.template.loader import render_to_string
from django.template import Context
from django.db.models import get_model
from django.conf import settings
#from pages.models import PageGenerator
class Build():
def write_file(self, path, text_object, ext='html', filename='index'):
"""
Given a path and object intended to be a webpage, write the page the
disc
"""
path = '%s%s' % (settings.FLATFILES_ROOT, path)
if not os.path.isdir(path):
os.makedirs(path)
fpath = '%s%s.%s' % (path, filename, ext)
file = open(fpath, 'wb')
file.write(text_object)
file.close()
if ext == 'js':
from jsmin import jsmin
fpath = '%s%s.min.%s' % (path, filename, ext)
compressed = jsmin(text_object.decode(encoding='UTF-8'))
with open(fpath, 'wb') as js_file:
minified = js_file.write(compressed.encode('utf-8'))
js_file.close()
def build_archive_pages(self, qs=None, base_path='', paginate_by=10):
"""
Archive Page builder that actually crawls the urls
because we need to be able to pass a request object to the template
"""
if qs is None:
qs = self.get_model_querset()
c = Client()
pages = ceil(Decimal(qs.count()) / Decimal(paginate_by))
for page in range(int(pages)):
path = '%s%s/' % (base_path, page + 1)
url = '/%s%s/' % (base_path, str(page + 1))
page_url = base_path + '%d/'
response = c.post(url, {'page_url': page_url, 'page': int(page), 'builder': True}, HTTP_HOST='127.0.0.1')
if page == 0:
self.write_file(base_path, response.content)
self.write_file(path, response.content)
class BuildAll(Build):
def build(self):
BuildWriting().build()
BuildPhotos().build()
BuildProjects().build()
BuildMap().build()
BuildWritingFeed().build()
BuildSitemap().build()
BuildPages().build()
p.write_files()
class BuildWriting(Build):
def build(self):
self.build_detail_pages()
self.build_writing_archives()
self.build_country_archive_pages()
self.build_region_archive_pages()
self.build_homepage()
self.build_404()
self.writing_year_archives()
self.writing_month_archives()
def get_model_querset(self):
model = get_model('blog', 'entry')
qs = model.objects.filter(status__exact=1)
return qs
def build_detail_pages(self):
'''
Grab all the blog posts, render them to a template string and write that out to the filesystem
'''
qs = self.get_model_querset()
for entry in qs:
c = Context({'object': entry, 'MEDIA_URL': settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL})
t = render_to_string('details/entry.html', c).encode('utf-8')
path = '/jrnl/%s/' %(entry.pub_date.strftime("%Y/%m").lower())
slug = '%s' %(entry.slug)
self.write_file(path, t, 'html', slug)
s = render_to_string('details/entry.txt',c).encode('utf-8')
self.write_file(path, s,'txt', slug)
def build_writing_archives(self):
qs = self.get_model_querset()
self.build_archive_pages(qs, 'jrnl/')
def build_region_archive_pages(self):
model = get_model('locations', 'Region')
blog = get_model('blog', 'entry')
regions = model.objects.all()
for c in regions:
qs = blog.objects.filter(status__exact=1, location__state__country__lux_region=c.id).order_by('-pub_date')
path = 'jrnl/%s/' % (c.slug)
self.build_archive_pages(qs, path)
def build_country_archive_pages(self):
model = get_model('locations', 'Country')
blog = get_model('blog', 'entry')
countries = model.objects.filter(visited=True)
for c in countries:
qs = blog.objects.filter(status__exact=1, location__state__country=c).order_by('-pub_date')
path = 'jrnl/%s/' % (c.slug)
self.build_archive_pages(qs, path)
def writing_year_archives(self):
entry = get_model('blog', 'entry')
years = entry.objects.dates('pub_date', 'year')
for year in years:
year = year.strftime('%Y')
qs = entry.objects.filter(status__exact=1, pub_date__year=year).order_by('-pub_date')
c = Context({'type': 'year', 'date': year, 'object_list': qs})
t = render_to_string('archives/writing_date.html', c).encode('utf-8')
fpath = 'jrnl/%s/' % (year)
self.write_file(fpath, t)
def writing_month_archives(self):
entry = get_model('blog', 'entry')
months = entry.objects.dates('pub_date', 'month')
for m in months:
year = m.strftime('%Y')
month = m.strftime('%m')
month_name = m.strftime('%b')
month_full_name = m.strftime('%B')
qs = entry.objects.filter(status__exact=1, pub_date__year=year,
pub_date__month=month).order_by('-pub_date')
c = Context({'type': 'monthly', 'date': '%s %s' % (
month_full_name, year), 'object_list': qs, })
t = render_to_string('archives/writing_date.html', c).encode('utf-8')
fpath = 'jrnl/%s/%s/' % (year, month)
self.write_file(fpath, t)
def build_homepage(self):
obj = get_model('blog', 'homepagecurrator').objects.get(pk=1)
recent = get_model('blog', 'entry').objects.filter(status__exact=1)[:4]
template = obj.template_name
c = Context({'homepage': obj, 'recent': recent, 'MEDIA_URL': settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL})
t = render_to_string(template, c).encode('utf-8')
self.write_file('', t)
def build_404(self):
c = Context()
t = render_to_string('404.html', c).encode('utf-8')
self.write_file('', t, 'html', '404')
class BuildPhotos(Build):
def build(self):
self.build_photo_archive_pages()
self.build_detail_pages()
self.build_js()
def build_photo_archive_pages(self):
qs = get_model('photos', 'PhotoGallery').objects.all()
self.build_archive_pages(qs, 'photos/', 18)
def build_detail_pages(self):
qs = get_model('photos', 'PhotoGallery').objects.all()
for photo in qs:
c = Context({'object': photo, 'MEDIA_URL':
settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL})
t = render_to_string('details/photo_galleries.html', c).encode('utf-8')
path = 'photos/galleries/%s/' % (photo.set_slug)
self.write_file(path, t)
def build_js(self):
fpath = '%sdesign/templates/js/leaflet-providers.js' % settings.PROJ_ROOT
leaflet_providers_js = open(fpath, 'r').read()
fpath = '%sapp/photos/photos.js' % settings.PROJ_ROOT
photos_js = open(fpath, 'r', encoding='UTF8').read()
js = leaflet_providers_js + photos_js
self.write_file('media/js/', js.encode('utf-8'), 'js', 'photos')
class BuildProjects(Build):
def build(self):
self.build_project_archive()
self.build_project_details()
self.build_project_data()
self.build_gifs()
self.build_np_basejs()
def get_projects(self):
all_proj = []
projects = get_model('projects', 'Project').objects.filter(status__exact=1).order_by('-pub_date')
for proj in projects:
row = {'slug': proj.slug, 'name': proj.model_name}
all_proj.append(row)
return all_proj
def build_project_archive(self):
qs = get_model('projects', 'Project').objects.filter(status__exact=1).order_by('-pub_date')
c = Context({'object_list': qs, 'MEDIA_URL': settings.BAKED_MEDIA_URL,
'IMAGES_URL': settings.BAKED_IMAGES_URL})
t = render_to_string('archives/projects.html', c).encode('utf-8')
self.write_file('projects/', t)
def build_project_details(self):
projects = self.get_projects()
for proj in projects:
model = get_model('projects', proj['name'])
if proj['name'] == 'NationalParks':
qs = model.objects.filter(visited__exact=True).order_by("-date_visited_begin")
else:
qs = model.objects.filter(status__exact=1)
c = Context({
'object_list': qs,
'MEDIA_URL': settings.BAKED_MEDIA_URL,
'IMAGES_URL': settings.BAKED_IMAGES_URL
})
t = render_to_string('details/%s.html' % (proj['slug']), c).encode('utf-8')
path = 'projects/%s/' % (proj['slug'])
self.write_file(path, t)
"""
not sure how to handle projects really, the above doesn't work and
if I just keep writing if/else statements that gets messy, so I guess
functions it is.
"""
def build_gifs(self):
qs = get_model('projects', 'AnimatedGif').objects.all()
for gif in qs:
c = Context({
'object': gif,
'MEDIA_URL': settings.BAKED_MEDIA_URL,
'IMAGES_URL': settings.BAKED_IMAGES_URL
})
t = render_to_string('details/gifs.html', c).encode('utf-8')
path = 'projects/gifs/%s/' % (gif.slug)
self.write_file(path, t)
def build_project_data(self):
model = get_model('projects', 'NationalParks')
for park in model.objects.filter(visited__exact=True):
path = 'projects/data/natparks/'
json = park.mpoly.json
self.write_file(path, json.encode('utf-8'), 'json', park.id)
def build_np_basejs(self):
fpath = '%sdesign/templates/js/leaflet-providers.js' % settings.PROJ_ROOT
leaflet_providers_js = open(fpath, 'r').read()
fpath = '%sapp/projects/natparks.js' % settings.PROJ_ROOT
natparks_js = open(fpath, 'r').read()
js = leaflet_providers_js + natparks_js
self.write_file('media/js/', js.encode('utf-8'), 'js', 'natparks')
class BuildSitemap(Build):
def build(self):
c = Client()
response = c.get('/sitemap.xml', HTTP_HOST='127.0.0.1')
self.write_file('', response.content, 'xml', 'sitemap')
class BuildWritingFeed(Build):
def build(self):
qs = get_model('blog', 'entry').objects.filter(status__exact=1).order_by('-pub_date')[:20]
c = Context({'object_list': qs, 'SITE_URL': settings.SITE_URL})
t = render_to_string('feed.xml', c).encode('utf-8')
fpath = '%s' % ('rss/',)
self.write_file(fpath, t, 'xml')
class BuildPages(Build):
def build(self):
model = get_model('pages', 'page')
pages = model.objects.all()
for page in pages:
c = Context({'object':page,'SITE_URL':settings.SITE_URL, 'MEDIA_URL':settings.BAKED_MEDIA_URL})
t = render_to_string(["details/%s.html" % page.slug, 'details/page.html'],c).encode('utf-8')
s = render_to_string('details/page.txt',c).encode('utf-8')
fpath = '%s' %(page.slug)
self.write_file('', t, 'html', page.slug)
self.write_file('', t, 'txt', page.slug)
class BuildMap(Build):
def build(self):
qs = get_model('blog', 'entry').objects.filter(status__exact=1)
cl = get_model('locations', 'Country').objects.filter(visited=True).exclude(name='default')
rl = get_model('locations', 'Region').objects.all()
rtl = get_model('locations', 'Route').objects.all()
c = Context({
'object_list': qs,
'country_list': cl,
'region_list': rl,
'route_list': rtl,
'MEDIA_URL': settings.BAKED_MEDIA_URL,
'IMAGES_URL': settings.BAKED_IMAGES_URL
})
t = render_to_string('archives/map_data.html', c).encode('utf-8')
fpath = '%sdesign/templates/js/leaflet-providers.js' % settings.PROJ_ROOT
leaflet_providers_js = open(fpath, 'r').read()
js = leaflet_providers_js + t.decode(encoding='utf-8')
self.write_file('media/js/', js.encode('utf-8'), 'js', 'mainmap')
c = Context({
'country_list': cl,
'region_list': rl,
'route_list': rtl,
'MEDIA_URL': settings.BAKED_MEDIA_URL,
'IMAGES_URL': settings.BAKED_IMAGES_URL
})
t = render_to_string('archives/map.html', c).encode('utf-8')
self.write_file('', t, "html",'map')
# Back up entries to markdown text files which are then stored in dropbox and git
class EntryBak(Build):
def get_model_querset(self):
model = get_model('blog', 'entry')
qs = model.objects.filter(status__exact=1)
return qs
def write_file(self, path, text_object):
file = open(path, 'wb')
file.write(text_object)
file.close()
def build_writing_bak(self):
qs = self.get_model_querset()
for obj in qs:
c = Context({'object': obj, 'MEDIA_URL': settings.BAKED_MEDIA_URL, 'IMAGES_URL': settings.BAKED_IMAGES_URL})
path = "%szbak/posts/%s_%s.txt" %(settings.PROJ_ROOT, (obj.pub_date.strftime("%Y-%m-%d").lower()), obj.slug)
t = render_to_string('details/entry-bak.txt', c).encode('utf-8')
self.write_file(path, t)
|