tried to speed up some queries

pull/1/head
Ed Summers 2011-11-17 22:11:40 -05:00
parent 9516b4b174
commit 267d3c5aef
1 changed files with 20 additions and 16 deletions

View File

@ -162,6 +162,10 @@ 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)
def __init__(self, *args, **kwargs):
self._last_campaign = None
super(Work, self).__init__(*args, **kwargs)
def cover_image_small(self): def cover_image_small(self):
return self.editions.all()[0].cover_image_small() return self.editions.all()[0].cover_image_small()
@ -169,21 +173,22 @@ class Work(models.Model):
return self.editions.all()[0].cover_image_thumbnail() return self.editions.all()[0].cover_image_thumbnail()
def author(self): def author(self):
authorlist = self.editions.all()[0].authors.all() authors = list(Author.objects.filter(editions__work=self).all())
if authorlist.count() == 1: if len(authors) == 1:
myauthor = authorlist[0].name return authors[0].name
elif authorlist.count() > 1: elif len(authors) > 1:
myauthor = authorlist[0].name + ' et al.' return authors[0].name + ' et al.'
else: return ''
myauthor = ''
return myauthor
def last_campaign(self): def last_campaign(self):
# stash away the last campaign to prevent repeated lookups
if hasattr(self, '_last_campaign'):
return self._last_campaign
try: try:
last = self.campaigns.order_by('-created')[0] self._last_campaign = self.campaigns.order_by('-created')[0]
except: except IndexError:
last = None pass
return last return self._last_campaign
def last_campaign_status(self): def last_campaign_status(self):
campaign = self.last_campaign() campaign = self.last_campaign()
@ -236,9 +241,8 @@ class Work(models.Model):
return self.first_ebook('epub') return self.first_ebook('epub')
def first_ebook(self, ebook_format=None): def first_ebook(self, ebook_format=None):
for edition in self.editions.all(): for ebook in Ebook.objects.filter(edition__work=self,
for ebook in edition.ebooks.all(): format=ebook_format):
if ebook_format == None or ebook.format == ebook_format:
return ebook return ebook
return None return None