summaryrefslogtreecommitdiff
path: root/app/build/base.py
blob: f6df69d1a29869a401c13dd296923ed43d83b58f (plain)
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
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.conf import settings
from django.template import Context
from django.db.models import get_model
from django.conf import settings


class Build():
    def write_file(self, path, object):
        file = open(path, 'w')
        file.write(object)
        file.close()

class BuildSitemap(Build):
    def build(self):
        c = Client()
        response = c.get('/sitemap.xml')
        fpath = '%ssitemap.xml' %(settings.BAKED_ROOT)
        self.write_file(fpath,str(response.content))

class BuildWriting(Build):


    def build(self):
        self.build_detail_pages()
        self.build_archive_pages()
        self.build_location_archive_pages()
        self.build_homepage()
        
    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 = '%s%s/%s/' %(settings.BAKED_ROOT, entry.pub_date.strftime("%Y/%b/%d").lower(), entry.slug)
            if not os.path.isdir(path):
                os.makedirs(path)
            fpath = '%sindex.html' %(path)
            self.write_file(fpath,t)

    def build_archive_pages(self, qs=None, extra='writing/', paginate_by=10):
        if qs == None:
            qs = self.get_model_querset()
        c = Client()
        pages = ceil(Decimal(qs.count())/Decimal(paginate_by))
        print pages
        for page in range(int(pages)):
            base_path = '%s%s' %(settings.BAKED_ROOT, extra)
            path = '%s%s/' %(base_path, page+1)
            if not os.path.isdir(path):
                os.makedirs(path)
            url = '/%s%s/' %(extra, str(page+1))
            page_url = extra+'%d/'
            response = c.post(url, {'page_url': page_url, 'page': int(page)})
            fpath = '%sindex.html' %(path)
            if page == 0:
                self.write_file(base_path+'/index.html',str(response.content))
            self.write_file(fpath,str(response.content))
    
    def build_location_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 = 'writing/%s/' %(c.slug)
            self.build_archive_pages(qs, path)
    
    def build_recent_entries(self):
        model = get_model('blog', 'entry')
        qs = {'object_list': model.objects.filter(status__exact=1).order_by('-pub_date')[1:4]}
        c = Context(qs)
        t = render_to_string('bin/recent_entries.html',c).encode('utf-8') 
        fpath = '%s%s' %(settings.PROJ_ROOT,'templates/includes/recent_entries.html')
        self.write_file(fpath,t)
    
    
    
    def build_homepage(self):
        self.build_recent_entries()
        qs = get_model('blog', 'entry').objects.filter(status__exact=1).latest()
        c = Context({'featured':qs,'MEDIA_URL':settings.BAKED_MEDIA_URL, 'IMAGES_URL':settings.BAKED_IMAGES_URL})
        t = render_to_string('archives/homepage.html',c).encode('utf-8') 
        fpath = '%s%s' %(settings.BAKED_ROOT,'index.html')
        self.write_file(fpath,t)


class BuildPhotos(BuildWriting):

    def build(self):
        self.build_photo_archive_pages()
        self.build_detail_pages()
        
    def build_photo_archive_pages(self):
        qs = get_model('photos', 'PhotoGallery').objects.all()
        path = 'photos/'
        self.build_archive_pages(qs, path, 18)
        
    def build_detail_pages(self):
        qs = get_model('photos', 'PhotoGallery').objects.all()
        path = 'photos/galleries/'
        '''
        Grab all the blog posts, render them to a template string and write that out to the filesystem
        '''
        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 = '%sphotos/galleries/%s/' %(settings.BAKED_ROOT, photo.set_slug)
            if not os.path.isdir(path):
                os.makedirs(path)
            fpath = '%sindex.html' %(path)
            self.write_file(fpath,t)



class BuildAbout(Build):
    def build(self):
        model = get_model('chunks', 'Chunk')
        qs = model.objects.filter(key__in=['about_top','about_middle','about_bottom'])
        c = Context({
            'object_list': qs,
            'IMAGES_URL' : settings.BAKED_IMAGES_URL,
            'MEDIA_URL':settings.BAKED_MEDIA_URL
        })
        t = render_to_string('details/about.html',c).encode('utf-8') 
        path = '%sabout/' %(settings.BAKED_ROOT)
        if not os.path.isdir(path):
            os.makedirs(path)
        fpath = '%sindex.html' %(path)
        self.write_file(fpath,t)
        
class BuildMap(Build):
    
    def build_map_templates(self):
        import codecs
        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('bin/map_entry_list.html',c).encode('utf-8') 
        fpath = '%s%s' %(settings.PROJ_ROOT,'media/js/mainmap.js')
        self.write_file(fpath,t)
        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('bin/map_sidebar.html',c).encode('utf-8') 
        fpath = '%s%s' %(settings.PROJ_ROOT,'templates/includes/map_sidebar.html')
        self.write_file(fpath,t)
        
    def build(self):
        self.build_map_templates()
        c = Context({'MEDIA_URL':settings.BAKED_MEDIA_URL,'IMAGES_URL':settings.BAKED_IMAGES_URL})
        t = render_to_string('archives/map.html', c).encode('utf-8') 
        path = '%smap/' %(settings.BAKED_ROOT)
        if not os.path.isdir(path):
            os.makedirs(path)
        fpath = '%sindex.html' %(path)
        self.write_file(fpath,t)
        
class BuildContact(Build):
    def build(self):
        c = Client()
        response = c.get('/contact/')
        path = '%scontact/' %(settings.BAKED_ROOT)
        if not os.path.isdir(path):
            os.makedirs(path)
        fpath = '%sindex.html' %(path)
        self.write_file(fpath,str(response.content))
        
class BuildProjects(Build):

    def build(self):
        self.build_project_archive()
        self.build_project_details()
        self.build_project_data()
        
    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') 
        path = '%sprojects/' %(settings.BAKED_ROOT)
        if not os.path.isdir(path):
            os.makedirs(path)
        fpath = '%sindex.html' %(path)
        self.write_file(fpath,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 = '%sprojects/%s/' %(settings.BAKED_ROOT, proj['slug'])
            if not os.path.isdir(path):
                os.makedirs(path)
            fpath = '%sindex.html' %(path)
            self.write_file(fpath,t)
    def build_project_data(self):
        from projects.shortcuts import render_to_geojson
        model = get_model('projects', 'NationalParks')
        for park in model.objects.filter(visited__exact=True):
            qs = model.objects.filter(pk=park.id)
            json = render_to_geojson(
                qs,
                included_fields=['id',],
                geom_attribute='mpoly',
                mimetype = 'application/json',
                pretty_print=True
            ) 
            json = str(json)
            json ="\n".join(json.splitlines()[3:])
            #print json
            path = '%sprojects/data/' %(settings.BAKED_ROOT)
            if not os.path.isdir(path):
                os.makedirs(path)
            fpath = '%s%s.json' %(path, park.id)
            self.write_file(fpath,json)