From 91f1ab37b2dda0108e44c840f00d9bb9ca0570ec Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sun, 14 Oct 2012 22:42:13 -0400 Subject: renamed build to builder and delted extraneous files in builder and blog app --- app/blog/models.py | 3 - app/blog/signals.py | 21 ---- app/build/__init__.py | 0 app/build/admin.py | 80 -------------- app/build/base.py | 260 -------------------------------------------- app/build/models.py | 156 -------------------------- app/builder/__init__.py | 0 app/builder/admin.py | 80 ++++++++++++++ app/builder/base.py | 260 ++++++++++++++++++++++++++++++++++++++++++++ app/builder/views.py | 74 +++++++++++++ config/base_urls.py | 2 +- config/settings/settings.py | 1 - 12 files changed, 415 insertions(+), 522 deletions(-) delete mode 100644 app/blog/signals.py delete mode 100644 app/build/__init__.py delete mode 100644 app/build/admin.py delete mode 100644 app/build/base.py delete mode 100644 app/build/models.py create mode 100644 app/builder/__init__.py create mode 100644 app/builder/admin.py create mode 100644 app/builder/base.py create mode 100644 app/builder/views.py diff --git a/app/blog/models.py b/app/blog/models.py index 4ee446e..8e7a513 100644 --- a/app/blog/models.py +++ b/app/blog/models.py @@ -10,8 +10,6 @@ from utils import markdown2 as markdown from photos.models import PhotoGallery from locations.models import Location,Region -#from locations.signals import create_location_item -from blog.signals import update_recent def get_upload_path(self, filename): return "images/post-images/%s/%s" %(datetime.datetime.today().strftime("%Y"), filename) @@ -176,5 +174,4 @@ class LatestFull(Feed): from django.dispatch import dispatcher from django.db.models import signals -signals.post_save.connect(update_recent, sender=Entry) diff --git a/app/blog/signals.py b/app/blog/signals.py deleted file mode 100644 index a8c0556..0000000 --- a/app/blog/signals.py +++ /dev/null @@ -1,21 +0,0 @@ -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 - -from locations.models import Region,Country,Route - -from build.base import BuildWriting,BuildMap - -def update_recent(sender, instance, signal, *args, **kwargs): - if not settings.DEVELOPMENT: - if instance.status == 1: - #update homepage, archives and details - b = BuildWriting() - b.build_homepage() - b.build_archive_pages() - b.build_detail_pages() - #update map - b = BuildMap() - b.build() \ No newline at end of file diff --git a/app/build/__init__.py b/app/build/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/app/build/admin.py b/app/build/admin.py deleted file mode 100644 index ee6957e..0000000 --- a/app/build/admin.py +++ /dev/null @@ -1,80 +0,0 @@ -from django.contrib import admin -from django import forms -from blog.models import Entry, PostImage, Topic -from blog.widgets import AdminImageWidget -from django.contrib.gis.admin import OSMGeoAdmin -from django.contrib.gis.maps.google import GoogleMap -from django.conf import settings - - - -GMAP = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY) - -class EntryAdmin(OSMGeoAdmin): - def formfield_for_dbfield(self, db_field, **kwargs): - if db_field.name == 'thumbnail': - field = forms.FileField(widget=AdminImageWidget) - else: - field = super(EntryAdmin,self).formfield_for_dbfield(db_field,**kwargs) - return field - list_display = ('title', 'pub_date','enable_comments', 'status','region','location','photo_gallery') - search_fields = ['title', 'body_markdown'] - prepopulated_fields = {"slug" : ('title',)} - list_filter = ('pub_date', 'enable_comments', 'status','region','location') - fieldsets = ( - ('Entry', {'fields': ('title','body_markdown', ('location','region'), 'pub_date', ('status','enable_comments'), 'slug','photo_gallery'), 'classes': ('show','extrapretty','wide')}), - ('Pub Location', {'fields': ('point',('image','thumbnail',),'dek', 'topics', 'meta_description','template_name'), 'classes': ('collapse', 'wide')}), - ) - - class Media: - js = ['/media/admin/custom/model.js'] - extra_js = [GMAP.api_url + GMAP.key] - map_template = 'gis/admin/google.html' - #map_template = 'gis/admin/google.html' - # Default GeoDjango OpenLayers map options - # Uncomment and modify as desired - # To learn more about this jargon visit: - # www.openlayers.org - - default_lon = -9314310 - default_lat = 3991847 - default_zoom = 6 - #display_wkt = False - #display_srid = False - #extra_js = [] - #num_zoom = 18 - #max_zoom = False - #min_zoom = False - #units = False - #max_resolution = False - #max_extent = False - #modifiable = True - #mouse_position = True - #scale_text = True - #layerswitcher = True - scrollable = False - #admin_media_prefix = settings.ADMIN_MEDIA_PREFIX - map_width = 700 - map_height = 325 - #map_srid = 4326 - #map_template = 'gis/admin/openlayers.html' - #openlayers_url = 'http://openlayers.org/api/2.6/OpenLayers.js' - #wms_url = 'http://labs.metacarta.com/wms/vmap0' - #wms_layer = 'basic' - #wms_name = 'OpenLayers WMS' - #debug = False - #widget = OpenLayersWidget - - - - - -class PostImageAdmin(admin.ModelAdmin): - list_display = ('title', 'output_tags') - -class TopicAdmin(admin.ModelAdmin): - list_display = ('name', 'slug') - -admin.site.register(Topic, TopicAdmin) -admin.site.register(PostImage, PostImageAdmin) -admin.site.register(Entry, EntryAdmin) diff --git a/app/build/base.py b/app/build/base.py deleted file mode 100644 index 49f1989..0000000 --- a/app/build/base.py +++ /dev/null @@ -1,260 +0,0 @@ -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, 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, 'w') - file.write(object) - file.close() - if ext == 'js': - import jsmin - compressed = jsmin.jsmin(object) - fpath = '%s%s.min.%s' %(path, filename, ext) - file = open(fpath, 'w') - file.write(compressed) - 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 == 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)}) - if page == 0: - self.write_file(base_path,str(response.content)) - self.write_file(path,str(response.content)) - -class BuildWriting(Build): - def build(self): - self.build_detail_pages() - self.build_writing_archives() - self.build_location_archive_pages() - self.build_homepage() - 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 = '%s/%s/' %(entry.pub_date.strftime("%Y/%b/%d").lower(), entry.slug) - self.write_file(path,t) - s = render_to_string('details/entry.txt',c).encode('utf-8') - self.write_file(path,s,'txt') - - - def build_writing_archives(self): - qs = self.get_model_querset() - self.build_archive_pages(qs, 'writing/') - - 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 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 = '%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 = '%s/%s/' %(year,month_name) - self.write_file(fpath,t) - - def build_homepage(self): - 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') - self.write_file('',t) - -class BuildPhotos(Build): - 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() - 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) - -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') - 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) - - 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 = 'projects/data/' - fpath = '%s%s.json' %(path, park.id) - self.write_file(fpath,json) - -class BuildSitemap(Build): - def build(self): - c = Client() - response = c.get('/sitemap.xml') - self.write_file('',str(response.content), 'xml','sitemap') - -class BuildWritingFeed(Build): - def build(self): - qs = 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' %('feed/',) - self.write_file(fpath,t,'xml') - -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') - self.write_file('media/js/',t, '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('map/',t) - -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,'IMAGES_URL':settings.BAKED_IMAGES_URL}) - t = render_to_string('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(fpath,t) - self.write_file(fpath,s,'txt') - - - -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 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) - diff --git a/app/build/models.py b/app/build/models.py deleted file mode 100644 index 65c0149..0000000 --- a/app/build/models.py +++ /dev/null @@ -1,156 +0,0 @@ -import datetime -from django.contrib.gis.db import models -from django.conf import settings -from django.contrib.syndication.feeds import Feed -from django.contrib.sitemaps import Sitemap -from django.template.defaultfilters import truncatewords_html - - -from utils import markdown2 as markdown - -from photos.models import PhotoGallery -from locations.models import Location,Region -#from locations.signals import create_location_item -from blog.signals import update_recent - -def get_upload_path(self, filename): - return "images/post-images/%s/%s" %(datetime.datetime.today().strftime("%Y"), filename) - -def get_tn_path(self, filename): - return "images/post-thumbnail/%s/%s" %(datetime.datetime.today().strftime("%Y"), filename) - - -def markdown_processor(md): - processed = markdown.markdown(md, ['footnotes'],safe_mode = False).split('') - html = processed[0]+processed[1] - lede = processed[0] - return html, lede - -PUB_STATUS = ( - (0, 'Draft'), - (1, 'Published'), - ) - -TEMPLATES = ( - (0, 'single'), - (1, 'double'), - (2, 'single-dark'), - (3, 'double-dark'), - ) -class PostImage(models.Model): - title = models.CharField(max_length=100) - image = models.ImageField(upload_to="%s%s" %(settings.IMAGES_ROOT, datetime.datetime.today().strftime("%Y"))) - - def __unicode__(self): - return self.title - - def output_tags(self): - return force_unicode('%s' % \ - (settings.IMAGES_URL, self.image.url.split('images')[1].split('/',1)[1], self.title)) - - -class Topic(models.Model): - name = models.CharField(max_length=100) - slug = models.SlugField() - - def __unicode__(self): - return self.name - - def get_absolute_url(self): - return "/topics/%s/" % (self.slug) - -class Entry(models.Model): - title = models.CharField(max_length=200) - slug = models.SlugField(unique_for_date='pub_date') - lede = models.TextField(blank=True) - body_html = models.TextField(blank=True) - body_markdown = models.TextField() - dek = models.TextField(null=True,blank=True) - pub_date = models.DateTimeField('Date published') - enable_comments = models.BooleanField(default=True) - point = models.PointField(null=True) - location = models.ForeignKey(Location, null=True) - region = models.ForeignKey(Region, null=True) - status = models.IntegerField(choices=PUB_STATUS, default=0) - photo_gallery = models.ForeignKey(PhotoGallery, blank=True, null=True, verbose_name='photo set') - image = models.FileField(upload_to=get_upload_path, null=True,blank=True) - thumbnail = models.FileField(upload_to=get_tn_path, null=True,blank=True) - meta_description = models.CharField(max_length=256, null=True, blank=True) - topics = models.ManyToManyField(Topic, blank=True) - template_name = models.IntegerField(choices=TEMPLATES, default=0) - - @property - def longitude(self): - '''Get the site's longitude.''' - return self.point.x - - @property - def latitude(self): - '''Get the site's latitude.''' - return self.point.y - - class Meta: - ordering = ('-pub_date',) - get_latest_by = 'pub_date' - verbose_name_plural = 'entries' - - def __unicode__(self): - return self.title - - def get_absolute_url(self): - return "/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug) - - def get_previous_published(self): - return self.get_previous_by_pub_date(status__exact=1) - - def get_next_published(self): - return self.get_next_by_pub_date(status__exact=1) - - - def comment_period_open(self): - return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date - - def get_thumbnail_url(self): - if settings.DEVELOPMENT == True: - return '%s%s' %(settings.IMAGES_URL, self.thumbnail.url[35:]) - else: - return '%s%s' %(settings.IMAGES_URL, self.thumbnail.url[33:]) - - def get_image_url(self): - if settings.DEVELOPMENT == True: - return '%s%s' %(settings.IMAGES_URL, self.image.url[35:]) - else: - return '%s%s' %(settings.IMAGES_URL, self.image.url[33:]) - - def save(self): - html,lede = markdown_processor(self.body_markdown) - self.body_html = html - self.lede = lede - self.dek == markdown.markdown(self.dek, safe_mode = False) - super(Entry, self).save() - -class BlogSitemap(Sitemap): - changefreq = "never" - priority = 1.0 - - def items(self): - return Entry.objects.filter(status=1) - - def lastmod(self, obj): - return obj.pub_date - -class LatestFull(Feed): - title = "Luxagraf: Topographical Writings" - link = "/writing/" - description = "Latest postings to luxagraf.net" - description_template = 'feeds/blog_description.html' - - def items(self): - return Entry.objects.filter(status__exact=1).order_by('-pub_date')[:10] - - -from django.dispatch import dispatcher -from django.db.models import signals - -signals.post_save.connect(update_recent, sender=Entry) - diff --git a/app/builder/__init__.py b/app/builder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/builder/admin.py b/app/builder/admin.py new file mode 100644 index 0000000..ee6957e --- /dev/null +++ b/app/builder/admin.py @@ -0,0 +1,80 @@ +from django.contrib import admin +from django import forms +from blog.models import Entry, PostImage, Topic +from blog.widgets import AdminImageWidget +from django.contrib.gis.admin import OSMGeoAdmin +from django.contrib.gis.maps.google import GoogleMap +from django.conf import settings + + + +GMAP = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY) + +class EntryAdmin(OSMGeoAdmin): + def formfield_for_dbfield(self, db_field, **kwargs): + if db_field.name == 'thumbnail': + field = forms.FileField(widget=AdminImageWidget) + else: + field = super(EntryAdmin,self).formfield_for_dbfield(db_field,**kwargs) + return field + list_display = ('title', 'pub_date','enable_comments', 'status','region','location','photo_gallery') + search_fields = ['title', 'body_markdown'] + prepopulated_fields = {"slug" : ('title',)} + list_filter = ('pub_date', 'enable_comments', 'status','region','location') + fieldsets = ( + ('Entry', {'fields': ('title','body_markdown', ('location','region'), 'pub_date', ('status','enable_comments'), 'slug','photo_gallery'), 'classes': ('show','extrapretty','wide')}), + ('Pub Location', {'fields': ('point',('image','thumbnail',),'dek', 'topics', 'meta_description','template_name'), 'classes': ('collapse', 'wide')}), + ) + + class Media: + js = ['/media/admin/custom/model.js'] + extra_js = [GMAP.api_url + GMAP.key] + map_template = 'gis/admin/google.html' + #map_template = 'gis/admin/google.html' + # Default GeoDjango OpenLayers map options + # Uncomment and modify as desired + # To learn more about this jargon visit: + # www.openlayers.org + + default_lon = -9314310 + default_lat = 3991847 + default_zoom = 6 + #display_wkt = False + #display_srid = False + #extra_js = [] + #num_zoom = 18 + #max_zoom = False + #min_zoom = False + #units = False + #max_resolution = False + #max_extent = False + #modifiable = True + #mouse_position = True + #scale_text = True + #layerswitcher = True + scrollable = False + #admin_media_prefix = settings.ADMIN_MEDIA_PREFIX + map_width = 700 + map_height = 325 + #map_srid = 4326 + #map_template = 'gis/admin/openlayers.html' + #openlayers_url = 'http://openlayers.org/api/2.6/OpenLayers.js' + #wms_url = 'http://labs.metacarta.com/wms/vmap0' + #wms_layer = 'basic' + #wms_name = 'OpenLayers WMS' + #debug = False + #widget = OpenLayersWidget + + + + + +class PostImageAdmin(admin.ModelAdmin): + list_display = ('title', 'output_tags') + +class TopicAdmin(admin.ModelAdmin): + list_display = ('name', 'slug') + +admin.site.register(Topic, TopicAdmin) +admin.site.register(PostImage, PostImageAdmin) +admin.site.register(Entry, EntryAdmin) diff --git a/app/builder/base.py b/app/builder/base.py new file mode 100644 index 0000000..49f1989 --- /dev/null +++ b/app/builder/base.py @@ -0,0 +1,260 @@ +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, 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, 'w') + file.write(object) + file.close() + if ext == 'js': + import jsmin + compressed = jsmin.jsmin(object) + fpath = '%s%s.min.%s' %(path, filename, ext) + file = open(fpath, 'w') + file.write(compressed) + 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 == 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)}) + if page == 0: + self.write_file(base_path,str(response.content)) + self.write_file(path,str(response.content)) + +class BuildWriting(Build): + def build(self): + self.build_detail_pages() + self.build_writing_archives() + self.build_location_archive_pages() + self.build_homepage() + 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 = '%s/%s/' %(entry.pub_date.strftime("%Y/%b/%d").lower(), entry.slug) + self.write_file(path,t) + s = render_to_string('details/entry.txt',c).encode('utf-8') + self.write_file(path,s,'txt') + + + def build_writing_archives(self): + qs = self.get_model_querset() + self.build_archive_pages(qs, 'writing/') + + 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 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 = '%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 = '%s/%s/' %(year,month_name) + self.write_file(fpath,t) + + def build_homepage(self): + 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') + self.write_file('',t) + +class BuildPhotos(Build): + 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() + 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) + +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') + 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) + + 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 = 'projects/data/' + fpath = '%s%s.json' %(path, park.id) + self.write_file(fpath,json) + +class BuildSitemap(Build): + def build(self): + c = Client() + response = c.get('/sitemap.xml') + self.write_file('',str(response.content), 'xml','sitemap') + +class BuildWritingFeed(Build): + def build(self): + qs = 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' %('feed/',) + self.write_file(fpath,t,'xml') + +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') + self.write_file('media/js/',t, '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('map/',t) + +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,'IMAGES_URL':settings.BAKED_IMAGES_URL}) + t = render_to_string('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(fpath,t) + self.write_file(fpath,s,'txt') + + + +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 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) + diff --git a/app/builder/views.py b/app/builder/views.py new file mode 100644 index 0000000..0dee430 --- /dev/null +++ b/app/builder/views.py @@ -0,0 +1,74 @@ +from django.http import HttpResponseRedirect +from django.core.urlresolvers import reverse +from django.shortcuts import render_to_response,get_object_or_404,redirect + +from django.template import RequestContext + +from builder.base import BuildWriting, BuildWritingFeed, BuildMap, BuildPages, BuildPhotos,BuildAbout,BuildProjects,BuildSitemap,BuildContact +def do_build(request): + section = request.GET.get('id', '') + if section != '': + + #build writing archives + if section == 'writing': + BuildWriting().build() + context = {'message': 'Writing Posts to Disk',} + + #build photo galleries + elif section == 'photos': + BuildPhotos().build() + context = {'message': 'Writing Photo Galleries to Disk',} + + #build project pages + elif section == 'projects': + BuildProjects().build() + context = {'message': 'Writing Project pages to Disk',} + + #build pages + elif section == 'pages': + BuildPages().build() + context = {'message': 'Writing Pages to Disk',} + + #build map + elif section == 'map': + BuildMap().build() + context = {'message': 'Writing Map to Disk',} + + #build Writing RSS Feed + elif section == 'feed': + BuildWritingFeed().build() + context = {'message': 'Writing RSS Feed to Disk',} + + #build Sitemap + elif section == 'sitemap': + BuildSitemap().build() + context = {'message': 'Writing Sitemap to Disk',} + + """ + elif section == 'all': + g.build_entire_site() + context = {'message': 'Writing Entire Site to disk',} + elif section == 'newpost': + g.create_new_post() + #build writing archives + #BuildWriting().build() + #build map + #build homepage + #build feed + #build sitemap + context = {'message': 'Publishing New Post',} + + elif section == 'home': + g.create_homepage() + context = {'message': 'Writing Homepage to Disk',} + + """ + + else: + context = {} + #return redirect('/admin/', message='test') + return render_to_response('admin/message.html', context, context_instance = RequestContext(request)) + #return HttpResponseRedirect(reverse('admin:index')) + + + diff --git a/config/base_urls.py b/config/base_urls.py index 91ab63d..b2cd642 100644 --- a/config/base_urls.py +++ b/config/base_urls.py @@ -43,7 +43,7 @@ urlpatterns += patterns('', urlpatterns += patterns('', (r'^admin/doc/', include('django.contrib.admindocs.urls')), #(r'^admin/filebrowser/', include('filebrowser.urls')), - (r'^admin/build/.*', 'static_generator.views.do_build'), + (r'^admin/build/.*', 'builder.views.do_build'), (r'^admin/', include(admin.site.urls),), (r'^grappelli/', include('grappelli.urls')), #(r'^feeds/(?P.*)/$', 'django.contrib.syndication.views.feed', {'feed_dict': feeds}), diff --git a/config/settings/settings.py b/config/settings/settings.py index 8b71e3c..efd7e74 100644 --- a/config/settings/settings.py +++ b/config/settings/settings.py @@ -144,7 +144,6 @@ INSTALLED_APPS = ( 'templatetags', 'projects', 'guide', - 'static_generator', 'pages' ) -- cgit v1.2.3