eagerly fetch the version to save 1 query per button

This commit is contained in:
Jeff Balogh 2010-03-24 16:41:25 -07:00
Родитель 9b682e4911
Коммит 99fc0da592
2 изменённых файлов: 7 добавлений и 7 удалений

Просмотреть файл

@ -94,7 +94,7 @@ class InstallButton(object):
def links(self):
rv = []
for file in self.version.files.all():
for file in self.version.files.select_related('version'):
text, url, os = self.file_details(file)
rv.append(Link(text, self.fix_link(url), os, file))
return rv

Просмотреть файл

@ -25,9 +25,10 @@ class ButtonTest(object):
self.addon.current_version = v
self.file = self.get_file(amo.PLATFORM_ALL)
v.files.all.return_value = [self.file]
v.files.select_related.return_value = [self.file]
self.platforms = amo.PLATFORM_MAC, amo.PLATFORM_LINUX
self.platform_files = map(self.get_file, self.platforms)
self.request = test_utils.RequestFactory().get('/')
# Make GET mutable.
@ -272,12 +273,11 @@ class TestButton(ButtonTest):
eq_(b.fix_link('foo.com'), 'foo.com?src=src&collection=xxx')
def test_links(self):
platforms = self.platforms
self.version.files.all.return_value = map(self.get_file, platforms)
self.version.files.select_related.return_value = self.platform_files
links = self.get_button().links()
eq_(len(links), len(platforms))
eq_([x.os for x in links], list(platforms))
eq_(len(links), len(self.platforms))
eq_([x.os for x in links], list(self.platforms))
class TestButtonHtml(ButtonTest):
@ -325,7 +325,7 @@ class TestButtonHtml(ButtonTest):
eq_('xpi.latest', button.attr('data-realurl'))
def test_multi_platform(self):
self.version.files.all.return_value = map(self.get_file, self.platforms)
self.version.files.select_related.return_value = self.platform_files
names = [p.name for p in self.platforms]
doc = self.render()
eq_(doc('.button').length, 2)