add atom feeds (with legacy compatibility)
authorFrédéric Péters <fpeters@0d.be>
Sun, 29 Dec 2019 19:55:50 +0000 (20:55 +0100)
committerFrédéric Péters <fpeters@0d.be>
Mon, 30 Dec 2019 08:35:50 +0000 (09:35 +0100)
chloro/phyll/management/commands/load-old-posts.py
chloro/phyll/templates/phyll/base.html
chloro/phyll/urls.py
chloro/phyll/views.py
chloro/settings.py

index 0e2795c..61dd893 100644 (file)
@@ -80,3 +80,4 @@ class Command(BaseCommand):
                 note.tags.add(tag)
             if getattr(old_post, 'language', None):
                 note.tags.add('lang-%s' % old_post.language)
+            note.tags.add('old-post-id-%s' % old_post.id)
index d46f147..de553ab 100644 (file)
@@ -5,6 +5,7 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Coin web de Frédéric Péters</title>
     <link rel="stylesheet" type="text/css" href="/static/css/style.css">
+    <link rel="alternate" type="application/atom+xml" title="Atom feed" href="/feed/atom" />
     {% block bottom-head %}
     {% endblock %}
   </head>
index 13c70ba..b3171df 100644 (file)
@@ -39,6 +39,8 @@ urlpatterns = [
         staff_member_required(views.NoteDeleteView.as_view(), login_url='login'),
     ),
     url(r'^new-note/$', staff_member_required(views.NoteAddView.as_view(), login_url='login')),
+    url(r'^feeds/(?P<sub>[\w:-]+)/atom$', views.AtomFeed()),
+    url(r'^feed/atom$', views.AtomFeed()),
     url(r'^tag/(?P<tag>[\w:-]+)/$', views.ListOnTagView.as_view()),
     url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(?P<slug>[\w:-]+)/$', views.NoteView.as_view()),
     url(r'^(?P<slug>[\w:-]+)/$', views.NoteView.as_view()),
index b8598dc..5264298 100644 (file)
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import urllib.parse
+
+from django.conf import settings
+from django.contrib.syndication.views import Feed
 from django.core.exceptions import PermissionDenied
 from django.http import Http404
+from django.utils.feedgenerator import Atom1Feed
 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
 
 from .models import Note
@@ -69,3 +74,48 @@ class ListOnTagView(ListView):
         if not self.request.user.is_staff:
             qs = qs.filter(published=True)
         return qs
+
+
+class Atom1FeedWithBaseXml(Atom1Feed):
+    def root_attributes(self):
+        root_attributes = super(Atom1FeedWithBaseXml, self).root_attributes()
+        scheme, netloc, path, params, query, fragment = urllib.parse.urlparse(self.feed['feed_url'])
+        root_attributes['xml:base'] = urllib.parse.urlunparse((scheme, netloc, '/', params, query, fragment))
+        return root_attributes
+
+
+class AtomFeed(Feed):
+    title = settings.SITE_TITLE
+    link = '/'
+    feed_type = Atom1FeedWithBaseXml
+    author_name = settings.SITE_AUTHOR
+
+    def get_object(self, request, *args, **kwargs):
+        self.sub = kwargs.get('sub', 'default')
+        return super(AtomFeed, self).get_object(request, *args, **kwargs)
+
+    def items(self):
+        qs = Note.objects.filter(published=True)
+        if self.sub == 'default':
+            pass
+        elif self.sub == 'gnome-en':
+            qs = qs.filter(tags__name__in=['gnome']).filter(tags__name__in=['lang-en'])
+        else:
+            qs = qs.filter(tags__name__in=[self.sub])
+        return qs.select_related()[:20]
+
+    def item_description(self, item):
+        return item.text
+
+    def item_guid(self, item):
+        legacy_id = None
+        for tag in item.tags.all():
+            if tag.name.startswith('old-post-id-'):
+                return 'http://www.0d.be/posts/%s' % tag.name.split('-')[-1]
+        return 'https://www.0d.be' + item.get_absolute_url()
+
+    def item_title(self, item):
+        return item.title
+
+    def item_pubdate(self, item):
+        return item.creation_timestamp
index dffc6df..13ee8a1 100644 (file)
@@ -144,6 +144,9 @@ CKEDITOR_CONFIGS = {
     },
 }
 
+SITE_AUTHOR = "Frédéric Péters"
+SITE_TITLE = "Coin web de Frédéric Péters"
+
 local_settings_file = os.environ.get(
     'CHLORO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')
 )