diff --git a/core/models.py b/core/models.py index f4686ad6..ea69fb83 100755 --- a/core/models.py +++ b/core/models.py @@ -619,10 +619,11 @@ class Work(models.Model): @property def googlebooks_id(self): preferred_id=self.preferred_edition.googlebooks_id + # note that there's always a preferred edition if preferred_id: return preferred_id try: - return self.identifiers.values('type', 'value').filter(type='goog')[0]['value'] + return self.identifiers.filter(type='goog')[0].value except IndexError: return '' @@ -639,7 +640,7 @@ class Work(models.Model): if preferred_id: return preferred_id try: - return self.identifiers.values('type', 'value').filter(type='gdrd')[0]['value'] + return self.identifiers.filter(type='gdrd')[0].value except IndexError: return '' @@ -650,7 +651,7 @@ class Work(models.Model): @property def librarything_id(self): try: - return self.identifiers.values('type', 'value').filter(type='ltwk')[0]['value'] + return self.identifiers.filter(type='ltwk')[0].value except IndexError: return '' @@ -661,7 +662,7 @@ class Work(models.Model): @property def openlibrary_id(self): try: - return self.identifiers.values('type', 'value').filter(type='olwk')[0]['value'] + return self.identifiers.filter(type='olwk')[0].value except IndexError: return '' diff --git a/core/tests.py b/core/tests.py index 6878cefa..086c13c0 100755 --- a/core/tests.py +++ b/core/tests.py @@ -52,6 +52,8 @@ class BookLoaderTests(TestCase): # work self.assertTrue(edition.work) + self.assertEqual(edition.work.googlebooks_id, 'IDFfMPW32hQC') + self.assertEqual(edition.work.first_isbn_13(), '9780441007462') # publisher names old_pub_name = edition.publisher_name diff --git a/experimental/test_id_speed.py b/experimental/test_id_speed.py new file mode 100644 index 00000000..e671bb64 --- /dev/null +++ b/experimental/test_id_speed.py @@ -0,0 +1,57 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth.models import User +from datetime import timedelta +import datetime +from regluit.core import models + +num=0 +start = datetime.datetime.now() +for work in models.Work.objects.all(): + try: + id = work.identifiers.values('type', 'value').filter(type='goog')[0]['value'] + except IndexError: + pass + num += 1 + if num == 10000: + break +end = datetime.datetime.now() +print "2 values "+str(end - start) + +num=0 +start = datetime.datetime.now() +for work in models.Work.objects.all(): + try: + id = work.identifiers.filter(type='goog')[0].value + except IndexError: + pass + num += 1 + if num == 10000: + break +end = datetime.datetime.now() +print "orig "+str(end - start) + +num=0 +start = datetime.datetime.now() +for work in models.Work.objects.all(): + try: + id = work.identifiers.values('value').filter(type='goog')[0]['value'] + except IndexError: + pass + num += 1 + if num == 10000: + break +end = datetime.datetime.now() +print "1 value "+ str(end - start) + +num=0 +start = datetime.datetime.now() +for work in models.Work.objects.all(): + try: + id = work.identifiers.values('type', 'value').filter(type='goog')[0]['value'] + except IndexError: + pass + num += 1 + if num == 10000: + break +end = datetime.datetime.now() +print "2 values "+str(end - start)