Skip to content

Commit

Permalink
Merge pull request #325 from brunoamaral/308-add-option-to-subscribe-…
Browse files Browse the repository at this point in the history
…to-authors-new-content

adds an rss feed to every author, available at /feed/articles/author/<author_id>/
  • Loading branch information
brunoamaral authored Apr 15, 2023
2 parents 9e58646 + ac8778b commit 0adadcb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
1 change: 1 addition & 0 deletions django/admin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:author_id>/', ArticlesByAuthorFeed(), name='articles_by_author_feed'),
path('feed/articles/category/<str:category>/', ArticlesByCategoryFeed()),
path('feed/articles/subject/<str:subject>/', ArticlesBySubjectFeed()),
path('feed/articles/open/',OpenAccessFeed()),
Expand Down
49 changes: 47 additions & 2 deletions django/rss/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down Expand Up @@ -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
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/<author_id>/`"

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

0 comments on commit 0adadcb

Please sign in to comment.