mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-30 20:10:36 +00:00
Adds shelf mappings
This commit is contained in:
parent
147dd95e8d
commit
f3bcced0a0
3 changed files with 20 additions and 1 deletions
|
@ -36,6 +36,11 @@ class Importer:
|
||||||
"date_finished": ["date finished", "last date read", "date read", "finished"],
|
"date_finished": ["date finished", "last date read", "date read", "finished"],
|
||||||
}
|
}
|
||||||
date_fields = ["date_added", "date_started", "date_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):
|
def create_job(self, user, csv_file, include_reviews, privacy):
|
||||||
"""check over a csv and creates a database entry for the job"""
|
"""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):
|
def create_item(self, job, index, data):
|
||||||
"""creates and saves an import item"""
|
"""creates and saves an import item"""
|
||||||
normalized = self.normalize_row(data, job.mappings)
|
normalized = self.normalize_row(data, job.mappings)
|
||||||
|
normalized["shelf"] = self.get_shelf(normalized)
|
||||||
ImportItem(job=job, index=index, data=data, normalized_data=normalized).save()
|
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
|
def normalize_row(self, entry, mappings): # pylint: disable=no-self-use
|
||||||
"""use the dataclass to create the formatted row of data"""
|
"""use the dataclass to create the formatted row of data"""
|
||||||
return {k: entry.get(v) for k, v in mappings.items()}
|
return {k: entry.get(v) for k, v in mappings.items()}
|
||||||
|
|
|
@ -17,3 +17,10 @@ class LibrarythingImporter(Importer):
|
||||||
date = normalized[date_field]
|
date = normalized[date_field]
|
||||||
normalized[date_field] = re.sub(r"\[|\]", "", date)
|
normalized[date_field] = re.sub(r"\[|\]", "", date)
|
||||||
return normalized
|
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"
|
||||||
|
|
|
@ -172,6 +172,6 @@ class LibrarythingImport(TestCase):
|
||||||
)
|
)
|
||||||
review = models.Review.objects.get(book=self.book, user=self.local_user)
|
review = models.Review.objects.get(book=self.book, user=self.local_user)
|
||||||
self.assertEqual(review.content, "chef d'oeuvre")
|
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.published_date, make_date(2007, 5, 8))
|
||||||
self.assertEqual(review.privacy, "unlisted")
|
self.assertEqual(review.privacy, "unlisted")
|
||||||
|
|
Loading…
Reference in a new issue