diff options
author | Dan McGee <dan@archlinux.org> | 2011-06-15 15:50:14 -0500 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2011-06-15 15:50:14 -0500 |
commit | 4a9b6867a3a2786435316ab7deefa54257bb931d (patch) | |
tree | 3b0cfc08238dcf6b67dac16b8c35bc26b14b213a /main/models.py | |
parent | eb231660b017eab886ecd98828bf0295e36a1112 (diff) |
Refactor common select_related into manager method
For a Package object query, we almost always did .select_related('arch',
'repo). Refactor this into the manager as a 'normal()' method so we can
avoid sprinkling the same logic everywhere.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main/models.py')
-rw-r--r-- | main/models.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/main/models.py b/main/models.py index eae55c8b..677647ac 100644 --- a/main/models.py +++ b/main/models.py @@ -74,8 +74,10 @@ class TodolistManager(models.Manager): class PackageManager(models.Manager): def flagged(self): - return self.get_query_set().filter(flag_date__isnull=False) + return self.filter(flag_date__isnull=False) + def normal(self): + return self.select_related('arch', 'repo') class Donor(models.Model): name = models.CharField(max_length=255, unique=True) @@ -255,8 +257,7 @@ class Package(models.Model): deps = [] # TODO: we can use list comprehension and an 'in' query to make this more effective for dep in self.packagedepend_set.order_by('optional', 'depname'): - pkgs = Package.objects.select_related('arch', 'repo').filter( - pkgname=dep.depname) + pkgs = Package.objects.normal().filter(pkgname=dep.depname) if not self.arch.agnostic: # make sure we match architectures if possible pkgs = pkgs.filter(arch__in=self.applicable_arches()) @@ -320,7 +321,7 @@ class Package(models.Model): if self.repo.testing: return None try: - return Package.objects.get(repo__testing=True, + return Package.objects.normal().get(repo__testing=True, pkgname=self.pkgname, arch=self.arch) except Package.DoesNotExist: return None @@ -328,7 +329,7 @@ class Package(models.Model): def elsewhere(self): '''attempt to locate this package anywhere else, regardless of architecture or repository. Excludes this package from the list.''' - return Package.objects.select_related('arch', 'repo').filter( + return Package.objects.normal().filter( pkgname=self.pkgname).exclude(id=self.id).order_by( 'arch__name', 'repo__name') |