Commits

Josh VanderLinden committed 475ef81

Started working on a directive to insert attachments...

Comments (0)

Files changed (6)

articles/__init__.py

 except:
     # 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/models.py

 from django.utils.translation import ugettext_lazy as _
 from datetime import datetime
 from base64 import encodestring
+import mimetypes
 import re
 import urllib
 
     attachment = models.FileField(upload_to=upload_to)
     caption = models.CharField(max_length=255, blank=True)
 
+    @property
+    def filename(self):
+        return self.attachment.name.split('/')[-1]
+
+    @property
+    def content_type_class(self):
+        mt = mimetypes.guess_type(self.attachment.path)[0]
+        if mt:
+            content_type = mt.replace('/', '_')
+        else:
+            content_type = 'text_plain'
+
+        return content_type
+

articles/templates/articles/_article_content.html

     {{ article.rendered_content|safe }}
 </div>
 
+{% for att in article.attachments.all %}
+{% if forloop.first %}<div id="article-attachments">
+    <h3>Attachments</h3>
+    <ul>{% endif %}
+        <li 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>
+    {% if forloop.last %}</ul>
+</div>{% endif %}
+{% endfor %}
+

articles/templates/articles/_comments.html

 {% if disqus_forum %}
 <div id="disqus_thread"></div>
-<script type="text/javascript">
+<script type="application/javascript">
 var disqus_identifier = {{ article.id }};
 </script>
-<script type="text/javascript" src="http://disqus.com/forums/{{ disqus_forum }}/embed.js"></script>
+<script type="application/javascript" src="http://disqus.com/forums/{{ disqus_forum }}/embed.js"></script>
 <noscript><a href="http://disqus.com/forums/{{ disqus_forum }}/?url=ref">View the discussion thread.</a></noscript>
 <a href="http://disqus.com" class="dsq-brlink">Comments powered by <span class="logo-disqus">Disqus</span></a>
 {% endif %}

articles/templates/articles/_meta.html

     {% if article.use_addthis_button and article.addthis_username %}
     <!-- AddThis Button BEGIN -->
     <div>
-        <script type="text/javascript">var addthis_pub="{{ article.addthis_username }}";</script>
+        <script type="application/javascript">var addthis_pub="{{ article.addthis_username }}";</script>
         <a href="http://www.addthis.com/bookmark.php?v=20" onmouseover="return addthis_open(this, '', '[URL]', '[TITLE]')" onmouseout="addthis_close()" onclick="return addthis_sendto()"><img src="http://s7.addthis.com/static/btn/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a>
-        <script type="text/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>
+        <script type="application/javascript" src="http://s7.addthis.com/js/200/addthis_widget.js"></script>
     </div>
     <!-- AddThis Button END -->
     {% endif %}
 
-    <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
+    <script type="application/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script>
 
     <h4>{% trans 'Tags' %}</h4>
     <p>{% if article.tags.count %}{% for tag in article.tags.all %}<a href="{{ tag.get_absolute_url }}">{{ tag.name }}</a> {% endfor %}{% else %}None{% endif %}</p>

articles/templates/articles/base.html

 {% block footer %}
 {{ block.super }}
 
-<script type="text/javascript">
+<script type="application/javascript">
 //<![CDATA[
 (function() {
     var links = document.getElementsByTagName('a');
         query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
     }
     }
-    document.write('<script charset="utf-8" type="text/javascript" src="http://disqus.com/forums/{{ disqus_forum }}/get_num_replies.js' + query + '"></' + 'script>');
+    document.write('<script charset="utf-8" type="application/javascript" src="http://disqus.com/forums/{{ disqus_forum }}/get_num_replies.js' + query + '"></' + 'script>');
 })();
 //]]>
 </script>