make sure there are some hits when adding a book by isbn

pull/1/head
Ed Summers 2011-10-10 17:26:38 -04:00
parent b4583507d6
commit 1b54ba5f6b
4 changed files with 9 additions and 5 deletions

View File

@ -13,7 +13,7 @@ def add_by_isbn(isbn):
url = "https://www.googleapis.com/books/v1/volumes"
results = _get_json(url, {"q": "isbn:%s" % isbn})
if len(results['items']) == 0:
if not results.has_key('items') or len(results['items']) == 0:
logger.warn("no google hits for %s" % isbn)
return None

View File

@ -9,8 +9,8 @@ class Command(BaseCommand):
def handle(self, filename, **options):
for isbn in open(filename):
isbn = isbn.strip()
edition = bookloader.add_book(isbn)
edition = bookloader.add_by_isbn(isbn)
if edition:
print edition
print "loaded %s as %s" % (isbn, edition)
else:
print "failed to load book for %s" % isbn

View File

@ -142,7 +142,7 @@ class Edition(models.Model):
work = models.ForeignKey("Work", related_name="editions", null=True)
def __unicode__(self):
return self.title
return "%s (%s)" % (self.title, self.isbn_13)
@classmethod
def get_by_isbn(klass, isbn):

View File

@ -41,7 +41,10 @@ class TestBooks(TestCase):
self.assertEqual(models.Author.objects.all().count(), 1)
self.assertEqual(models.Work.objects.all().count(), 1)
self.assertEqual(models.Subject.objects.all().count(), 11)
def test_missing_isbn(self):
e = bookloader.add_by_isbn('0139391401')
self.assertEqual(e, None)
class SearchTests(TestCase):
@ -62,6 +65,7 @@ class SearchTests(TestCase):
response = search.googlebooks_search('melville')
self.assertEqual(len(response['items']), 10)
class CampaignTests(TestCase):
def test_required_fields(self):