Merge branch 'main' into import-limit
This commit is contained in:
commit
d0adb370cd
45 changed files with 863 additions and 884 deletions
|
@ -43,6 +43,7 @@ class EditBook(View):
|
|||
form = forms.EditionForm(request.POST, request.FILES, instance=book)
|
||||
|
||||
data = {"book": book, "form": form}
|
||||
ensure_transient_values_persist(request, data)
|
||||
if not form.is_valid():
|
||||
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
||||
|
||||
|
@ -101,6 +102,8 @@ class CreateBook(View):
|
|||
"authors": authors,
|
||||
}
|
||||
|
||||
ensure_transient_values_persist(request, data)
|
||||
|
||||
if not form.is_valid():
|
||||
return TemplateResponse(request, "book/edit/edit_book.html", data)
|
||||
|
||||
|
@ -136,6 +139,11 @@ class CreateBook(View):
|
|||
return redirect(f"/book/{book.id}")
|
||||
|
||||
|
||||
def ensure_transient_values_persist(request, data):
|
||||
"""ensure that values of transient form fields persist when re-rendering the form"""
|
||||
data["cover_url"] = request.POST.get("cover-url")
|
||||
|
||||
|
||||
def add_authors(request, data):
|
||||
"""helper for adding authors"""
|
||||
add_author = [author for author in request.POST.getlist("add_author") if author]
|
||||
|
@ -150,7 +158,6 @@ def add_authors(request, data):
|
|||
data["confirm_mode"] = True
|
||||
# this isn't preserved because it isn't part of the form obj
|
||||
data["remove_authors"] = request.POST.getlist("remove_authors")
|
||||
data["cover_url"] = request.POST.get("cover-url")
|
||||
|
||||
for author in add_author:
|
||||
# filter out empty author fields
|
||||
|
|
|
@ -94,7 +94,7 @@ class List(View):
|
|||
return redirect(book_list.local_path)
|
||||
|
||||
|
||||
def get_list_suggestions(book_list, user, query=None):
|
||||
def get_list_suggestions(book_list, user, query=None, num_suggestions=5):
|
||||
"""What books might a user want to add to a list"""
|
||||
if query:
|
||||
# search for books
|
||||
|
@ -103,20 +103,26 @@ def get_list_suggestions(book_list, user, query=None):
|
|||
filters=[~Q(parent_work__editions__in=book_list.books.all())],
|
||||
)
|
||||
# just suggest whatever books are nearby
|
||||
suggestions = user.shelfbook_set.filter(~Q(book__in=book_list.books.all()))
|
||||
suggestions = [s.book for s in suggestions[:5]]
|
||||
if len(suggestions) < 5:
|
||||
suggestions += [
|
||||
suggestions = user.shelfbook_set.filter(
|
||||
~Q(book__in=book_list.books.all())
|
||||
).distinct()[:num_suggestions]
|
||||
suggestions = [s.book for s in suggestions[:num_suggestions]]
|
||||
if len(suggestions) < num_suggestions:
|
||||
others = [
|
||||
s.default_edition
|
||||
for s in models.Work.objects.filter(
|
||||
~Q(editions__in=book_list.books.all()),
|
||||
).order_by("-updated_date")[: 5 - len(suggestions)]
|
||||
)
|
||||
.distinct()
|
||||
.order_by("-updated_date")[:num_suggestions]
|
||||
]
|
||||
# get 'num_suggestions' unique items
|
||||
suggestions = list(set(suggestions + others))[:num_suggestions]
|
||||
return suggestions
|
||||
|
||||
|
||||
def sort_list(request, items):
|
||||
"""helper to handle the surprisngly involved sorting"""
|
||||
"""helper to handle the surprisingly involved sorting"""
|
||||
# sort_by shall be "order" unless a valid alternative is given
|
||||
sort_by = request.GET.get("sort_by", "order")
|
||||
if sort_by not in ("order", "title", "rating"):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue