From e75aa2759bc9478d9608dab6761ec39b5012b1d7 Mon Sep 17 00:00:00 2001 From: luxagraf Date: Sun, 7 Nov 2010 16:20:58 -0500 Subject: added new build module, plus updated settings file and contact template to redirect to live site for processing --- apps/build/__init__.py | 0 apps/build/admin.py | 80 +++++++++++++++++++++++++ apps/build/base.py | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ apps/build/models.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 394 insertions(+) create mode 100644 apps/build/__init__.py create mode 100644 apps/build/admin.py create mode 100644 apps/build/base.py create mode 100644 apps/build/models.py (limited to 'apps/build') diff --git a/apps/build/__init__.py b/apps/build/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/build/admin.py b/apps/build/admin.py new file mode 100644 index 0000000..ee6957e --- /dev/null +++ b/apps/build/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/apps/build/base.py b/apps/build/base.py new file mode 100644 index 0000000..6b806b6 --- /dev/null +++ b/apps/build/base.py @@ -0,0 +1,158 @@ +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)) \ No newline at end of file diff --git a/apps/build/models.py b/apps/build/models.py new file mode 100644 index 0000000..65c0149 --- /dev/null +++ b/apps/build/models.py @@ -0,0 +1,156 @@ +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) + -- cgit v1.2.3-70-g09d2