From 681dba9eef7991c5113d35562b3ace55bb782ad4 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 7 Dec 2010 13:31:59 -0600 Subject: Move some common package code to a function We do this determination of 'applicable arches' a few times, so move it to a method on the package object and also clean things up so items aren't duplicated in the list. Signed-off-by: Dan McGee --- main/models.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'main') diff --git a/main/models.py b/main/models.py index 51cf89a8..af4466ff 100644 --- a/main/models.py +++ b/main/models.py @@ -156,16 +156,21 @@ def signoffs(self): def approved_for_signoff(self): return len(self.signoffs) >= 2 + @cache_function(300) + def applicable_arches(self): + '''The list of (this arch) + (available agnostic arches).''' + arches = set(Arch.objects.filter(agnostic=True)) + arches.add(self.arch) + return list(arches) + @cache_function(300) def get_requiredby(self): """ Returns a list of package objects. """ - arches = list(Arch.objects.filter(agnostic=True)) - arches.append(self.arch) requiredby = Package.objects.select_related('arch', 'repo').filter( packagedepend__depname=self.pkgname, - arch__in=arches).distinct() + arch__in=self.applicable_arches()).distinct() return requiredby.order_by('pkgname') @cache_function(300) @@ -176,15 +181,13 @@ def get_depends(self): else pkg will be None if it is a 'virtual' dependency. """ deps = [] - arches = list(Arch.objects.filter(agnostic=True)) - arches.append(self.arch) # TODO: we can use list comprehension and an 'in' query to make this more effective for dep in self.packagedepend_set.order_by('depname'): pkgs = Package.objects.select_related('arch', 'repo').filter( pkgname=dep.depname) if not self.arch.agnostic: # make sure we match architectures if possible - pkgs = pkgs.filter(arch__in=arches) + pkgs = pkgs.filter(arch__in=self.applicable_arches()) if len(pkgs) == 0: # couldn't find a package in the DB # it should be a virtual depend (or a removed package) @@ -231,7 +234,7 @@ def split_packages(self): repo.testing flag. For any non-split packages, the return value will be an empty list. """ - return Package.objects.filter(arch=self.arch, + return Package.objects.filter(arch__in=self.applicable_arches, repo__testing=self.repo.testing, pkgbase=self.pkgbase).exclude(id=self.id) def get_svn_link(self, svnpath): -- cgit v1.2.3-54-g00ecf