Display results of import.

This commit is contained in:
Adam Kelly 2020-03-24 16:58:11 +00:00
parent 8fb082e2fc
commit 2188371f44
2 changed files with 59 additions and 2 deletions

View 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 %}

View file

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