Safer request for normalized data

This commit is contained in:
Mouse Reeve 2021-11-14 10:29:12 -08:00
parent 31f3351854
commit 9f6796bbf5
1 changed files with 11 additions and 11 deletions

View File

@ -131,18 +131,18 @@ class ImportItem(models.Model):
@property @property
def title(self): def title(self):
"""get the book title""" """get the book title"""
return self.normalized_data["title"] return self.normalized_data.get("title")
@property @property
def author(self): def author(self):
"""get the book's authors""" """get the book's authors"""
return self.normalized_data["authors"] return self.normalized_data.get("authors")
@property @property
def isbn(self): def isbn(self):
"""pulls out the isbn13 field from the csv line data""" """pulls out the isbn13 field from the csv line data"""
return unquote_string(self.normalized_data["isbn_13"]) or unquote_string( return unquote_string(self.normalized_data.get("isbn_13")) or unquote_string(
self.normalized_data["isbn_10"] self.normalized_data.get("isbn_10")
) )
@property @property
@ -153,13 +153,13 @@ class ImportItem(models.Model):
@property @property
def review(self): def review(self):
"""a user-written review, to be imported with the book data""" """a user-written review, to be imported with the book data"""
return self.normalized_data["review_body"] return self.normalized_data.get("review_body")
@property @property
def rating(self): def rating(self):
"""x/5 star rating for a book""" """x/5 star rating for a book"""
if self.normalized_data.get("rating"): if self.normalized_data.get("rating"):
return float(self.normalized_data["rating"]) return float(self.normalized_data.get("rating"))
return None return None
@property @property
@ -167,7 +167,7 @@ class ImportItem(models.Model):
"""when the book was added to this dataset""" """when the book was added to this dataset"""
if self.normalized_data.get("date_added"): if self.normalized_data.get("date_added"):
return timezone.make_aware( return timezone.make_aware(
dateutil.parser.parse(self.normalized_data["date_added"]) dateutil.parser.parse(self.normalized_data.get("date_added"))
) )
return None return None
@ -176,7 +176,7 @@ class ImportItem(models.Model):
"""when the book was started""" """when the book was started"""
if self.normalized_data.get("date_started"): if self.normalized_data.get("date_started"):
return timezone.make_aware( return timezone.make_aware(
dateutil.parser.parse(self.normalized_data["date_started"]) dateutil.parser.parse(self.normalized_data.get("date_started"))
) )
return None return None
@ -185,7 +185,7 @@ class ImportItem(models.Model):
"""the date a book was completed""" """the date a book was completed"""
if self.normalized_data.get("date_finished"): if self.normalized_data.get("date_finished"):
return timezone.make_aware( return timezone.make_aware(
dateutil.parser.parse(self.normalized_data["date_finished"]) dateutil.parser.parse(self.normalized_data.get("date_finished"))
) )
return None return None
@ -218,10 +218,10 @@ class ImportItem(models.Model):
def __repr__(self): def __repr__(self):
# pylint: disable=consider-using-f-string # pylint: disable=consider-using-f-string
return "<{!r} Item {!r}>".format(self.index, self.normalized_data["title"]) return "<{!r} Item {!r}>".format(self.index, self.normalized_data.get("title"))
def __str__(self): def __str__(self):
# pylint: disable=consider-using-f-string # pylint: disable=consider-using-f-string
return "{} by {}".format( return "{} by {}".format(
self.normalized_data["title"], self.normalized_data["authors"] self.normalized_data.get("title"), self.normalized_data.get("authors")
) )