Commits

jokull  committed 9bf5343

Added Atom feed

  • Participants
  • Parent commits 3e12a78

Comments (0)

Files changed (8)

File relaxlog/settings.py

     'django.contrib.sessions',
     'django.contrib.admin',
     'django.contrib.markup',
-    'weblog'
+    'django.contrib.syndication',
+    'weblog',
 )
 
 COUCHDB_DATABASE = 'solberg'
+ISO_DATE = '%Y-%m-%d'
+SITE_URI = 'http://www.solberg.is/'
 
 try:
     from settings_local import *

File relaxlog/templates/base.html

     <link rel="stylesheet" href="{{ MEDIA_URL }}static/friendly.css" type="text/css">
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
     <script src="{{ MEDIA_URL }}static/script.js"></script>
-    <link rel="alternate" title="Nýjustu færslur" href="/straumar/faerslur/" type="application/atom+xml">
-    <link rel="alternate" title="Nýjustu hlekkir" href="/straumar/hlekkir/" type="application/atom+xml">
+    <link rel="alternate" title="Nýjustu færslur" href="/atom/blogg/" type="application/atom+xml">
 </head>
 <body>
     <div id="container">

File relaxlog/urls.py

 from django.conf.urls.defaults import *
 from django.contrib import admin
 
+from weblog.feeds import LatestFeed
+
 admin.autodiscover()
 
 urlpatterns = patterns('',
     url(r'^accounts/', include('django.contrib.auth.urls')),
 )
 
+urlpatterns += patterns('',
+    url(r'^xml/(?P<url>.*)/$', 'django.contrib.syndication.views.feed', 
+        {'feed_dict': { 'blogg': LatestFeed }, }),
+)
+
 from django.conf import settings
 
 if settings.DEBUG:

File relaxlog/weblog/feeds.py

+# coding=UTF-8
+
+import datetime
+
+from couchdb.client import Server
+
+from django.conf import settings
+from django.contrib.syndication.feeds import Feed
+from django.utils.feedgenerator import Atom1Feed
+from django.core.urlresolvers import reverse
+
+class LatestFeed(Feed):
+    
+    feed_type = Atom1Feed
+    
+    description_template = 'weblog/feeds/article_description.html'
+    title_template = 'weblog/feeds/article_title.html'
+    
+    title = u'Jökull Sólberg'
+    link = ''
+    subtitle = u'Nýjustu færslur'
+    
+    def items(self):
+        server = Server()
+        db = server[settings.COUCHDB_DATABASE]
+        options = {'count': 15, 'descending': True, }
+        view_results = db.view('articles/by_date', **options)
+        return [r.value for r in view_results.rows]
+    
+    def item_pubdate(self, item):
+        return datetime.datetime.strptime(item['date'], settings.ISO_DATE)
+    
+    def item_title(self, item):
+        print item['title']
+        return item['title']
+    
+    def item_link(self, item):
+        return settings.SITE_URI.rstrip('/') + reverse('weblog.views.article', args=[item['slug']])

File relaxlog/weblog/templates/weblog/feeds/article_description.html

+{% load markup %}{{ obj.content|restructuredtext }}

File relaxlog/weblog/templates/weblog/feeds/article_title.html

+{{ obj.title }}

File relaxlog/weblog/urls.py

 from django.conf.urls.defaults import *
 
 urlpatterns = patterns('weblog.views',
-    (r'^$', 'latest'),
-    (r'^add/$', 'add'),
-    (r'^preview/$', 'preview'),
-    (r'^safn/$', 'archive'),
-    (r'^(?P<slug>[-\w]+)/$', 'article'),
-    (r'^(?P<slug>[-\w]+)/edit/$', 'edit'),
+    url(r'^$', 'latest'),
+    url(r'^add/$', 'add'),
+    url(r'^preview/$', 'preview'),
+    url(r'^safn/$', 'archive'),
+    url(r'^(?P<slug>[-\w]+)/$', 'article'),
+    url(r'^(?P<slug>[-\w]+)/edit/$', 'edit'),
 )

File relaxlog/weblog/views.py

 from weblog.forms import DocumentForm, DocumentAddForm
 
 COUNT = 3
-ISO_DATE = '%Y-%m-%d'
 
 def latest(request):
     
     latest_articles = []
     for document in view_results.rows[:COUNT]:
         article = dict(**document.value)
-        article['date'] = datetime.datetime.strptime(document.value['date'], ISO_DATE)
+        article['date'] = datetime.datetime.strptime(document.value['date'], settings.ISO_DATE)
         latest_articles.append(article)
 
     context = {}
     except client.ResourceNotFound:
         raise Http404
     article = dict(**main)
-    article['date'] = datetime.datetime.strptime(main['date'], ISO_DATE)
+    article['date'] = datetime.datetime.strptime(main['date'], settings.ISO_DATE)
     context = {'article': article, }
     
     view_results = db.view('articles/by_date', count=2, 
             _id = data['slug']
             db[_id] = {
                 'title': data['title'],
-                'date': data['date'].strftime(ISO_DATE),
+                'date': data['date'].strftime(settings.ISO_DATE),
                 'content': data['content'],
                 'type': 'Article',
                 'status': 'Online',
             data = form.cleaned_data
             article['title'] = data['title']
             article['content'] = data['content']
-            article['date'] = data['date'].strftime(ISO_DATE)
+            article['date'] = data['date'].strftime(settings.ISO_DATE)
             db[article.id] = article
             return HttpResponseRedirect(reverse('weblog.views.article', args=[slug]))
     else:
         article = dict(**article)
-        date = datetime.datetime.strptime(article['date'], ISO_DATE)
+        date = datetime.datetime.strptime(article['date'], settings.ISO_DATE)
         article['date'] = datetime.date(*date.timetuple()[:3]) # Convert from datetime to date
         form = DocumentForm(initial=article)