Formatting

This commit is contained in:
Mouse Reeve 2021-03-11 16:40:35 -08:00
parent c1976dbd62
commit 28db3e2733

View file

@ -131,22 +131,24 @@ class EditBook(View):
# we're adding an author through a free text field # we're adding an author through a free text field
if add_author: if add_author:
data["add_author"] = add_author data["add_author"] = add_author
data['author_matches'] = [] data["author_matches"] = []
for author in add_author.split(','): for author in add_author.split(","):
# check for existing authors # check for existing authors
vector = SearchVector("name", weight="A") + SearchVector( vector = SearchVector("name", weight="A") + SearchVector(
"aliases", weight="B" "aliases", weight="B"
) )
data["author_matches"].append({ data["author_matches"].append(
'name': author.strip(), {
'matches': ( "name": author.strip(),
models.Author.objects.annotate(search=vector) "matches": (
.annotate(rank=SearchRank(vector, add_author)) models.Author.objects.annotate(search=vector)
.filter(rank__gt=0.4) .annotate(rank=SearchRank(vector, add_author))
.order_by("-rank")[:5] .filter(rank__gt=0.4)
) .order_by("-rank")[:5]
}) ),
}
)
# we're creating a new book # we're creating a new book
if not book: if not book:
@ -197,16 +199,14 @@ class ConfirmEditBook(View):
# get or create author as needed # get or create author as needed
if request.POST.get("add_author"): if request.POST.get("add_author"):
for (i, author) in enumerate(request.POST.get("add_author").split(',')): for (i, author) in enumerate(request.POST.get("add_author").split(",")):
match = request.POST.get("author_match-%d" % i) match = request.POST.get("author_match-%d" % i)
if match and match != "0": if match and match != "0":
author = get_object_or_404( author = get_object_or_404(
models.Author, id=request.POST["author_match-%d" % i] models.Author, id=request.POST["author_match-%d" % i]
) )
else: else:
author = models.Author.objects.create( author = models.Author.objects.create(name=author.strip())
name=author.strip()
)
book.authors.add(author) book.authors.add(author)
# create work, if needed # create work, if needed