Commits

David Chambers  committed 2cc221a

Add Atom feed to `./manage.py bake` output (closes #17).

  • Participants
  • Parent commits cd97d28

Comments (0)

Files changed (5)

-VERSION = '0.8.3'
+VERSION = '0.9dev'
 from django import http
 from django.core.cache import cache
+from django.core.urlresolvers import reverse
 from django.utils import feedgenerator
 
 import mango.settings
 from mango.main import Index
 
-def atom(request, tag=None):
-    content_type = 'application/atom+xml; charset=utf-8'
-    feed_url = request.build_absolute_uri()
+def atom_xml(request=None, tag=None):
+    if request is None:
+        feed_url = mango.settings.BASE_URL + reverse(atom)[1:]
+    else:
+        feed_url = request.build_absolute_uri()
     cache_key = 'mango:%s' % feed_url
     xml = cache.get(cache_key)
     if xml:
-        return http.HttpResponse(xml, content_type=content_type)
+        return xml
 
     def required_kwargs(required, post=None):
         try:
 
     xml = feed.writeString('utf-8')
     cache.set(cache_key, xml, mango.settings.INDEX_CACHE_SECONDS)
-    return http.HttpResponse(xml, content_type=content_type)
+    return xml
+
+def atom(request, tag=None):
+    content_type = 'application/atom+xml; charset=utf-8'
+    return http.HttpResponse(atom_xml(request, tag=tag),
+                             content_type=content_type)

File management/commands/bake.py

 
 from django.core.management.base import BaseCommand, CommandError
 from django.template.loader import render_to_string
+from django.utils.encoding import smart_unicode
 
+import mango.settings
+from mango.feeds import atom_xml
 from mango.main import Index
 
 class Command(BaseCommand):
         if not args:
             raise CommandError('You must specify a directory.')
 
+        # Feed URLs are different for static Mango sites. This hook
+        # enables the URL dispatcher to determine which URL applies.
+        mango.settings._baking = True
+
         path = os.path.expanduser(args[0])
         index = Index.get()
         documents = index.descendants(include_pages=True)
             f.write(u'%s\n' % document.source.rstrip())
             f.close()
 
+        # Atom feed
+        f = codecs.open(os.path.join(path, 'atom.xml'), 'w', 'utf-8')
+        f.write(smart_unicode(atom_xml()))
+        f.close()
+
         count = len(documents)
         self.stdout.write('Finished processing %s %s.\n'
                           % (count, count == 1 and 'document' or 'documents'))

File settings/__init__.py

 
 UNIX_DOCUMENTS_PATH = _unix_documents_path()
 
+_baking = False
+
 del dirname, os, smart_unicode  # tidy the namespace

File urls/defaults.py

 
 urlpatterns += patterns('',
     (r'^$', index),
-    (r'^feed/$', atom),
+    (r'^atom.xml$' if mango.settings._baking else r'^feed/$', atom),
     (r'^flushcache/$', flush_cache),
     (r'^moderate/(approve|delete|spam|close)$', moderate),
     (r'^(.+?)/redirect/(.+)?$', redirect),