From: Frédéric Péters Date: Sun, 29 Dec 2019 19:55:50 +0000 (+0100) Subject: add atom feeds (with legacy compatibility) X-Git-Tag: v2022~47 X-Git-Url: https://git.0d.be/?p=chloro.git;a=commitdiff_plain;h=108015252575972ad3c70749484e3c74b5e6b2dd add atom feeds (with legacy compatibility) --- diff --git a/chloro/phyll/management/commands/load-old-posts.py b/chloro/phyll/management/commands/load-old-posts.py index 0e2795c..61dd893 100644 --- a/chloro/phyll/management/commands/load-old-posts.py +++ b/chloro/phyll/management/commands/load-old-posts.py @@ -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) diff --git a/chloro/phyll/templates/phyll/base.html b/chloro/phyll/templates/phyll/base.html index d46f147..de553ab 100644 --- a/chloro/phyll/templates/phyll/base.html +++ b/chloro/phyll/templates/phyll/base.html @@ -5,6 +5,7 @@ Coin web de Frédéric Péters + {% block bottom-head %} {% endblock %} diff --git a/chloro/phyll/urls.py b/chloro/phyll/urls.py index 13c70ba..b3171df 100644 --- a/chloro/phyll/urls.py +++ b/chloro/phyll/urls.py @@ -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[\w:-]+)/atom$', views.AtomFeed()), + url(r'^feed/atom$', views.AtomFeed()), url(r'^tag/(?P[\w:-]+)/$', views.ListOnTagView.as_view()), url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[\w:-]+)/$', views.NoteView.as_view()), url(r'^(?P[\w:-]+)/$', views.NoteView.as_view()), diff --git a/chloro/phyll/views.py b/chloro/phyll/views.py index b8598dc..5264298 100644 --- a/chloro/phyll/views.py +++ b/chloro/phyll/views.py @@ -14,8 +14,13 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +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 diff --git a/chloro/settings.py b/chloro/settings.py index dffc6df..13ee8a1 100644 --- a/chloro/settings.py +++ b/chloro/settings.py @@ -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') )