select_related prefetch refactored

This commit is contained in:
volpino 2012-05-05 10:54:04 +02:00
parent 5c0dbe11f2
commit 22bcf20c7c
4 changed files with 31 additions and 29 deletions

View File

@ -88,15 +88,21 @@ class PackagesHandler(AnonymousBaseHandler):
data = {}
if 'category' in kwargs:
packages = Package.objects.for_category(kwargs['category'])
packages = Package.objects.for_category(
kwargs['category'],
last_versions=True
)
data = {'category': kwargs['category']}
elif 'herd' in kwargs:
herd = Herd.objects.get(herd=kwargs['herd'])
packages = Package.objects.for_herd(herd)
packages = Package.objects.for_herd(herd, last_versions=True)
data = {'herd': herd}
elif 'maintainer_id' in kwargs:
maintainer = Maintainer.objects.get(id=kwargs['maintainer_id'])
packages = Package.objects.for_maintainer(maintainer)
packages = Package.objects.for_maintainer(
maintainer,
last_versions=True
)
data = {'maintainer': maintainer}
data['packages'] = packages

View File

@ -15,6 +15,14 @@ def xint(i):
return 0
def select_related_last_versions(queryset):
queryset = queryset.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
def version_key(version):
version = version.version
try:

View File

@ -3,7 +3,7 @@ djeuscan.managers
"""
from django.db import models
from djeuscan.helpers import xint, rename_fields
from djeuscan.helpers import xint, rename_fields, select_related_last_versions
class PackageMixin(object):
@ -90,48 +90,36 @@ class PackageMixin(object):
)
return packages.filter(version__overlay=overlay).distinct()
def for_maintainer(self, maintainer):
def for_maintainer(self, maintainer, last_versions=False):
"""
Returns packages that belong to the given maintainer
"""
res = self.filter(maintainers__id=maintainer.id)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
if last_versions:
select_related_last_versions(res)
return res
def for_herd(self, herd):
def for_herd(self, herd, last_versions=False):
"""
Returns packages that belong to the given herd
"""
res = self.filter(herds__id=herd.id)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
if last_versions:
select_related_last_versions(res)
return res
def for_category(self, category):
def for_category(self, category, last_versions=False):
"""
Returns packages that belong to the given category
"""
res = self.filter(category=category)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
if last_versions:
select_related_last_versions(res)
return res

View File

@ -43,7 +43,7 @@ def categories(request):
@render_to('euscan/category.html')
def category(request, category):
packages = Package.objects.for_category(category)
packages = Package.objects.for_category(category, last_versions=True)
if not packages:
raise Http404
@ -59,7 +59,7 @@ def herds(request):
@render_to('euscan/herd.html')
def herd(request, herd):
herd = get_object_or_404(Herd, herd=herd)
packages = Package.for_herd(herd)
packages = Package.for_herd(herd, last_versions=True)
return {'herd': herd, 'packages': packages}
@ -72,7 +72,7 @@ def maintainers(request):
@render_to('euscan/maintainer.html')
def maintainer(request, maintainer_id):
maintainer = get_object_or_404(Maintainer, pk=maintainer_id)
packages = Package.objects.for_maintainer(maintainer)
packages = Package.objects.for_maintainer(maintainer, last_versions=True)
return {'maintainer': maintainer, 'packages': packages}
@ -84,7 +84,7 @@ def overlays(request):
@render_to('euscan/overlay.html')
def overlay(request, overlay):
packages = Package.objects.for_overlay(overlay)
packages = Package.objects.for_overlay(overlay, last_versions=True)
if not packages:
raise Http404
return {'overlay': overlay, 'packages': packages}