From 22bcf20c7cbec53621e0d94934d5e07a14629222 Mon Sep 17 00:00:00 2001 From: volpino Date: Sat, 5 May 2012 10:54:04 +0200 Subject: [PATCH] select_related prefetch refactored --- euscanwww/djeuscan/api/handlers.py | 12 ++++++++--- euscanwww/djeuscan/helpers.py | 8 ++++++++ euscanwww/djeuscan/managers.py | 32 ++++++++++-------------------- euscanwww/djeuscan/views.py | 8 ++++---- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/euscanwww/djeuscan/api/handlers.py b/euscanwww/djeuscan/api/handlers.py index 67fe00c..7c7cb82 100644 --- a/euscanwww/djeuscan/api/handlers.py +++ b/euscanwww/djeuscan/api/handlers.py @@ -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 diff --git a/euscanwww/djeuscan/helpers.py b/euscanwww/djeuscan/helpers.py index 0b285f9..fb93a94 100644 --- a/euscanwww/djeuscan/helpers.py +++ b/euscanwww/djeuscan/helpers.py @@ -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: diff --git a/euscanwww/djeuscan/managers.py b/euscanwww/djeuscan/managers.py index 7cbb77c..91d5f44 100644 --- a/euscanwww/djeuscan/managers.py +++ b/euscanwww/djeuscan/managers.py @@ -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 diff --git a/euscanwww/djeuscan/views.py b/euscanwww/djeuscan/views.py index 1f30ecd..346adb2 100644 --- a/euscanwww/djeuscan/views.py +++ b/euscanwww/djeuscan/views.py @@ -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}