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 BuildWriting(Build): 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.MEDIA_URL}) t = render_to_string('details/entry.html',c).encode('utf-8') path = '%s%s/%s/' %(settings.STATIC_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.STATIC_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('includes/recent_entries_template.html',c) 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.MEDIA_URL}) t = render_to_string('archives/homepage.html',c) fpath = '%s%s' %(settings.STATIC_ROOT,'index.html') self.write_file(fpath,t) class BuildPhotos(BuildWriting): 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.MEDIA_URL}) t = render_to_string('details/photo_galleries.html',c).encode('utf-8') path = '%sphotos/galleries/%s/' %(settings.STATIC_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') c = Context({ 'top': model.objects.get(key='about_top'), 'middle': model.objects.get(key='about_middle'), 'bottom': model.objects.get(key='about_bottom'), 'IMAGES_URL' : settings.IMAGES_URL, 'MEDIA_URL':settings.MEDIA_URL }) t = render_to_string('details/about.html',c).encode('utf-8') path = '%sabout/' %(settings.STATIC_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.MEDIA_URL}) t = render_to_string('includes/map_entry_list_template.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.MEDIA_URL}) t = render_to_string('includes/map_sidebar_template.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.MEDIA_URL,}) t = render_to_string('archives/map.html', c).encode('utf-8') path = '%smap/' %(settings.STATIC_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/') fpath = '%scontact/index.html' %(settings.STATIC_ROOT) self.write_file(fpath,str(response.content))