From 8a1800ea261a314e4d2781433fb203f8fa1be8f2 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Wed, 16 Dec 2020 13:59:18 -0800 Subject: [PATCH] Federate shelve activities when switching editions --- bookwyrm/tests/test_view_actions.py | 25 +++++++++++++++++++++++++ bookwyrm/view_actions.py | 7 ++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/bookwyrm/tests/test_view_actions.py b/bookwyrm/tests/test_view_actions.py index bb0fcdb24..77584c90b 100644 --- a/bookwyrm/tests/test_view_actions.py +++ b/bookwyrm/tests/test_view_actions.py @@ -237,3 +237,28 @@ class ViewActions(TestCase): resp = actions.password_reset(request) self.assertEqual(resp.template_name, 'password_reset.html') self.assertTrue(models.PasswordReset.objects.exists()) + + def test_switch_edition(self): + ''' updates user's relationships to a book ''' + work = models.Work.objects.create(title='test work') + edition1 = models.Edition.objects.create( + title='first ed', parent_work=work) + edition2 = models.Edition.objects.create( + title='second ed', parent_work=work) + shelf = models.Shelf.objects.create( + name='Test Shelf', user=self.local_user) + shelf.books.add(edition1) + models.ReadThrough.objects.create( + user=self.local_user, book=edition1) + + self.assertEqual(models.ShelfBook.objects.get().book, edition1) + self.assertEqual(models.ReadThrough.objects.get().book, edition1) + request = self.factory.post('', { + 'edition': edition2.id + }) + request.user = self.local_user + with patch('bookwyrm.broadcast.broadcast_task.delay'): + actions.switch_edition(request) + + self.assertEqual(models.ShelfBook.objects.get().book, edition2) + self.assertEqual(models.ReadThrough.objects.get().book, edition2) diff --git a/bookwyrm/view_actions.py b/bookwyrm/view_actions.py index 47ac36de9..fcb684764 100644 --- a/bookwyrm/view_actions.py +++ b/bookwyrm/view_actions.py @@ -18,6 +18,7 @@ from django.utils import timezone from django.views.decorators.http import require_GET, require_POST from bookwyrm import books_manager +from bookwyrm.broadcast import broadcast from bookwyrm import forms, models, outgoing from bookwyrm import goodreads_import from bookwyrm.emailing import password_reset_email @@ -256,12 +257,16 @@ def switch_edition(request): new_edition = get_object_or_404(models.Edition, id=edition_id) shelfbooks = models.ShelfBook.objects.filter( book__parent_work=new_edition.parent_work, - added_by=request.user + shelf__user=request.user ) for shelfbook in shelfbooks.all(): + broadcast(request.user, shelfbook.to_remove_activity(request.user)) + shelfbook.book = new_edition shelfbook.save() + broadcast(request.user, shelfbook.to_add_activity(request.user)) + readthroughs = models.ReadThrough.objects.filter( book__parent_work=new_edition.parent_work, user=request.user