From 29ee027f6dbebcf44230410edab8377f1e60e020 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 31 Dec 2020 08:05:24 -0800 Subject: [PATCH 1/3] Tests tag page and orders tag collection json --- bookwyrm/models/book.py | 2 +- bookwyrm/models/tag.py | 4 +++- bookwyrm/tests/test_views.py | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index fb20ee25b..081895109 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -129,7 +129,7 @@ class Work(OrderedCollectionPageMixin, Book): def to_edition_list(self, **kwargs): ''' an ordered collection of editions ''' return self.to_ordered_collection( - self.editions.all(), + self.editions.order_by('-updated_date').all(), remote_id='%s/editions' % self.remote_id, **kwargs ) diff --git a/bookwyrm/models/tag.py b/bookwyrm/models/tag.py index 940b41924..cb12d382f 100644 --- a/bookwyrm/models/tag.py +++ b/bookwyrm/models/tag.py @@ -17,7 +17,9 @@ class Tag(OrderedCollectionMixin, BookWyrmModel): @classmethod def book_queryset(cls, identifier): ''' county of books associated with this tag ''' - return cls.objects.filter(identifier=identifier) + return cls.objects.filter( + identifier=identifier + ).order_by('-updated_date') @property def collection_queryset(self): diff --git a/bookwyrm/tests/test_views.py b/bookwyrm/tests/test_views.py index a1ff52a08..fa8c99e48 100644 --- a/bookwyrm/tests/test_views.py +++ b/bookwyrm/tests/test_views.py @@ -196,3 +196,43 @@ class Views(TestCase): result = views.editions_page(request, self.work.id) self.assertIsInstance(result, JsonResponse) self.assertEqual(result.status_code, 200) + + + def test_author_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + author = models.Author.objects.create(name='Jessica') + request = self.factory.get('') + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = False + result = views.author_page(request, author.id) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'author.html') + self.assertEqual(result.status_code, 200) + + request = self.factory.get('') + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.author_page(request, author.id) + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) + + + def test_tag_page(self): + ''' there are so many views, this just makes sure it LOADS ''' + tag = models.Tag.objects.create(name='hi there') + models.UserTag.objects.create( + tag=tag, user=self.local_user, book=self.book) + request = self.factory.get('') + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = False + result = views.tag_page(request, tag.identifier) + self.assertIsInstance(result, TemplateResponse) + self.assertEqual(result.template_name, 'tag.html') + self.assertEqual(result.status_code, 200) + + request = self.factory.get('') + with patch('bookwyrm.views.is_api_request') as is_api: + is_api.return_value = True + result = views.tag_page(request, tag.identifier) + self.assertIsInstance(result, JsonResponse) + self.assertEqual(result.status_code, 200) From a7a586ea90083bf999e53be0e58c3ae9d10828f7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 31 Dec 2020 08:30:51 -0800 Subject: [PATCH 2/3] Fixes tagging --- bookwyrm/outgoing.py | 5 ----- bookwyrm/tests/test_view_actions.py | 23 +++++++++++++++++++++++ bookwyrm/view_actions.py | 4 ++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index aee463c90..38dff400c 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -283,11 +283,6 @@ def to_markdown(content): return sanitizer.get_output() -def handle_tag(user, tag): - ''' tag a book ''' - broadcast(user, tag.to_add_activity(user)) - - def handle_untag(user, book, name): ''' tag a book ''' book = models.Book.objects.get(id=book) diff --git a/bookwyrm/tests/test_view_actions.py b/bookwyrm/tests/test_view_actions.py index c846c2481..98a57985f 100644 --- a/bookwyrm/tests/test_view_actions.py +++ b/bookwyrm/tests/test_view_actions.py @@ -41,6 +41,9 @@ class ViewActions(TestCase): content='Test status', remote_id='https://example.com/status/1', ) + self.work = models.Work.objects.create(title='Test Work') + self.book = models.Edition.objects.create( + title='Test Book', parent_work=self.work) self.settings = models.SiteSettings.objects.create(id=1) self.factory = RequestFactory() @@ -351,3 +354,23 @@ class ViewActions(TestCase): author.refresh_from_db() self.assertEqual(author.name, 'Test Author') self.assertEqual(resp.template_name, 'edit_author.html') + + + def test_tag(self): + ''' add a tag to a book ''' + request = self.factory.post( + '', { + 'name': 'A Tag!?', + 'book': self.book.id, + }) + request.user = self.local_user + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + actions.tag(request) + + tag = models.Tag.objects.get() + user_tag = models.UserTag.objects.get() + self.assertEqual(tag.name, 'A Tag!?') + self.assertEqual(tag.identifier, 'A+Tag%21%3F') + self.assertEqual(user_tag.user, self.local_user) + self.assertEqual(user_tag.book, self.book) diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index 78f762185..ca632ef94 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -577,14 +577,14 @@ def tag(request): tag_obj, created = models.Tag.objects.get_or_create( name=name, ) - user_tag = models.UserTag.objects.get_or_create( + user_tag, _ = models.UserTag.objects.get_or_create( user=request.user, book=book, tag=tag_obj, ) if created: - outgoing.handle_tag(request.user, user_tag) + broadcast(request.user, user_tag.to_add_activity(request.user)) return redirect('/book/%s' % book_id) From 69af2f4cd3f92408cff3a0fe82c60b03dff66fa7 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 31 Dec 2020 08:42:09 -0800 Subject: [PATCH 3/3] Fixes untag --- bookwyrm/models/tag.py | 2 +- bookwyrm/outgoing.py | 10 ---------- bookwyrm/tests/test_view_actions.py | 20 ++++++++++++++++++++ bookwyrm/view_actions.py | 9 ++++++++- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/bookwyrm/models/tag.py b/bookwyrm/models/tag.py index cb12d382f..6e0ba8ab5 100644 --- a/bookwyrm/models/tag.py +++ b/bookwyrm/models/tag.py @@ -66,7 +66,7 @@ class UserTag(BookWyrmModel): id='%s#remove' % self.remote_id, actor=user.remote_id, object=self.book.to_activity(), - target=self.to_activity(), + target=self.remote_id, ).serialize() diff --git a/bookwyrm/outgoing.py b/bookwyrm/outgoing.py index 38dff400c..a01d8cfed 100644 --- a/bookwyrm/outgoing.py +++ b/bookwyrm/outgoing.py @@ -283,16 +283,6 @@ def to_markdown(content): return sanitizer.get_output() -def handle_untag(user, book, name): - ''' tag a book ''' - book = models.Book.objects.get(id=book) - tag = models.Tag.objects.get(name=name, book=book, user=user) - tag_activity = tag.to_remove_activity(user) - tag.delete() - - broadcast(user, tag_activity) - - def handle_favorite(user, status): ''' a user likes a status ''' try: diff --git a/bookwyrm/tests/test_view_actions.py b/bookwyrm/tests/test_view_actions.py index 98a57985f..b5ed6751b 100644 --- a/bookwyrm/tests/test_view_actions.py +++ b/bookwyrm/tests/test_view_actions.py @@ -374,3 +374,23 @@ class ViewActions(TestCase): self.assertEqual(tag.identifier, 'A+Tag%21%3F') self.assertEqual(user_tag.user, self.local_user) self.assertEqual(user_tag.book, self.book) + + + def test_untag(self): + ''' remove a tag from a book ''' + tag = models.Tag.objects.create(name='A Tag!?') + user_tag = models.UserTag.objects.create( + user=self.local_user, book=self.book, tag=tag) + request = self.factory.post( + '', { + 'user': self.local_user.id, + 'book': self.book.id, + 'name': tag.name, + }) + request.user = self.local_user + + with patch('bookwyrm.broadcast.broadcast_task.delay'): + actions.untag(request) + + self.assertTrue(models.Tag.objects.filter(name='A Tag!?').exists()) + self.assertFalse(models.UserTag.objects.exists()) diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index ca632ef94..a8f85c022 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -593,9 +593,16 @@ def tag(request): def untag(request): ''' untag a book ''' name = request.POST.get('name') + tag = get_object_or_404(models.Tag, name=name) book_id = request.POST.get('book') + book = get_object_or_404(models.Edition, id=book_id) - outgoing.handle_untag(request.user, book_id, name) + tag = get_object_or_404( + models.UserTag, tag=tag, book=book, user=request.user) + tag_activity = tag.to_remove_activity(request.user) + tag.delete() + + broadcast(request.user, tag_activity) return redirect('/book/%s' % book_id)