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}" 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)