From d7daf3ae5d8885ee85abcfc60f36ec81c8cb8280 Mon Sep 17 00:00:00 2001 From: Daniele Guido Date: Mon, 20 Feb 2023 14:41:07 +0100 Subject: [PATCH 1/2] Update urls.py --- jdhapi/urls.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/jdhapi/urls.py b/jdhapi/urls.py index 955bd89..9bce9ed 100644 --- a/jdhapi/urls.py +++ b/jdhapi/urls.py @@ -1,5 +1,6 @@ from django.urls import path, include from jdhapi import views +from jdhapi import feeds from rest_framework.urlpatterns import format_suffix_patterns from rest_framework.routers import DefaultRouter from rest_framework import routers @@ -25,5 +26,8 @@ path('api/tags//', views.TagDetail.as_view(), name='tag-detail'), path('api/callofpaper/', views.CallOfPaperList.as_view(), name='callofpaper-list'), path('api/callofpaper//', views.CallOfPaperDetail.as_view(), name='callofpaper-detail'), + # feeds + path('api/feeds/articles/', feeds.LatestArticleFeed()), + ] urlpatterns = format_suffix_patterns(urlpatterns) From 4a988324ea0ba9d50b5b6ba8d24027b5a0a362e4 Mon Sep 17 00:00:00 2001 From: Daniele Guido Date: Mon, 20 Feb 2023 14:41:15 +0100 Subject: [PATCH 2/2] Create feeds.py --- jdhapi/feeds.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 jdhapi/feeds.py diff --git a/jdhapi/feeds.py b/jdhapi/feeds.py new file mode 100644 index 0000000..7bbfe48 --- /dev/null +++ b/jdhapi/feeds.py @@ -0,0 +1,39 @@ +from django.contrib.syndication.views import Feed + +from .models import Article + + +class LatestArticleFeed(Feed): + title = "Journal of Digital History" + link = "https://journalofdigitalhistory.org" + description = "International, academic, peer-reviewed and diamond open-access Journal of digital History" + + def items(self): + # latest 50 PUBLISHED articles + return Article.objects.filter(status=Article.Status.PUBLISHED).order_by( + "-publication_date" + )[:50] + + def item_title(self, item): + title_parts = item.data.get("title", []) + # if is string, return it + if isinstance(title_parts, str): + return title_parts + # if is array, join all elements + elif isinstance(title_parts, list): + return " ".join(title_parts) + return "" + + def item_description(self, item): + abstract_parts = item.data.get("abstract", []) + # if is string, return it + if isinstance(abstract_parts, str): + return abstract_parts + # if is array, join all elements + elif isinstance(abstract_parts, list): + return " ".join(abstract_parts) + return "" + + # item_link is only needed if NewsItem has no get_absolute_url method. + def item_link(self, item): + return f"{self.link}/en/article/{item.abstract.pid}"