summaryrefslogtreecommitdiff
path: root/apps/blog
diff options
context:
space:
mode:
Diffstat (limited to 'apps/blog')
-rw-r--r--apps/blog/__init__.py0
-rw-r--r--apps/blog/admin.py73
-rw-r--r--apps/blog/fields.py7
-rw-r--r--apps/blog/models.py101
-rw-r--r--apps/blog/urls.py15
-rw-r--r--apps/blog/views.py65
-rw-r--r--apps/blog/widgets.py32
7 files changed, 293 insertions, 0 deletions
diff --git a/apps/blog/__init__.py b/apps/blog/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/apps/blog/__init__.py
diff --git a/apps/blog/admin.py b/apps/blog/admin.py
new file mode 100644
index 0000000..37aab8d
--- /dev/null
+++ b/apps/blog/admin.py
@@ -0,0 +1,73 @@
+from django.contrib import admin
+from django import forms
+from blog.models import Entry, PostImage
+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')
+ 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'), 'tags', 'slug'), 'classes': ('show','extrapretty','wide')}),
+ ('Pub Location', {'fields': ('point',('thumbnail',),'dek'), 'classes': ('collapse', 'wide')}),
+ )
+ extra_js = [GMAP.api_url + GMAP.key]
+ 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')
+
+
+admin.site.register(PostImage, PostImageAdmin)
+admin.site.register(Entry, EntryAdmin)
diff --git a/apps/blog/fields.py b/apps/blog/fields.py
new file mode 100644
index 0000000..bab8ad3
--- /dev/null
+++ b/apps/blog/fields.py
@@ -0,0 +1,7 @@
+from django import forms
+from blog.widgets import AdminImageWidget
+
+
+class FileUploadForm(forms.ModelForm):
+ upload = forms.FileField(widget=AdminImageWidget)
+
diff --git a/apps/blog/models.py b/apps/blog/models.py
new file mode 100644
index 0000000..d9cea69
--- /dev/null
+++ b/apps/blog/models.py
@@ -0,0 +1,101 @@
+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
+
+
+import markdown2 as markdown
+from tagging.fields import TagField
+from tagging.models import Tag
+
+from photos.models import PhotoGallery
+from locations.models import Location,Region
+#from locations.signals import create_location_item
+
+def get_upload_path(self, filename):
+ return "%s/post-thumbs/%s/%s" %(settings.IMAGES_ROOT, datetime.datetime.today().strftime("%Y"), filename)
+
+def markdown_processor(md):
+ processed = markdown.markdown(md, safe_mode = False).split('<break>')
+ html = processed[0]+processed[1]
+ lede = processed[0]
+ return html, lede
+
+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('<img src="%s%s" alt="%s" class="postpic"/>' % \
+ (settings.IMAGES_URL, self.image.url.split('images')[1].split('/',1)[1], self.title))
+
+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')
+ tags = TagField()
+ enable_comments = models.BooleanField(default=True)
+ point = models.PointField(null=True)
+ location = models.ForeignKey(Location, null=True)
+ region = models.ForeignKey(Region, null=True)
+ PUB_STATUS = (
+ (0, 'Draft'),
+ (1, 'Published'),
+ )
+ status = models.IntegerField(choices=PUB_STATUS, default=0)
+ photo_gallery = models.ForeignKey(PhotoGallery, blank=True, null=True, verbose_name='photo set')
+ thumbnail = models.FileField(upload_to=get_upload_path, null=True,blank=True)
+
+ 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 get_tags(self):
+ return Tag.objects.get_for_object(self)
+
+ def comment_period_open(self):
+ return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date
+
+ def get_thumbnail_url(self):
+ return '%s%s' %(settings.IMAGES_URL, self.thumbnail.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
+
+#signals.post_save.connect(create_location_item, sender=Entry) \ No newline at end of file
diff --git a/apps/blog/urls.py b/apps/blog/urls.py
new file mode 100644
index 0000000..57a3e7a
--- /dev/null
+++ b/apps/blog/urls.py
@@ -0,0 +1,15 @@
+from django.conf.urls.defaults import *
+from django.views.generic.list_detail import object_list
+from django.views.generic.simple import redirect_to
+
+from blog.models import Entry
+
+
+
+urlpatterns = patterns('',
+
+ (r'(?P<slug>[-\w]+)/(?P<page>\d+)/$', 'blog.views.entry_list_by_area'),
+ (r'(?P<page>\d+)/$', 'blog.views.entry_list'),
+ (r'(?P<slug>[-\w]+)/$', redirect_to, {'url': '/writing/%(slug)s/1/'}),
+ (r'', redirect_to, {'url': '/writing/1/'}),
+)
diff --git a/apps/blog/views.py b/apps/blog/views.py
new file mode 100644
index 0000000..9641813
--- /dev/null
+++ b/apps/blog/views.py
@@ -0,0 +1,65 @@
+from django.shortcuts import render_to_response,get_object_or_404
+from django.template import RequestContext
+from django.views.generic.date_based import object_detail
+from django.views.generic.list_detail import object_list
+from django.http import Http404
+
+
+from blog.models import Entry
+from locations.models import Region, Country
+
+def home(request):
+ featured = Entry.objects.filter(status__exact=1).order_by('-pub_date')[:1].get()
+ context = {
+ 'featured': featured,
+ 'object_list': Entry.objects.all().exclude(id=featured.id).order_by('-pub_date')[:5],
+ }
+ return render_to_response('archives/homepage.html', context, context_instance = RequestContext(request))
+
+
+def entry_detail(request, year, month, day, slug):
+ obj = get_object_or_404(Entry, slug__exact=slug)
+ photos = {}
+ if obj.photo_gallery:
+ photos = Photo.objects.filter(set__exact=obj.get().photo_gallery.id)[:9]
+ extra = {'photos':photos,}
+ return render_to_response('details/entry.html', {'object': obj,'photos':photos}, context_instance=RequestContext(request))
+
+
+"""
+List of all writing
+"""
+def entry_list(request,page):
+ request.page_url = '/writing/%d/'
+ request.page = int(page)
+ qs = Entry.objects.filter(status__exact=1).order_by('-pub_date')
+ context = {
+ 'country_list': Country.objects.filter(visited=True),
+ 'region_list': Region.objects.all()
+ }
+ return object_list(request, queryset=qs, template_name='archives/writing.html',
+ extra_context=context)
+
+
+"""
+Grabs entries by region or country
+"""
+def entry_list_by_area(request,slug,page):
+ request.page_url = '/writing/'+slug+'/%d/'
+ request.page = int(page)
+ try:
+ region = Region.objects.get(slug__exact=slug)
+ qs = Entry.objects.filter(status__exact=1,region = region).order_by('-pub_date')
+ except:
+ region = Country.objects.get(slug__exact=slug)
+ qs = Entry.objects.filter(status__exact=1,location__state__country = region).order_by('-pub_date')
+ if not region:
+ raise Http404
+ context = {
+ 'region': region,
+ 'country_list': Country.objects.filter(visited=True),
+ 'region_list': Region.objects.all()
+ }
+ return object_list(request, queryset=qs, template_name='archives/writing.html',
+ extra_context=context)
+
diff --git a/apps/blog/widgets.py b/apps/blog/widgets.py
new file mode 100644
index 0000000..a9451e7
--- /dev/null
+++ b/apps/blog/widgets.py
@@ -0,0 +1,32 @@
+from django.contrib.admin.widgets import AdminFileWidget
+from django.utils.translation import ugettext as _
+from django.utils.safestring import mark_safe
+from django.conf import settings
+from PIL import Image
+import os
+
+try:
+ from sorl.thumbnail.main import DjangoThumbnail
+ def thumbnail(image_path):
+ t = DjangoThumbnail(relative_source=image_path, requested_size=(200,200))
+ return u'<img src="%s" alt="%s" />' % (t.absolute_url, image_path)
+except ImportError:
+ def thumbnail(image_path):
+ absolute_url = os.path.join(settings.IMAGES_URL, image_path)
+ return u'<img src="%s" alt="%s" />' % (absolute_url, image_path)
+
+class AdminImageWidget(AdminFileWidget):
+ """
+ A FileField Widget that displays an image instead of a file path
+ if the current file is an image.
+ """
+ def render(self, name, value, attrs=None):
+ output = []
+ file_name = str(value)
+ if file_name:
+ file_path = '%s' % (file_name)
+ output.append('<a target="_blank" href="%s">%s</a><br />%s <a target="_blank" href="%s">%s</a><br />%s ' % \
+ (file_path, thumbnail(file_name), _('Currently:'), file_path, file_name, _('Change:')))
+
+ output.append(super(AdminFileWidget, self).render(name, value, attrs))
+ return mark_safe(u''.join(output))