Josh VanderLinden avatar Josh VanderLinden committed a7412b8

Removed the RST directive for including attachments. Updated the management
command to be able to save email attachments. Updated the template to include
an ID for attachments.

Comments (0)

Files changed (4)

articles/__init__.py

     # the user probably doesn't have pygments installed
     pass
 
-from docutils import nodes
-from docutils.parsers.rst import directives, Directive
-
-class Attach(Directive):
-
-    required_arguments = 1
-    optional_arguments = 0
-    final_argument_whitespace = True
-    option_spec = {
-        'class': directives.unchanged,
-        'height': directives.nonnegative_int,
-        'width': directives.nonnegative_int,
-        'scale': directives.nonnegative_int,
-    }
-    has_content = False
-
-    def run(self):
-        self.options['uri'] = directives.uri(self.arguments[0])
-        attach_node = nodes.image(rawsource=self.block_text, **self.options)
-        return [attach_node]
-
-directives.register_directive('attach', Attach)
-

articles/management/commands/check_for_articles_from_email.py

+from base64 import b64decode
 from datetime import datetime
 from email.parser import FeedParser
 from email.utils import parseaddr, parsedate
 from django.core.management.base import BaseCommand
 from django.utils.translation import ugettext_lazy as _
 
-from articles.models import Article, MARKUP_HTML, MARKUP_MARKDOWN, MARKUP_REST, MARKUP_TEXTILE
+from articles.models import Article, Attachment, MARKUP_HTML, MARKUP_MARKDOWN, MARKUP_REST, MARKUP_TEXTILE
 
 MB_IMAP4 = 'IMAP4'
 MB_POP3 = 'POP3'
         if email.is_multipart():
             self.log('Extracting email contents from multipart message')
             for pl in email.get_payload():
-                if pl.get_content_type() in ('text/plain', 'text/html'):
+                if pl.get_content_type() in ('text/plain', 'text/html') and pl.get_filename() is None:
                     return pl.get_payload()
         else:
             return email.get_payload()
         created = []
         site = Site.objects.get_current()
 
-        # make sure we have a valid default markup
         ack = self.config.get('acknowledge', False)
         autopost = self.config.get('autopost', False)
+
+        # make sure we have a valid default markup
         markup = self.config.get('markup', MARKUP_HTML)
         if markup not in (MARKUP_HTML, MARKUP_MARKDOWN, MARKUP_REST, MARKUP_TEXTILE):
             markup = MARKUP_HTML
                 # try to grab the timestamp from the email message
                 publish_date = datetime.fromtimestamp(time.mktime(parsedate(email['Date'])))
             except StandardError, err:
-                self.log("An error occured when I tried to convert the email's timestamp into a datetime object: %s" % (err,))
+                self.log("An error occurred when I tried to convert the email's timestamp into a datetime object: %s" % (err,))
                 publish_date = datetime.now()
 
             # post the article
                 self.log('Error creating article: %s' % (err,), 0)
                 continue
             else:
+
+                # handle attachments
+                if email.is_multipart():
+                    files = [pl for pl in email.get_payload() if pl.get_filename() is not None]
+                    for att in files:
+                        obj = Attachment(
+                            article=article,
+                            caption=att.get_filename(),
+                        )
+                        obj.attachment.save(obj.caption, ChunkyString(att.get_payload()))
+                        obj.save()
+
                 created.append(num)
 
             if ack:
 
         return created
 
+class ChunkyString(str):
+    """Makes is possible to easily chunk attachments"""
+
+    def chunks(self):
+        i = 0
+        decoded = b64decode(self)
+        while True:
+            l = i
+            i += 1024
+            yield decoded[l:i]
+
+            if i > len(decoded):
+                raise StopIteration
+

articles/models.py

         if mt:
             content_type = mt.replace('/', '_')
         else:
+            # assume everything else is text/plain
             content_type = 'text_plain'
 
         return content_type

articles/templates/articles/_article_content.html

 {% if forloop.first %}<div id="article-attachments">
     <h3>Attachments</h3>
     <ul>{% endif %}
-        <li class="attachment ct_{{ att.content_type_class }}">
+        <li id="attachment-{{ forloop.counter }}" class="attachment ct_{{ att.content_type_class }}">
             <a href="{{ att.attachment.url }}" class="attachment-link">{{ att.filename }}</a>
             <span class="attachment-size">Size: {{ att.attachment.size|filesizeformat }}</span>
         </li>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.