diff --git a/bookwyrm/templates/get_started/books.html b/bookwyrm/templates/get_started/books.html index 3f40550c..9e2e422c 100644 --- a/bookwyrm/templates/get_started/books.html +++ b/bookwyrm/templates/get_started/books.html @@ -22,8 +22,9 @@
{% endblock %} diff --git a/bookwyrm/tests/views/test_get_started.py b/bookwyrm/tests/views/test_get_started.py index 11896f30..89cc5a26 100644 --- a/bookwyrm/tests/views/test_get_started.py +++ b/bookwyrm/tests/views/test_get_started.py @@ -25,6 +25,9 @@ class GetStartedViews(TestCase): title="Example Edition", remote_id="https://example.com/book/1", ) + models.Connector.objects.create( + identifier="self", connector_file="self_connector", local=True + ) models.SiteSettings.objects.create() def test_profile_view(self): @@ -69,6 +72,36 @@ class GetStartedViews(TestCase): result.render() self.assertEqual(result.status_code, 200) + def test_books_view_with_query(self): + """ there are so many views, this just makes sure it LOADS """ + view = views.GetStartedBooks.as_view() + request = self.factory.get("?query=Example") + request.user = self.local_user + + result = view(request) + + self.assertIsInstance(result, TemplateResponse) + result.render() + self.assertEqual(result.status_code, 200) + + def test_books_view_post(self): + """ shelve some books """ + view = views.GetStartedBooks.as_view() + data = {self.book.id: self.local_user.shelf_set.first().id} + request = self.factory.post("", data) + request.user = self.local_user + + self.assertFalse(self.local_user.shelfbook_set.exists()) + with patch( + "bookwyrm.models.activitypub_mixin.broadcast_task.delay" + ) as delay_mock: + view(request) + self.assertEqual(delay_mock.call_count, 1) + + shelfbook = self.local_user.shelfbook_set.first() + self.assertEqual(shelfbook.book, self.book) + self.assertEqual(shelfbook.user, self.local_user) + def test_users_view(self): """ there are so many views, this just makes sure it LOADS """ view = views.GetStartedUsers.as_view() diff --git a/bookwyrm/views/get_started.py b/bookwyrm/views/get_started.py index 7bf236c8..fba01840 100644 --- a/bookwyrm/views/get_started.py +++ b/bookwyrm/views/get_started.py @@ -1,7 +1,10 @@ """ Helping new users figure out the lay of the land """ +import re + from django.contrib.auth.decorators import login_required from django.db.models import Count, Q -from django.shortcuts import redirect +from django.http import HttpResponseNotFound +from django.shortcuts import get_object_or_404, redirect from django.template.response import TemplateResponse from django.utils.decorators import method_decorator from django.views import View @@ -17,11 +20,13 @@ from .user import save_user_form class GetStartedProfile(View): """ tell us about yourself """ + next_view = "get-started-books" + def get(self, request): """ basic profile info """ data = { "form": forms.LimitedEditUserForm(instance=request.user), - "next": "get-started-books", + "next": self.next_view, } return TemplateResponse(request, "get_started/profile.html", data) @@ -34,13 +39,15 @@ class GetStartedProfile(View): data = {"form": form, "next": "get-started-books"} return TemplateResponse(request, "get_started/profile.html", data) save_user_form(form) - return redirect('get-started-books') + return redirect(self.next_view) @method_decorator(login_required, name="dispatch") class GetStartedBooks(View): """ name a book, any book, we gotta start somewhere """ + next_view = "get-started-users" + def get(self, request): """ info about a book """ query = request.GET.get("query") @@ -57,8 +64,9 @@ class GetStartedBooks(View): for b in request.user.shelfbook_set.distinct().all() ] ) - | # - or if it's already in search results - Q(parent_work__in=[b.parent_work for b in book_results]) + | Q( # and exclude if it's already in search results + parent_work__in=[b.parent_work for b in book_results] + ) ) .annotate(Count("shelfbook")) .order_by("-shelfbook__count")[: 5 - len(book_results)] @@ -67,10 +75,26 @@ class GetStartedBooks(View): data = { "book_results": book_results, "popular_books": popular_books, - "next": "get-started-users", + "next": self.next_view, } return TemplateResponse(request, "get_started/books.html", data) + def post(self, request): + """ shelve some books """ + shelve_actions = [ + (k, v) + for k, v in request.POST.items() + if re.match(r"\d+", k) and re.match(r"\d+", v) + ] + for (book_id, shelf_id) in shelve_actions: + book = get_object_or_404(models.Edition, id=book_id) + shelf = get_object_or_404(models.Shelf, id=shelf_id) + if shelf.user != request.user: + # hmmmmm + return HttpResponseNotFound() + models.ShelfBook.objects.create(book=book, shelf=shelf, user=request.user) + return redirect(self.next_view) + @method_decorator(login_required, name="dispatch") class GetStartedUsers(View): diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index cce921be..fbc41358 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -167,6 +167,7 @@ class EditUser(View): return redirect(user.local_path) + def save_user_form(form): """ special handling for the user form """ user = form.save(commit=False)