Commits

Dan Watson committed 8723960

Fixed attachments, added basic display to the stream theme

Comments (0)

Files changed (8)

 class AttachmentInline (admin.TabularInline):
 	model = Attachment
 	extra = 1
+
 class MetaInline (admin.TabularInline):
 	model = LeafMeta
 	extra = 1
 	date_hierarchy = 'pub_date'
 	prepopulated_fields = {'slug': ('title',)}
 	search_fields = ('title', 'summary', 'content')
-	inlines = (AttachmentInline, MetaInline,)
+	inlines = (AttachmentInline, MetaInline)
 
 	def formfield_for_dbfield(self, db_field, **kwargs):
 		if db_field.name == 'author_name' and 'request' in kwargs:
 	exp_date = models.DateTimeField(verbose_name=_('expiration date'), blank=True, null=True)
 	mod_date = models.DateTimeField(verbose_name=_('modification date'), editable=False)
 	changefreq = models.CharField(verbose_name=_('sitemap change frequency'), max_length=7, choices=CHANGEFREQ_CHOICES, default='monthly', help_text=_('How often should search engines index this entry?'))
-	priority = models.FloatField(verbose_name=_('sitemap priority'), choices=PRIORITY_CHOICES, default=0.5, help_text=_('Relative importance to other entries on the site.  1.0 is highest.'))
+	priority = models.FloatField(verbose_name=_('sitemap priority'), choices=PRIORITY_CHOICES, default=0.5, help_text=_('Relative importance to other entries on the site.	1.0 is highest.'))
 	tags = models.ManyToManyField(Tag, blank=True, related_name='leaves')
 	custom_url = models.CharField(max_length=100, blank=True, help_text=_('A custom URL for this page. Should start and end with a slash.'))
 	page_template = models.CharField(max_length=200, blank=True, help_text=_("(Optional) A custom template for this leaf's page."))
 		return self.key
 
 def attachment_checksum(f):
-	"""
-	Compute the md5 hash of the specified file. Adapted from:
-	http://thejaswihr.blogspot.com/2008/06/python-md5-checksum-of-file.html
-	"""
 	try:
-		f.open(mode='rb')
-	except IOError:
-		return 'No md5 available.'
-	content = f.readlines()
-	f.close()
-	m = hashlib.md5()
-	for line in content:
-		m.update(line)
-	return m.hexdigest()
+		h = hashlib.md5()
+		for chunk in f.chunks():
+			h.update(chunk)
+		return h.hexdigest()
+	except:
+		return ''
 
 def attachment_path(instance, filename):
-	return instance.leaf.pk
+	return 'attachments/%s/%s' % (instance.leaf.pk, filename)
 
 class Attachment (models.Model, Translatable):
 	leaf = models.ForeignKey(Leaf, related_name='attachments')

leaves/themes/stream/static/stream/css/style.css

     margin-bottom: 3px;
 }
 
+.attachments {
+	list-style-type: none;
+	padding: 0px;
+	margin: 20px 0px 0px 0px;
+}
+.attachments li {
+	margin: 0px;
+	padding: 0px 0px 0px 20px;
+	background-image: url(../img/file.png);
+	background-repeat: no-repeat;
+	height: 16px;
+	line-height: 16px;
+}
 #comments {
     margin-left: 220px;
 }

leaves/themes/stream/static/stream/img/file.png

Added
New image

leaves/themes/stream/templates/leaf_attachments.html

+{% if leaf.attachments.count %}
+<ul class="attachments">
+	{% for att in leaf.attachments.all %}
+	<li><a href="{{ att.get_absolute_url }}">{{ att.filename }}</a></li>
+	{% endfor %}
+</ul>
+{% endif %}

leaves/themes/stream/templates/leaf_detail.html

 <div class="leaf-content {{ leaf.subclass }}-content detail">
     {% leaf_content leaf %}
 </div>
+{% include "leaf_attachments.html" %}
 {% include "leaf_footer.html" with selector="detail" %}
 	url(r'^tag/(?P<slug>[\w-]+)/$', 'view_tag', name='view-tag'),
 	url(r'^author/(?P<username>[\w-]+)/$', 'view_author', name='view-author'),
 	url(r'^leaf/(?P<leaf_id>\d+)/$', 'view_leaf', name='view-leaf'),
-	url(r'^files/(?P<attachment_id>\d+)/(?P<filename>.*)$', 'view_attachment', name='view-attachment'),
+	url(r'^files/(?P<attachment_id>\d+)/(?P<filename>[^/]*)/?$', 'view_attachment', name='view-attachment'),
 	url(r'^archive/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'view_archive', name='view-archive'),
 	url(r'^archive/(?P<year>\d{4})/$', 'view_archive', name='view-archive'),
 )
 from django.contrib.auth.models import User
 from django.conf import settings
 from django.http import HttpResponse, Http404
-from leaves.models import Leaf, Tag
+from leaves.models import Leaf, Tag, Attachment
 from leaves.forms import CommentForm
 from leaves.utils import get_page
 import mimetypes
 import datetime
+import os
 
 def homepage(request):
 	mod_path, func_name = settings.LEAVES_HOMEPAGE_VIEW.rsplit('.', 1)
 def view_attachment(request, attachment_id, filename):
 	attachment = get_object_or_404(Attachment, pk=attachment_id)
 	try:
-		f = attachment.attachment.open(mode='rb')
-		mime = mimetypes.guess_type(f.name)[0] 
-		data = f.read()
-		resp = HttpResponse(data, mimetype=mime)
+		name = os.path.basename(attachment.attachment.name)
+		mime = mimetypes.guess_type(name)[0] 
+		resp = HttpResponse(attachment.attachment, mimetype=mime)
 		if attachment.filename:
-			resp['Content-Disposition'] = 'inline; filename="%s"' % attachment.filename
-		elif attachment.title:
-			resp['Content-Disposition'] = 'inline; filename="%s"' % attachment.title
-		else:
-			resp['Content-Disposition'] = 'inline; filename="%d"' % attachment.pk
+			name = attachment.filename
+		resp['Content-Disposition'] = 'inline; filename="%s"' % name
 		return resp
 	except Exception, ex:
+		print str(ex)
 		return HttpResponse('')	
 
 def leaf_search(request):