diff options
author | Dan McGee <dan@archlinux.org> | 2010-01-31 15:19:12 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2010-01-31 15:19:12 -0600 |
commit | f58b354a037d2c46fd8db9320f8ba29c9e014436 (patch) | |
tree | 05aaea7bcd1a055af9d873482a40b7ecdc44a082 | |
parent | e0eed01914752ee98d50aeab748f71b24df94dc6 (diff) |
Use select_related() to make a few more places more performant
Especially when looking at packages, we always want the arch and repo.
Another big hunk of changes deals with the very inefficient signoffs code.
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r-- | feeds.py | 4 | ||||
-rw-r--r-- | main/models.py | 14 | ||||
-rw-r--r-- | packages/views.py | 15 | ||||
-rw-r--r-- | public/views.py | 2 | ||||
-rw-r--r-- | todolists/views.py | 2 |
5 files changed, 24 insertions, 13 deletions
@@ -9,7 +9,7 @@ class PackageFeed(Feed): description = 'Recent Package Updates' def items(self): - return Package.objects.order_by('-last_update')[:24] + return Package.objects.select_related('arch', 'repo').order_by('-last_update')[:24] def item_pubdate(self, item): return item.last_update @@ -23,7 +23,7 @@ class NewsFeed(Feed): description = 'Recent News Updates' def items(self): - return News.objects.order_by('-postdate', '-id')[:10] + return News.objects.select_related('author').order_by('-postdate', '-id')[:10] def item_pubdate(self, item): d = item.postdate diff --git a/main/models.py b/main/models.py index 80033546..9e572cee 100644 --- a/main/models.py +++ b/main/models.py @@ -202,13 +202,18 @@ class Package(models.Model): @property def signoffs(self): - return Signoff.objects.filter( + if 'signoffs_cache' in dir(self): + if len(self.signoffs_cache) > 0: + print self.signoffs_cache + return self.signoffs_cache + self.signoffs_cache = list(Signoff.objects.filter( pkg=self, pkgver=self.pkgver, - pkgrel=self.pkgrel) + pkgrel=self.pkgrel)) + return self.signoffs_cache def approved_for_signoff(self): - return self.signoffs.count() >= 2 + return len(self.signoffs) >= 2 def get_requiredby(self): """ @@ -234,9 +239,10 @@ class Package(models.Model): if 'deps_cache' in dir(self): return self.deps_cache deps = [] + # TODO: we can use list comprehension and an 'in' query to make this more effective for dep in self.packagedepend_set.order_by('depname'): # we only need depend on same-arch-packages - pkgs = Package.objects.filter( + pkgs = Package.objects.select_related('arch', 'repo').filter( Q(arch__name__iexact='any') | Q(arch=self.arch), pkgname=dep.depname) if len(pkgs) == 0: diff --git a/packages/views.py b/packages/views.py index aa66271f..28d458a0 100644 --- a/packages/views.py +++ b/packages/views.py @@ -190,13 +190,18 @@ def unflag(request, pkgid): @permission_required('main.change_package') def signoffs(request): - packages = Package.objects.filter(repo__name="Testing").order_by("pkgname") + packages = Package.objects.select_related('arch', 'repo', 'signoffs').filter(repo__name__endswith="Testing").order_by("pkgname") package_list = [] - other_packages = Package.objects.exclude(repo__name="Testing") + + q_pkgname = Package.objects.filter(repo__name__endswith="Testing").values('pkgname').distinct().query + package_repos = Package.objects.values('pkgname', 'repo__name').exclude(repo__name__endswith="Testing").filter(pkgname__in=q_pkgname) + pkgtorepo = dict() + for pr in package_repos: + pkgtorepo[pr['pkgname']] = pr['repo__name'] + for package in packages: - other_package = other_packages.filter(pkgname=package.pkgname) - if len(other_package): - repo = other_package[0].repo.name + if package.pkgname in pkgtorepo: + repo = pkgtorepo[package.pkgname] else: repo = "Unknown" package_list.append((package, repo)) diff --git a/public/views.py b/public/views.py index b44b02ab..7f703c4a 100644 --- a/public/views.py +++ b/public/views.py @@ -10,7 +10,7 @@ from django.views.generic import list_detail def index(request): context = { 'news_updates': News.objects.order_by('-postdate', '-id')[:10], - 'pkg_updates': Package.objects.order_by('-last_update')[:15], + 'pkg_updates': Package.objects.select_related('arch', 'repo').order_by('-last_update')[:15], 'repos': Repo.objects.all() } return render_to_response('public/index.html', context, diff --git a/todolists/views.py b/todolists/views.py index 00236c62..9b64c25c 100644 --- a/todolists/views.py +++ b/todolists/views.py @@ -44,7 +44,7 @@ def view(request, listid): @login_required def list(request): - lists = Todolist.objects.order_by('-date_added') + lists = Todolist.objects.select_related('creator').order_by('-date_added') for l in lists: l.complete = TodolistPkg.objects.filter( list=l.id,complete=False).count() == 0 |