From c1976dbd62bf5491b4e5036e2005ddd1ba61db12 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Thu, 11 Mar 2021 16:33:49 -0800 Subject: [PATCH] Add multiple authors --- bookwyrm/templates/edit_book.html | 55 ++++++++++++++++++------------- bookwyrm/views/books.py | 47 +++++++++++++++----------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/bookwyrm/templates/edit_book.html b/bookwyrm/templates/edit_book.html index e42f77f2..107e75c5 100644 --- a/bookwyrm/templates/edit_book.html +++ b/bookwyrm/templates/edit_book.html @@ -39,29 +39,37 @@

{% trans "Confirm Book Info" %}

- {% if author_matches.exists %} -
- {% blocktrans with name=add_author %}Is "{{ name }}" an existing author?{% endblocktrans %} - {% for match in author_matches %} - -

- {% blocktrans with book_title=match.book_set.first.title %}Author of {{ book_title }}{% endblocktrans %} -

+ {% if author_matches %} +
+ {% for author in author_matches %} +
+ {% blocktrans with name=author.name %}Is "{{ name }}" an existing author?{% endblocktrans %} + {% with forloop.counter as counter %} + {% for match in author.matches %} + +

+ {% blocktrans with book_title=match.book_set.first.title %}Author of {{ book_title }}{% endblocktrans %} +

+ {% endfor %} + + {% endwith %} +
{% endfor %} - -
+
{% else %}

{% blocktrans with name=add_author %}Creating a new author: {{ name }}{% endblocktrans %}

{% endif %} {% if not book %} -
- {% trans "Is this an editions of an existing work?" %} - {% for match in book_matches %} - - {% endfor %} - -
+
+
+ {% trans "Is this an edition of an existing work?" %} + {% for match in book_matches %} + + {% endfor %} + +
+
{% endif %}
@@ -109,16 +117,19 @@

{% trans "Authors" %}

+ {% if book.authors.exists %}
{% for author in book.authors.all %} -

{{ author.name }} -

- - + {% endif %} + +

Separate multiple author names with commas.

+
diff --git a/bookwyrm/views/books.py b/bookwyrm/views/books.py index 9048f43d..ff0d6764 100644 --- a/bookwyrm/views/books.py +++ b/bookwyrm/views/books.py @@ -131,17 +131,22 @@ class EditBook(View): # we're adding an author through a free text field if add_author: data["add_author"] = add_author - # check for existing authors - vector = SearchVector("name", weight="A") + SearchVector( - "aliases", weight="B" - ) + data['author_matches'] = [] + for author in add_author.split(','): + # check for existing authors + vector = SearchVector("name", weight="A") + SearchVector( + "aliases", weight="B" + ) - data["author_matches"] = ( - models.Author.objects.annotate(search=vector) - .annotate(rank=SearchRank(vector, add_author)) - .filter(rank__gt=0.4) - .order_by("-rank")[:5] - ) + data["author_matches"].append({ + 'name': author.strip(), + 'matches': ( + models.Author.objects.annotate(search=vector) + .annotate(rank=SearchRank(vector, add_author)) + .filter(rank__gt=0.4) + .order_by("-rank")[:5] + ) + }) # we're creating a new book if not book: @@ -157,6 +162,8 @@ class EditBook(View): if add_author or not book: # creting a book or adding an author to a book needs another step 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") return TemplateResponse(request, "edit_book.html", data) remove_authors = request.POST.getlist("remove_authors") @@ -190,15 +197,17 @@ class ConfirmEditBook(View): # get or create author as needed if request.POST.get("add_author"): - if request.POST.get("author_match"): - author = get_object_or_404( - models.Author, id=request.POST["author_match"] - ) - else: - author = models.Author.objects.create( - name=request.POST.get("add_author") - ) - book.authors.add(author) + for (i, author) in enumerate(request.POST.get("add_author").split(',')): + match = request.POST.get("author_match-%d" % i) + if match and match != "0": + author = get_object_or_404( + models.Author, id=request.POST["author_match-%d" % i] + ) + else: + author = models.Author.objects.create( + name=author.strip() + ) + book.authors.add(author) # create work, if needed if not book_id: