Inline (some) images attached to emails
authorMagnus Hagander <magnus@hagander.net>
Sun, 6 Jan 2013 21:42:19 +0000 (22:42 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sun, 6 Jan 2013 21:42:19 +0000 (22:42 +0100)
Inline images if they're <75kb in size. Also use a CSS rule to make
sure that they don't overflow the display.

django/archives/mailarchives/models.py
django/archives/mailarchives/templates/message.html
django/media/css/archives.css

index 5325264c9d10c930fcd4bdffc1e372877ce660fe..4ce69e2ae44003047e53facebfc69c82a7013011 100644 (file)
@@ -25,6 +25,16 @@ class Message(models.Model):
        def shortdate(self):
                return self.date.strftime("%Y%m%d%H%M")
 
+       # We explicitly cache the attachments here, so we can use them
+       # multiple times from templates without generating multiple queries
+       # to the database.
+       _attachments = None
+       @property
+       def attachments(self):
+               if not self._attachments:
+                       self._attachments = self.attachment_set.extra(select={'len': 'length(attachment)'}).all()
+               return self._attachments
+
 class ListGroup(models.Model):
        groupid = models.IntegerField(null=False, primary_key=True)
        groupname = models.CharField(max_length=200, null=False, blank=False)
@@ -52,3 +62,11 @@ class Attachment(models.Model):
 
        class Meta:
                db_table = 'attachments'
+
+       def inlineable(self):
+               # Return True if this image should be inlined
+               if self.contenttype in ('image/png', 'image/gif', 'image/jpg', 'image/jpeg'):
+                       # Note! len needs to be set with extra(select=)
+                       if self.len < 75000:
+                               return True
+               return False
index bab68df694443d49490909182e8be8e91cec9c10..d2fb76dbd07ef72902d299bc7f5aa77788a0cd1d 100644 (file)
@@ -65,10 +65,15 @@ $(function(){
 <pre>{{msg.bodytxt|hideallemail|urlize}}</pre>
 
 {%if msg.has_attachment%}
-{%for a in msg.attachment_set.all%}
+{%for a in msg.attachments%}
+{%if a.inlineable%}<a href="/message-id/attachment/{{a.id}}/{{a.filename}}"><img class="attachedimage" src="/message-id/attachment/{{a.id}}/{{a.filename}}" /></a>{%endif%}
+{%if forloop.last%}<hr/>{%endif%}
+{%endfor%}
+
+{%for a in msg.attachments%}
 <div>
 <b>Attachment: <a href="/message-id/attachment/{{a.id}}/{{a.filename}}">{{a.filename}}</a></b><br/>
-Description: {{a.contenttype}}
+Description: {{a.contenttype}} {%if a.inlineable%}(inlined above){%endif%}
 </div>
 {%endfor%}
 {%endif%}
index c209b1ce1bd3b56f3b3b1bff21e066662f63efba..0c099613e6501dc2b3b6cd69a14bf35c2a1c791c 100644 (file)
@@ -93,4 +93,8 @@ a.ui-selectmenu {
 }
 .ui-selectmenu-menu-dropdown a {
    color: black;
+}
+
+img.attachedimage {
+   max-width: 600px;
 }
\ No newline at end of file