From ac8778b65dbb79bafd39071091465c340e4d9713 Mon Sep 17 00:00:00 2001 From: Bruno Amaral Date: Sat, 15 Apr 2023 21:12:26 +0100 Subject: [PATCH] adds an rss feed to every author --- django/admin/urls.py | 1 + django/rss/views.py | 49 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/django/admin/urls.py b/django/admin/urls.py index 2b2cdd0c..19839486 100644 --- a/django/admin/urls.py +++ b/django/admin/urls.py @@ -40,6 +40,7 @@ # path('feed/articles/prediction/none/', ToPredictFeed()), # path('trials/all/', AllTrialViewSet.as_view()), # path('articles/related/', RelatedArticles.as_view({'get': 'list'})), + path('feed/articles/author//', ArticlesByAuthorFeed(), name='articles_by_author_feed'), path('feed/articles/category//', ArticlesByCategoryFeed()), path('feed/articles/subject//', ArticlesBySubjectFeed()), path('feed/articles/open/',OpenAccessFeed()), diff --git a/django/rss/views.py b/django/rss/views.py index ccdcd15f..3994bb9c 100644 --- a/django/rss/views.py +++ b/django/rss/views.py @@ -3,7 +3,8 @@ # Create your views here. from django.contrib.syndication.views import Feed from django.conf import settings -from gregory.models import Articles, Trials, Sources, Categories +from gregory.models import Articles, Authors, Trials, Sources, Categories +from django.urls import reverse @@ -251,4 +252,48 @@ def item_pubdate(self, item): # # item_link is only needed if NewsItem has no get_absolute_url method. def item_link(self, item): - return item.link \ No newline at end of file + return item.link + + +class ArticlesByAuthorFeed(Feed): + title = "Articles by Author" + link = "/feed/articles/" + description = "RSS feed for articles by a specific author. Use `/feed/articles/author//`" + + def __init__(self, **kwargs): + self.author_id = kwargs.get('author_id') + super().__init__(**kwargs) + + def get(self, request, *args, **kwargs): + self.author_id = kwargs.get('author_id') + return super().get(request, *args, **kwargs) + + def get_object(self, request, author_id, *args, **kwargs): + return Authors.objects.get(pk=author_id) + + def items(self, obj): + return Articles.objects.filter(authors=obj) + + def item_title(self, item): + return item.title + + def item_description(self, item): + return item.summary + + def item_link(self, item): + return item.link + + def item_guid(self, item): + return item.link + + def item_pubdate(self, item): + return item.published_date + + def item_author_name(self, item): + return ', '.join([author.full_name for author in item.authors.all()]) + + def item_categories(self, item): + return [category.category_name for category in item.categories.all()] + + def link(self, obj): + return reverse('articles_by_author_feed', kwargs={'author_id': obj.pk}) \ No newline at end of file