From 3fc1f46897e4ff2d0bc3fcd2cfa7005359cbaed6 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 30 Oct 2020 12:43:02 -0700 Subject: [PATCH] Handle dashes in isbns --- bookwyrm/models/book.py | 3 +++ bookwyrm/tests/models/test_book_model.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/bookwyrm/models/book.py b/bookwyrm/models/book.py index e32846fe..c8b18f8e 100644 --- a/bookwyrm/models/book.py +++ b/bookwyrm/models/book.py @@ -186,6 +186,7 @@ class Edition(Book): def isbn_10_to_13(isbn_10): ''' convert an isbn 10 into an isbn 13 ''' + isbn_10 = isbn_10.replace('-', '') # drop the last character of the isbn 10 number (the original checkdigit) converted = isbn_10[:9] # add "978" to the front @@ -206,6 +207,8 @@ def isbn_13_to_10(isbn_13): if isbn_13[:3] != '978': return None + isbn_13 = isbn_13.replace('-', '') + # remove '978' and old checkdigit converted = isbn_13[3:-1] # calculate checkdigit diff --git a/bookwyrm/tests/models/test_book_model.py b/bookwyrm/tests/models/test_book_model.py index 78567a92..e8211a8f 100644 --- a/bookwyrm/tests/models/test_book_model.py +++ b/bookwyrm/tests/models/test_book_model.py @@ -54,11 +54,21 @@ class Book(TestCase): isbn_13 = isbn_10_to_13(isbn_10) self.assertEqual(isbn_13, '9781788161671') + isbn_10 = '1-788-16167-X' + isbn_13 = isbn_10_to_13(isbn_10) + self.assertEqual(isbn_13, '9781788161671') + + + def test_isbn_13_to_10(self): isbn_13 = '9781788161671' isbn_10 = isbn_13_to_10(isbn_13) self.assertEqual(isbn_10, '178816167X') + isbn_13 = '978-1788-16167-1' + isbn_10 = isbn_13_to_10(isbn_13) + self.assertEqual(isbn_10, '178816167X') + class Shelf(TestCase): def setUp(self):