From 0fe2e7a3561d7463b9cb496e54e43310ab1022a1 Mon Sep 17 00:00:00 2001 From: Chad Nelson Date: Wed, 20 Jan 2021 17:15:15 -0500 Subject: [PATCH 1/4] First pass at basic RSS --- bookwyrm/templates/snippets/rss_content.html | 1 + bookwyrm/templates/snippets/rss_title.html | 15 +++++++ bookwyrm/tests/views/test_rss_feed.py | 44 ++++++++++++++++++++ bookwyrm/urls.py | 3 ++ bookwyrm/views/rss_feed.py | 29 +++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 bookwyrm/templates/snippets/rss_content.html create mode 100644 bookwyrm/templates/snippets/rss_title.html create mode 100644 bookwyrm/tests/views/test_rss_feed.py create mode 100644 bookwyrm/views/rss_feed.py diff --git a/bookwyrm/templates/snippets/rss_content.html b/bookwyrm/templates/snippets/rss_content.html new file mode 100644 index 000000000..a72752d0d --- /dev/null +++ b/bookwyrm/templates/snippets/rss_content.html @@ -0,0 +1 @@ +{{ obj.pure_content | safe }} diff --git a/bookwyrm/templates/snippets/rss_title.html b/bookwyrm/templates/snippets/rss_title.html new file mode 100644 index 000000000..91b6d7e60 --- /dev/null +++ b/bookwyrm/templates/snippets/rss_title.html @@ -0,0 +1,15 @@ +{{ obj.user.username }}{% if obj.status_type == 'GeneratedNote' %} + {{ obj.content | safe }} +{% elif obj.status_type == 'Review' and not obj.name and not obj.content%} + rated +{% elif obj.status_type == 'Review' %} + reviewed +{% elif obj.status_type == 'Comment' %} + commented on +{% elif obj.status_type == 'Quotation' %} + quoted +{% endif %} +{% if obj.book %}{{ obj.book.title | safe}} +{% elif obj.mention_books %} +{{ obj.mention_books.first.title }} +{% endif %} \ No newline at end of file diff --git a/bookwyrm/tests/views/test_rss_feed.py b/bookwyrm/tests/views/test_rss_feed.py new file mode 100644 index 000000000..2e71e96e9 --- /dev/null +++ b/bookwyrm/tests/views/test_rss_feed.py @@ -0,0 +1,44 @@ +''' testing import ''' + +from unittest.mock import patch + +from django.test import RequestFactory, TestCase +import responses + +from bookwyrm import models +from bookwyrm.views import rss_feed +from bookwyrm.settings import DOMAIN + +class RssFeed(TestCase): + ''' rss feed behaves as expected ''' + def setUp(self): + self.user = models.User.objects.create_user( + 'rss_user', 'rss@test.rss', 'password', local=True) + + work = models.Work.objects.create(title='Test Work') + self.book = models.Edition.objects.create( + title='Example Edition', + remote_id='https://example.com/book/1', + parent_work=work + ) + + self.review = models.Review.objects.create( + name='Review name', content='test content', rating=3, + user=self.user, book=self.book) + + self.quote = models.Quotation.objects.create( + quote='a sickening sense', content='test content', + user=self.user, book=self.book) + + self.generatednote = models.GeneratedNote.objects.create( + content='test content', user=self.user) + + self.factory = RequestFactory() + + + def test_rss_feed(self): + request = self.factory.get('/user/rss_user/rss') + response = RssFeed(request) + self.assertEqual(response.status_code, 200) + self.assertEqual(False, True) + diff --git a/bookwyrm/urls.py b/bookwyrm/urls.py index ab134b1c5..ea1d62527 100644 --- a/bookwyrm/urls.py +++ b/bookwyrm/urls.py @@ -3,7 +3,9 @@ from django.conf.urls.static import static from django.contrib import admin from django.urls import path, re_path + from bookwyrm import incoming, settings, views, wellknown +from bookwyrm.views.rss_feed import RssFeed from bookwyrm.utils import regex user_path = r'^user/(?P%s)' % regex.username @@ -75,6 +77,7 @@ urlpatterns = [ re_path(r'%s/followers(.json)?/?$' % user_path, views.Followers.as_view()), re_path(r'%s/following(.json)?/?$' % user_path, views.Following.as_view()), re_path(r'^edit-profile/?$', views.EditUser.as_view()), + re_path(r'%s/rss' % user_path, views.rss_feed.RssFeed()), # reading goals re_path(r'%s/goal/(?P\d{4})/?$' % user_path, views.Goal.as_view()), diff --git a/bookwyrm/views/rss_feed.py b/bookwyrm/views/rss_feed.py new file mode 100644 index 000000000..d8de8fac7 --- /dev/null +++ b/bookwyrm/views/rss_feed.py @@ -0,0 +1,29 @@ +''' ''' + +from django.contrib.syndication.views import Feed +from django.urls import reverse +from bookwyrm.models.user import User +from .helpers import get_activity_feed, get_user_from_username + +class RssFeed(Feed): + + description_template = "snippets/rss_content.html" + title_template = "snippets/rss_title.html" + + def get_object(self, request, username): + return get_user_from_username(username) + + def link(self, obj): + return obj.local_path + + def title(self, obj): + return f"Status updates from {obj.username}" + + + def items(self, obj): + return get_activity_feed(obj, ['public', 'unlisted', 'followers']) + + + def item_link(self, item): + return item.local_path + From ad2baf3be514f9e9daac3a0849086327cd15a88e Mon Sep 17 00:00:00 2001 From: Chad Nelson Date: Sat, 23 Jan 2021 18:39:55 -0500 Subject: [PATCH 2/4] Use display_name instead of username --- bookwyrm/templates/snippets/rss_title.html | 2 +- bookwyrm/views/rss_feed.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bookwyrm/templates/snippets/rss_title.html b/bookwyrm/templates/snippets/rss_title.html index 91b6d7e60..dadb59612 100644 --- a/bookwyrm/templates/snippets/rss_title.html +++ b/bookwyrm/templates/snippets/rss_title.html @@ -1,4 +1,4 @@ -{{ obj.user.username }}{% if obj.status_type == 'GeneratedNote' %} +{{ obj.user.display_name }}{% if obj.status_type == 'GeneratedNote' %} {{ obj.content | safe }} {% elif obj.status_type == 'Review' and not obj.name and not obj.content%} rated diff --git a/bookwyrm/views/rss_feed.py b/bookwyrm/views/rss_feed.py index d8de8fac7..ee8063a6d 100644 --- a/bookwyrm/views/rss_feed.py +++ b/bookwyrm/views/rss_feed.py @@ -17,11 +17,11 @@ class RssFeed(Feed): return obj.local_path def title(self, obj): - return f"Status updates from {obj.username}" + return f"Status updates from {obj.display_name}" def items(self, obj): - return get_activity_feed(obj, ['public', 'unlisted', 'followers']) + return get_activity_feed(obj, ['public', 'unlisted']) def item_link(self, item): From 9c2813545c2efe5304aab7a910e9be905017342e Mon Sep 17 00:00:00 2001 From: Chad Nelson Date: Wed, 27 Jan 2021 21:23:20 -0500 Subject: [PATCH 3/4] Closer to some passing tests --- bookwyrm/tests/views/test_rss_feed.py | 7 ++++--- bookwyrm/views/rss_feed.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/bookwyrm/tests/views/test_rss_feed.py b/bookwyrm/tests/views/test_rss_feed.py index 2e71e96e9..bf0fb9a38 100644 --- a/bookwyrm/tests/views/test_rss_feed.py +++ b/bookwyrm/tests/views/test_rss_feed.py @@ -9,7 +9,7 @@ from bookwyrm import models from bookwyrm.views import rss_feed from bookwyrm.settings import DOMAIN -class RssFeed(TestCase): +class RssFeedView(TestCase): ''' rss feed behaves as expected ''' def setUp(self): self.user = models.User.objects.create_user( @@ -37,8 +37,9 @@ class RssFeed(TestCase): def test_rss_feed(self): + view = rss_feed.RssFeed() request = self.factory.get('/user/rss_user/rss') - response = RssFeed(request) - self.assertEqual(response.status_code, 200) + result = view(request, username=self.user.username) + self.assertEqual(result.status_code, 200) self.assertEqual(False, True) diff --git a/bookwyrm/views/rss_feed.py b/bookwyrm/views/rss_feed.py index ee8063a6d..d6bcd174d 100644 --- a/bookwyrm/views/rss_feed.py +++ b/bookwyrm/views/rss_feed.py @@ -21,7 +21,7 @@ class RssFeed(Feed): def items(self, obj): - return get_activity_feed(obj, ['public', 'unlisted']) + return get_activity_feed(obj, ['public', 'unlisted'], queryset=obj.status_set) def item_link(self, item): From 4274bf0508dcb6cba6464ed6ace9bc3459797d96 Mon Sep 17 00:00:00 2001 From: Chad Nelson Date: Wed, 27 Jan 2021 22:13:35 -0500 Subject: [PATCH 4/4] Patch site settings so this can pass --- bookwyrm/tests/views/test_rss_feed.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bookwyrm/tests/views/test_rss_feed.py b/bookwyrm/tests/views/test_rss_feed.py index bf0fb9a38..cbc97af7f 100644 --- a/bookwyrm/tests/views/test_rss_feed.py +++ b/bookwyrm/tests/views/test_rss_feed.py @@ -1,7 +1,6 @@ ''' testing import ''' from unittest.mock import patch - from django.test import RequestFactory, TestCase import responses @@ -12,6 +11,9 @@ from bookwyrm.settings import DOMAIN class RssFeedView(TestCase): ''' rss feed behaves as expected ''' def setUp(self): + + self.site = models.SiteSettings.objects.create() + self.user = models.User.objects.create_user( 'rss_user', 'rss@test.rss', 'password', local=True) @@ -39,7 +41,12 @@ class RssFeedView(TestCase): def test_rss_feed(self): view = rss_feed.RssFeed() request = self.factory.get('/user/rss_user/rss') - result = view(request, username=self.user.username) + with patch("bookwyrm.models.SiteSettings.objects.get") as site: + site.return_value = self.site + result = view(request, username=self.user.username) self.assertEqual(result.status_code, 200) - self.assertEqual(False, True) + + self.assertIn(b"Status updates from rss_user", result.content) + self.assertIn( b"a sickening sense", result.content) + self.assertIn(b"Example Edition", result.content)