Fixes import retry

This commit is contained in:
Mouse Reeve 2021-03-30 08:43:38 -07:00
parent 4b325fedee
commit a54014f693
5 changed files with 34 additions and 23 deletions

View file

@ -1,14 +1,16 @@
""" handle reading a csv from goodreads """ """ handle reading a csv from goodreads """
from bookwyrm.importer import Importer from bookwyrm.importer import Importer
# GoodReads is the default importer, thus Importer follows its structure. For a more complete example of overriding see librarything_import.py
class GoodreadsImporter(Importer): class GoodreadsImporter(Importer):
""" GoodReads is the default importer, thus Importer follows its structure.
For a more complete example of overriding see librarything_import.py """
service = "GoodReads" service = "GoodReads"
def parse_fields(self, data): def parse_fields(self, entry):
data.update({"import_source": self.service}) """ handle the specific fields in goodreads csvs """
entry.update({"import_source": self.service})
# add missing 'Date Started' field # add missing 'Date Started' field
data.update({"Date Started": None}) entry.update({"Date Started": None})
return data return entry

View file

@ -10,6 +10,8 @@ logger = logging.getLogger(__name__)
class Importer: class Importer:
""" Generic class for csv data import from an outside service """
service = "Unknown" service = "Unknown"
delimiter = "," delimiter = ","
encoding = "UTF-8" encoding = "UTF-8"
@ -29,10 +31,12 @@ class Importer:
self.save_item(job, index, entry) self.save_item(job, index, entry)
return job return job
def save_item(self, job, index, data): def save_item(self, job, index, data):# pylint: disable=no-self-use
""" creates and saves an import item """
ImportItem(job=job, index=index, data=data).save() ImportItem(job=job, index=index, data=data).save()
def parse_fields(self, entry): def parse_fields(self, entry):
""" updates csv data with additional info """
entry.update({"import_source": self.service}) entry.update({"import_source": self.service})
return entry return entry

View file

@ -1,35 +1,35 @@
""" handle reading a csv from librarything """ """ handle reading a csv from librarything """
import csv
import re import re
import math import math
from bookwyrm import models
from bookwyrm.models import ImportItem
from bookwyrm.importer import Importer from bookwyrm.importer import Importer
class LibrarythingImporter(Importer): class LibrarythingImporter(Importer):
""" csv downloads from librarything """
service = "LibraryThing" service = "LibraryThing"
delimiter = "\t" delimiter = "\t"
encoding = "ISO-8859-1" encoding = "ISO-8859-1"
# mandatory_fields : fields matching the book title and author # mandatory_fields : fields matching the book title and author
mandatory_fields = ["Title", "Primary Author"] mandatory_fields = ["Title", "Primary Author"]
def parse_fields(self, initial): def parse_fields(self, entry):
""" custom parsing for librarything """
data = {} data = {}
data["import_source"] = self.service data["import_source"] = self.service
data["Book Id"] = initial["Book Id"] data["Book Id"] = entry["Book Id"]
data["Title"] = initial["Title"] data["Title"] = entry["Title"]
data["Author"] = initial["Primary Author"] data["Author"] = entry["Primary Author"]
data["ISBN13"] = initial["ISBN"] data["ISBN13"] = entry["ISBN"]
data["My Review"] = initial["Review"] data["My Review"] = entry["Review"]
if initial["Rating"]: if entry["Rating"]:
data["My Rating"] = math.ceil(float(initial["Rating"])) data["My Rating"] = math.ceil(float(entry["Rating"]))
else: else:
data["My Rating"] = "" data["My Rating"] = ""
data["Date Added"] = re.sub("\[|\]", "", initial["Entry Date"]) data["Date Added"] = re.sub(r"\[|\]", "", entry["Entry Date"])
data["Date Started"] = re.sub("\[|\]", "", initial["Date Started"]) data["Date Started"] = re.sub(r"\[|\]", "", entry["Date Started"])
data["Date Read"] = re.sub("\[|\]", "", initial["Date Read"]) data["Date Read"] = re.sub(r"\[|\]", "", entry["Date Read"])
data["Exclusive Shelf"] = None data["Exclusive Shelf"] = None
if data["Date Read"]: if data["Date Read"]:

View file

@ -45,3 +45,6 @@ class ImportViews(TestCase):
self.assertIsInstance(result, TemplateResponse) self.assertIsInstance(result, TemplateResponse)
result.render() result.render()
self.assertEqual(result.status_code, 200) self.assertEqual(result.status_code, 200)
def test_retry_import(self):
""" retry failed items """

View file

@ -10,6 +10,7 @@ from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from bookwyrm import forms, goodreads_import, librarything_import, models from bookwyrm import forms, goodreads_import, librarything_import, models
from bookwyrm.importer import Importer
from bookwyrm.tasks import app from bookwyrm.tasks import app
# pylint: disable= no-self-use # pylint: disable= no-self-use
@ -89,10 +90,11 @@ class ImportStatus(View):
for item in request.POST.getlist("import_item"): for item in request.POST.getlist("import_item"):
items.append(get_object_or_404(models.ImportItem, id=item)) items.append(get_object_or_404(models.ImportItem, id=item))
job = goodreads_import.create_retry_job( importer = Importer()
job = importer.create_retry_job(
request.user, request.user,
job, job,
items, items,
) )
goodreads_import.start_import(job) importer.start_import(job)
return redirect("/import/%d" % job.id) return redirect("/import/%d" % job.id)