Commits

Dan Watson committed b39fc57

Added vanity photo URLs and a way to view originals

Comments (0)

Files changed (5)

leaves/plugins/pages/models.py

 	parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
 	rank = models.IntegerField(default=0, help_text=_('Used for ordering pages.'))
 	navigable = models.BooleanField(default=True, help_text=_('This page will show up in the navigation hierarchy when checked.'))
-	
+
 	objects = models.Manager()
 	on_site = LeafManager()
-	
+
 	class Meta:
 		ordering = ('rank', 'pub_date', 'title')
-	
+
 	def __unicode__(self):
 		return self.title
-	
+
 	@models.permalink
 	def get_absolute_url(self):
 		return ('pages-view', (), {'slug': self.slug})

leaves/plugins/photos/models.py

 from django.utils.translation import ugettext_lazy as _
 from leaves.models import Leaf
 from leaves.managers import LeafManager, RequestSiteManager
+import os
 
 class Photo (Leaf):
 	image = models.ImageField(upload_to='photos/%Y/%m/%d', max_length=200, height_field='height', width_field='width')
 	width = models.PositiveIntegerField(default=0, editable=False)
 	height = models.PositiveIntegerField(default=0, editable=False)
-	
+
 	objects = models.Manager()
 	on_site = LeafManager()
 
 	class Meta:
 		ordering = ('-pub_date', 'title')
 
+	@models.permalink
+	def get_absolute_url(self):
+		return ('photo-view', (), {'photo_id': self.pk})
+
+	@models.permalink
+	def get_original_url(self):
+		return ('photo-original', (), {'photo_id': self.pk, 'filename': self.get_filename()})
+
+	def get_filename(self):
+		return os.path.basename(self.image.name)
+
 class Gallery (Leaf):
 	photos = models.ManyToManyField(Photo, blank=True, related_name='galleries')
-	
+
 	objects = models.Manager()
 	on_site = LeafManager()
 
 	class Meta:
 		ordering = ('-pub_date', 'title')
 		verbose_name_plural = 'galleries'
+
+	@models.permalink
+	def get_absolute_url(self):
+		return ('photo-gallery-view', (), {'gallery_id': self.pk})

leaves/plugins/photos/urls.py

 urlpatterns = patterns('leaves.plugins.photos.views',
 	url(r'^photos/$', 'photo_stream', name='photo-stream'),
 	url(r'^galleries/$', 'galleries', name='photo-galleries'),
-	url(r'^thumbnail/(?P<leaf_id>\d+)/$', 'thumbnail', name='photo-thumbnail'),
+	url(r'^photo/(?P<photo_id>\d+)/$', 'view_photo', name='photo-view'),
+	url(r'^gallery/(?P<gallery_id>\d+)/$', 'view_gallery', name='photo-gallery-view'),
+	url(r'^photo/(?P<photo_id>\d+)/thumbnail/$', 'thumbnail', name='photo-thumbnail'),
+	url(r'^photo/(?P<photo_id>\d+)/original/(?P<filename>[^/]*)/?$', 'original', name='photo-original'),
 )

leaves/plugins/photos/views.py

 from django.shortcuts import get_object_or_404
 from django.template.response import TemplateResponse
 from leaves.plugins.photos.models import Photo, Gallery
+from leaves.views import view_leaf
 from leaves.utils import get_page
 from PIL import Image
+import mimetypes
 import os
 
-def thumbnail(request, leaf_id):
-	photo = get_object_or_404(Photo.on_site.active(), pk=leaf_id)
+def view_photo(request, photo_id):
+	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
+	return view_leaf(request, photo)
+
+def view_gallery(request, gallery_id):
+	gallery = get_object_or_404(Gallery.on_site.active(), pk=gallery_id)
+	return view_leaf(request, gallery)
+
+def thumbnail(request, photo_id):
+	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
 	try:
-		name = '%s_full.png' % leaf_id
+		name = '%s_full.png' % photo_id
 		if 'width' in request.GET:
-			name = '%s_w%s.png' % (leaf_id, request.GET['width'])
+			name = '%s_w%s.png' % (photo_id, request.GET['width'])
 		elif 'height' in request.GET:
-			name = '%s_h%s.png' % (leaf_id, request.GET['height'])
+			name = '%s_h%s.png' % (photo_id, request.GET['height'])
 		thumb_path = os.path.join(settings.MEDIA_ROOT, 'thumbs', name)
 		if os.path.exists(thumb_path):
 			data = open(thumb_path, 'rb').read()
 	except Exception, ex:
 		return HttpResponse('')
 
+def original(request, photo_id, filename):
+	photo = get_object_or_404(Photo.on_site.active(), pk=photo_id)
+	try:
+		name = photo.get_filename()
+		mime = mimetypes.guess_type(name)[0]
+		disp = 'inline'
+		if not mime:
+			mime = 'application/octet-stream'
+			disp = 'attachment'
+		resp = HttpResponse(photo.image, mimetype=mime)
+		resp['Content-Disposition'] = '%s; filename="%s"' % (disp, name)
+		return resp
+	except Exception, ex:
+		return HttpResponse('')
+
 def photo_stream(request):
 	return TemplateResponse(request, 'photos/stream.html', {
 		'page': get_page(request, Photo.on_site.active()),

leaves/themes/stream/templates/photos/photo_detail.html

 <div class="leaf-content {{ leaf.subclass }}-content detail">
     {% leaf_content leaf %}
 	<div class="photo">
-		<a href="{{ leaf.get_url }}" title="{{ leaf.title_trans }}"><img src="{% url photo-thumbnail leaf.pk %}?width=600" alt="{{ leaf.title_trans }}" /></a>
+		<a href="{{ leaf.get_original_url }}" title="{{ leaf.title_trans }}"><img src="{% url photo-thumbnail leaf.pk %}?width=600" alt="{{ leaf.title_trans }}" /></a>
 	</div>
+	<p><a href="{{ leaf.get_original_url }}">View Original</a></p>
 </div>
 {% include "leaf_footer.html" with selector="detail" %}