Save and continue from get started books view

This commit is contained in:
Mouse Reeve 2021-04-01 09:02:48 -07:00
parent e2388d8f67
commit ed127f4e07
4 changed files with 72 additions and 8 deletions

View file

@ -22,8 +22,9 @@
</div>
<form class="block" name="add-books" method="post" action="{% url 'get-started-books' %}">
{% csrf_token %}
<h3 class="title is-5">{% trans "Suggested Books" %}</h3>
<div class="columns scroll-x">
<fieldset name="books" class="columns scroll-x">
{% if book_results %}
<div class="column is-narrow content">
<p class="help mb-0">Search results</p>
@ -34,6 +35,7 @@
</div>
</div>
{% endif %}
{% if popular_books %}
<div class="column is-narrow content">
<p class="help mb-0">
{% blocktrans %}Popular on {{ site_name }}{% endblocktrans %}
@ -44,7 +46,11 @@
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% if not book_results and not popular_books %}
<p><em>{% trans "No books found" %}</em></p>
{% endif %}
</fieldset>
<button type="submit" class="button is-primary">{% trans "Save &amp; continue" %}</button>
</form>
{% endblock %}

View file

@ -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()

View file

@ -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):

View file

@ -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)