From 6b615d45b3fc237107521f76ecca2aa2cfe07e9b Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Mon, 26 Aug 2024 13:57:22 -0700 Subject: [PATCH] Adds a couple suggestions tests --- bookwyrm/models/list.py | 5 +++ bookwyrm/tests/views/books/test_book.py | 50 ++++++++++++++++++++++++- bookwyrm/views/books/books.py | 6 +-- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/bookwyrm/models/list.py b/bookwyrm/models/list.py index 76fc91c6e..a79123cc6 100644 --- a/bookwyrm/models/list.py +++ b/bookwyrm/models/list.py @@ -162,6 +162,11 @@ class List(OrderedCollectionMixin, BookWyrmModel): self.embed_key = uuid.uuid4() update_fields = add_update_fields(update_fields, "embed_key") + # ensure that suggestion lists have the right properties + if self.suggests_for: + self.privacy = "public" + self.curation = "open" + super().save(*args, update_fields=update_fields, **kwargs) diff --git a/bookwyrm/tests/views/books/test_book.py b/bookwyrm/tests/views/books/test_book.py index ee6e7d8b4..d9048d549 100644 --- a/bookwyrm/tests/views/books/test_book.py +++ b/bookwyrm/tests/views/books/test_book.py @@ -51,6 +51,11 @@ class BookViews(TestCase): remote_id="https://example.com/book/1", parent_work=cls.work, ) + cls.another_book = models.Edition.objects.create( + title="Another Example Edition", + remote_id="https://example.com/book/1", + parent_work=models.Work.objects.create(title="Another Work"), + ) models.SiteSettings.objects.create() @@ -267,7 +272,7 @@ class BookViews(TestCase): """make sure the endposition is served as well""" view = views.Book.as_view() - _ = models.Quotation.objects.create( + models.Quotation.objects.create( user=self.local_user, book=self.book, content="hi", @@ -290,6 +295,49 @@ class BookViews(TestCase): result.context_data["statuses"].object_list[0].endposition, "13" ) + def test_create_suggestion_list(self, *_): + """start a suggestion list for a book""" + self.assertFalse(hasattr(self.book, "suggestion_list")) + + view = views.create_suggestion_list + form = forms.SuggestionListForm() + form.data["user"] = self.local_user.id + form.data["suggests_for"] = self.book.id + request = self.factory.post("", form.data) + request.user = self.local_user + + view(request, self.book.id) + + self.book.refresh_from_db() + self.assertTrue(hasattr(self.book, "suggestion_list")) + + suggestion_list = self.book.suggestion_list + self.assertEqual(suggestion_list.suggests_for, self.book) + self.assertEqual(suggestion_list.privacy, "public") + self.assertEqual(suggestion_list.curation, "open") + + def test_book_add_suggestion(self, *_): + """Add a book to the recommendation list""" + suggestion_list = models.List.objects.create( + suggests_for=self.book, user=self.local_user + ) + view = views.book_add_suggestion + form = forms.ListItemForm() + form.data["user"] = self.local_user.id + form.data["book"] = self.another_book.id + form.data["book_list"] = suggestion_list.id + form.data["notes"] = "hello" + request = self.factory.post("", form.data) + request.user = self.local_user + + view(request, self.book.id) + + self.assertEqual(suggestion_list.listitem_set.count(), 1) + item = suggestion_list.listitem_set.first() + self.assertEqual(item.book, self.another_book) + self.assertEqual(item.user, self.local_user) + self.assertEqual(item.notes, "hello") + def _setup_cover_url(): """creates cover url mock""" diff --git a/bookwyrm/views/books/books.py b/bookwyrm/views/books/books.py index 3ecf97f7f..9c017538b 100644 --- a/bookwyrm/views/books/books.py +++ b/bookwyrm/views/books/books.py @@ -249,11 +249,9 @@ def create_suggestion_list(request, book_id): if not form.is_valid(): return redirect("book", book.id) + # saving in two steps means django uses the model's custom save functionality, + # which adds an embed key and fixes the privacy and curation settings suggestion_list = form.save(request, commit=False) - - # default values for the suggestion list - suggestion_list.privacy = "public" - suggestion_list.curation = "open" suggestion_list.save() return redirect("book", book.id)