Shelve the books.

This commit is contained in:
Adam Kelly 2020-03-25 12:58:27 +00:00
parent ce446d57fc
commit 323c7f8dbf
2 changed files with 19 additions and 1 deletions

View file

@ -5,6 +5,13 @@ from requests import HTTPError
from fedireads import books_manager
# Mapping goodreads -> fedireads shelf titles.
GOODREADS_SHELVES = {
'read': 'read',
'currently-reading': 'reading',
'to-read': 'to-read',
}
def unquote_string(text):
match = re.match(r'="([^"]*)"', text)
if match:
@ -25,7 +32,7 @@ class GoodreadsCsv(object):
self.reader = csv.DictReader(csv_file)
def __iter__(self):
for line in itertools.islice(self.reader, 20, 30):
for line in itertools.islice(self.reader, 30):
entry = GoodreadsItem(line)
try:
entry.resolve()
@ -55,5 +62,10 @@ class GoodreadsItem(object):
if search_results:
return books_manager.get_or_create_book(search_results[0].key)
@property
def shelf(self):
if self.line['Exclusive Shelf']:
return GOODREADS_SHELVES[self.line['Exclusive Shelf']]
def __repr__(self):
return "<GoodreadsItem {!r}>".format(self.line['Title'])

View file

@ -300,6 +300,12 @@ def import_data(request):
for item in GoodreadsCsv(TextIOWrapper(request.FILES['csv_file'], encoding=request.encoding)):
if item.book:
results.append(item.book)
if item.shelf:
desired_shelf = models.Shelf.objects.get(
identifier=item.shelf,
user=request.user
)
outgoing.handle_shelve(request.user, item.book, desired_shelf)
else:
failures.append(item)
return TemplateResponse(request, 'import_results.html', {