Commits

Dan Watson  committed 1b44581

Show attachment size, update num_downloads

  • Participants
  • Parent commits 8723960

Comments (0)

Files changed (4)

File leaves/models.py

 import datetime
 import inspect
 import hashlib
+import os
 
 # Django comes with a list of languages it supports, so we use that. We build
 # our own TRANSLATED_LANGUAGES list so that the language names are translated in
 class Attachment (models.Model, Translatable):
 	leaf = models.ForeignKey(Leaf, related_name='attachments')
 	attachment = models.FileField(upload_to=attachment_path)
-	filename = models.CharField(max_length=200)
-	title = models.CharField(max_length=200)
+	filename = models.CharField(max_length=200, blank=True)
+	title = models.CharField(max_length=200, blank=True)
 	md5_checksum = models.CharField(max_length=32, editable=False)
 	num_downloads = models.PositiveIntegerField(default=0, editable=False)
 	rank = models.IntegerField(default=0, help_text=_('Used for ordering attachments.'))
 
 	def __unicode__(self):
-		return self.filename
+		if self.title:
+			return self.title
+		elif self.filename:
+			return self.filename
+		else:
+			return os.path.basename(self.attachment.name)
 
 	def save(self, **kwargs):
-		self.md5_checksum = attachment_checksum(self.attachment)
+		if kwargs.pop('checksum', True):
+			self.md5_checksum = attachment_checksum(self.attachment)
 		super(Attachment, self).save(**kwargs)
 
 	@models.permalink

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

 }
 .attachments li {
 	margin: 0px;
-	padding: 0px 0px 0px 20px;
+	padding: 0px 0px 6px 20px;
 	background-image: url(../img/file.png);
 	background-repeat: no-repeat;
 	height: 16px;

File 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>
+	<li><a href="{{ att.get_absolute_url }}">{{ att }}</a> ({{ att.attachment.size|filesizeformat }})</li>
 	{% endfor %}
 </ul>
 {% endif %}

File leaves/views.py

 		if attachment.filename:
 			name = attachment.filename
 		resp['Content-Disposition'] = 'inline; filename="%s"' % name
+		attachment.num_downloads += 1
+		attachment.save(checksum=False)
 		return resp
 	except Exception, ex:
-		print str(ex)
 		return HttpResponse('')	
 
 def leaf_search(request):