Adds shelf mappings

This commit is contained in:
Mouse Reeve 2021-11-11 12:29:38 -08:00
parent 147dd95e8d
commit f3bcced0a0
3 changed files with 20 additions and 1 deletions

View file

@ -36,6 +36,11 @@ class Importer:
"date_finished": ["date finished", "last date read", "date read", "finished"],
}
date_fields = ["date_added", "date_started", "date_finished"]
shelf_mapping_guesses = {
"to-read": ["to-read"],
"read": ["read"],
"reading": ["currently-reading", "reading"],
}
def create_job(self, user, csv_file, include_reviews, privacy):
"""check over a csv and creates a database entry for the job"""
@ -66,8 +71,15 @@ class Importer:
def create_item(self, job, index, data):
"""creates and saves an import item"""
normalized = self.normalize_row(data, job.mappings)
normalized["shelf"] = self.get_shelf(normalized)
ImportItem(job=job, index=index, data=data, normalized_data=normalized).save()
def get_shelf(self, normalized_row):
"""determine which shelf to use"""
shelf_name = normalized_row["shelf"]
shelf = [s for (s, gs) in self.shelf_mapping_guesses if shelf_name in gs]
return shelf[0] if shelf else None
def normalize_row(self, entry, mappings): # pylint: disable=no-self-use
"""use the dataclass to create the formatted row of data"""
return {k: entry.get(v) for k, v in mappings.items()}

View file

@ -17,3 +17,10 @@ class LibrarythingImporter(Importer):
date = normalized[date_field]
normalized[date_field] = re.sub(r"\[|\]", "", date)
return normalized
def get_shelf(self, normalized_row):
if normalized_row["date_finished"]:
return "read"
if normalized_row["date_started"]:
return "reading"
return "to-read"

View file

@ -172,6 +172,6 @@ class LibrarythingImport(TestCase):
)
review = models.Review.objects.get(book=self.book, user=self.local_user)
self.assertEqual(review.content, "chef d'oeuvre")
self.assertEqual(review.rating, 5)
self.assertEqual(review.rating, 4.5)
self.assertEqual(review.published_date, make_date(2007, 5, 8))
self.assertEqual(review.privacy, "unlisted")