reduced number of queries needed when displaying when a book is already on a user wishlist

pull/1/head
Ed Summers 2011-10-10 18:08:53 -04:00
parent b991c1d375
commit 5e9439b965
2 changed files with 8 additions and 12 deletions

View File

@ -97,12 +97,6 @@ class Work(models.Model):
title = models.CharField(max_length=1000) title = models.CharField(max_length=1000)
openlibrary_id = models.CharField(max_length=50, null=True) openlibrary_id = models.CharField(max_length=50, null=True)
@classmethod
def get_by_isbn(klass, isbn):
for w in Work.objects.filter(Q(editions__isbn_10=isbn) | Q(editions__isbn_13=isbn)):
return w
return None
def cover_image_small(self): def cover_image_small(self):
server_id = random.randint(0, 9) server_id = random.randint(0, 9)
gb_id = self.editions.all()[0].googlebooks_id gb_id = self.editions.all()[0].googlebooks_id

View File

@ -50,14 +50,16 @@ def search(request):
q = request.GET.get('q', None) q = request.GET.get('q', None)
results = gluejar_search(q) results = gluejar_search(q)
# flag search result as on wishlist # flag search result as on wishlist as appropriate
# TODO: make this better and faster
if not request.user.is_anonymous(): if not request.user.is_anonymous():
# get a list of all the googlebooks_ids for works on the user's wishlist
wishlist = request.user.wishlist
editions = models.Edition.objects.filter(work__wishlists__in=[wishlist])
googlebooks_ids = [e['googlebooks_id'] for e in editions.values('googlebooks_id')]
# if the results is on their wishlist flag it
for result in results: for result in results:
if not result.has_key('isbn_10'): if result['googlebooks_id'] in googlebooks_ids:
continue
work = models.Work.get_by_isbn(result['isbn_10'])
if work and work in request.user.wishlist.works.all():
result['on_wishlist'] = True result['on_wishlist'] = True
else: else:
result['on_wishlist'] = False result['on_wishlist'] = False