diff --git a/bookwyrm/migrations/0044_auto_20210207_1924.py b/bookwyrm/migrations/0044_auto_20210207_1924.py new file mode 100644 index 000000000..ec95f7e20 --- /dev/null +++ b/bookwyrm/migrations/0044_auto_20210207_1924.py @@ -0,0 +1,30 @@ +# Generated by Django 3.0.7 on 2021-02-07 19:24 + +import bookwyrm.models.fields +from django.conf import settings +from django.db import migrations +import django.db.models.deletion + +def set_user(app_registry, schema_editor): + db_alias = schema_editor.connection.alias + shelfbook = app_registry.get_model('bookwyrm', 'ShelfBook') + for item in shelfbook.objects.using(db_alias).filter(user__isnull=True): + item.user = item.shelf.user + item.save(broadcast=False) + + +class Migration(migrations.Migration): + + dependencies = [ + ('bookwyrm', '0043_auto_20210204_2223'), + ] + + operations = [ + migrations.RunPython(set_user), + migrations.AlterField( + model_name='shelfbook', + name='user', + field=bookwyrm.models.fields.ForeignKey(default=2, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), + preserve_default=False, + ), + ] diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index 38e5913b2..921b8617c 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -55,12 +55,7 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel): shelf = fields.ForeignKey( 'Shelf', on_delete=models.PROTECT, activitypub_field='target') user = fields.ForeignKey( - 'User', - blank=True, - null=True, - on_delete=models.PROTECT, - activitypub_field='actor' - ) + 'User', on_delete=models.PROTECT, activitypub_field='actor') activity_serializer = activitypub.AddBook object_field = 'book' diff --git a/bookwyrm/tests/views/test_shelf.py b/bookwyrm/tests/views/test_shelf.py index 65f7ade52..4fa63689a 100644 --- a/bookwyrm/tests/views/test_shelf.py +++ b/bookwyrm/tests/views/test_shelf.py @@ -25,11 +25,12 @@ class ShelfViews(TestCase): remote_id='https://example.com/book/1', parent_work=self.work ) - self.shelf = models.Shelf.objects.create( - name='Test Shelf', - identifier='test-shelf', - user=self.local_user - ) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + self.shelf = models.Shelf.objects.create( + name='Test Shelf', + identifier='test-shelf', + user=self.local_user + ) models.SiteSettings.objects.create() @@ -96,7 +97,8 @@ class ShelfViews(TestCase): 'name': 'cool name' }) request.user = self.local_user - view(request, request.user.username, shelf.identifier) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + view(request, request.user.username, shelf.identifier) shelf.refresh_from_db() self.assertEqual(shelf.name, 'cool name') @@ -116,7 +118,8 @@ class ShelfViews(TestCase): 'name': 'cool name' }) request.user = self.local_user - view(request, request.user.username, shelf.identifier) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + view(request, request.user.username, shelf.identifier) self.assertEqual(shelf.name, 'To Read') @@ -128,7 +131,8 @@ class ShelfViews(TestCase): 'shelf': self.shelf.identifier }) request.user = self.local_user - views.shelve(request) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + views.shelve(request) # make sure the book is on the shelf self.assertEqual(self.shelf.books.get(), self.book) @@ -142,7 +146,8 @@ class ShelfViews(TestCase): }) request.user = self.local_user - views.shelve(request) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + views.shelve(request) # make sure the book is on the shelf self.assertEqual(shelf.books.get(), self.book) @@ -156,7 +161,8 @@ class ShelfViews(TestCase): }) request.user = self.local_user - views.shelve(request) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + views.shelve(request) # make sure the book is on the shelf self.assertEqual(shelf.books.get(), self.book) @@ -178,7 +184,12 @@ class ShelfViews(TestCase): def test_handle_unshelve(self, _): ''' remove a book from a shelf ''' - self.shelf.books.add(self.book) + with patch('bookwyrm.models.activitypub_mixin.broadcast_task.delay'): + models.ShelfBook.objects.create( + book=self.book, + user=self.local_user, + shelf=self.shelf + ) self.shelf.save() self.assertEqual(self.shelf.books.count(), 1) request = self.factory.post('', {