mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-26 11:31:08 +00:00
Display results of import.
This commit is contained in:
parent
8fb082e2fc
commit
2188371f44
2 changed files with 59 additions and 2 deletions
28
fedireads/templates/import_results.html
Normal file
28
fedireads/templates/import_results.html
Normal file
|
@ -0,0 +1,28 @@
|
|||
{% extends 'layout.html' %}
|
||||
{% block content %}
|
||||
<div id="content">
|
||||
<div>
|
||||
<h1>Search results</h1>
|
||||
{% for book in results %}
|
||||
<div>
|
||||
<a href="{{ book.absolute_id }}">
|
||||
{% include 'snippets/book_cover.html' with book=book %}
|
||||
</a>
|
||||
{{ book.title }}
|
||||
{{ book.author }}
|
||||
{% include 'snippets/shelve_button.html' with book=book pulldown=True %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
<h1>Failures</h1>
|
||||
<ul>
|
||||
{% for book in failures %}
|
||||
<li>
|
||||
{{ book.Title }}
|
||||
{{ book.Author }}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
|
@ -297,20 +297,49 @@ def unquote_string(text):
|
|||
else:
|
||||
return text
|
||||
|
||||
def construct_search_term(title, author):
|
||||
# Strip brackets (usually series title from search term)
|
||||
title = re.sub(r'\s*\([^)]*\)\s*', '', title)
|
||||
# Open library doesn't like including author initials in search term.
|
||||
author = re.sub(r'(\w\.)+\s*', '', author)
|
||||
|
||||
return ' '.join([title, author])
|
||||
|
||||
import itertools
|
||||
from io import TextIOWrapper
|
||||
from requests import HTTPError
|
||||
|
||||
@login_required
|
||||
def import_data(request):
|
||||
form = forms.ImportForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
reader = csv.DictReader(TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding))
|
||||
for line in itertools.islice(reader, 10):
|
||||
results = []
|
||||
failures = []
|
||||
for line in itertools.islice(reader, 20):
|
||||
isbn = unquote_string(line['ISBN13'])
|
||||
print(line['Title'], isbn, line['Exclusive Shelf'])
|
||||
search_results = books_manager.search(isbn)
|
||||
if search_results:
|
||||
book = books_manager.get_or_create_book(search_results[0].key)
|
||||
return HttpResponse('thanks')
|
||||
print(book)
|
||||
results.append(book)
|
||||
else:
|
||||
try:
|
||||
search_term = construct_search_term(line['Title'], line['Author'])
|
||||
print("Search term: ", search_term)
|
||||
search_results = books_manager.search(search_term)
|
||||
if search_results:
|
||||
book = books_manager.get_or_create_book(search_results[0].key)
|
||||
print(book)
|
||||
results.append(book)
|
||||
else:
|
||||
failures.append(line)
|
||||
except HTTPError:
|
||||
failures.append(line) #
|
||||
return TemplateResponse(request, 'import_results.html', {
|
||||
'results': results,
|
||||
'failures': failures
|
||||
})
|
||||
else:
|
||||
return HttpResponseBadRequest()
|
||||
|
|
Loading…
Reference in a new issue