From fc3ab08cb5663f9b3189b7e04bfd73e7ba2c3e92 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Tue, 7 Dec 2010 14:12:03 -0600 Subject: Slim down required by listings If we have testing/non-testing packages in this list, and we are looking at a package that is in both testing and non-testing, we can show only the packages that correspond with the relevant repo. I'm not sure any explanation will make this easier to understand, but the end result is we don't show a bunch of duplicates where we used to. Signed-off-by: Dan McGee --- main/models.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/main/models.py b/main/models.py index ec2416f7..b5c395a4 100644 --- a/main/models.py +++ b/main/models.py @@ -5,6 +5,9 @@ from django.contrib.sites.models import Site from main.utils import cache_function from packages.models import PackageRelation +from itertools import groupby +from operator import attrgetter + class UserProfile(models.Model): id = models.AutoField(primary_key=True) # not technically needed notify = models.BooleanField( @@ -166,19 +169,43 @@ class Package(models.Model): @cache_function(300) def get_requiredby(self): """ - Returns a list of package objects. + Returns a list of package objects. An attempt will be made to keep this + list slim by including the corresponding package in the same testing + category as this package if that check makes sense. """ requiredby = Package.objects.select_related('arch', 'repo').filter( packagedepend__depname=self.pkgname, - arch__in=self.applicable_arches()).distinct() - return requiredby.order_by('pkgname') + arch__in=self.applicable_arches() + ).distinct().order_by('pkgname') + + # find another package by this name in the opposite testing setup + if not Package.objects.filter(pkgname=self.pkgname, + arch=self.arch).exclude(id=self.id, + repo__testing=self.repo.testing).exists(): + # there isn't one? short circuit, all required by entries are fine + return requiredby + + trimmed = [] + # for each unique package name, try to screen our package list down to + # those packages in the same testing category (yes or no) iff there is + # a package in the same testing category. + for name, pkgs in groupby(requiredby, attrgetter('pkgname')): + pkgs = list(pkgs) + pkg = pkgs[0] + if len(pkgs) > 1: + pkgs = [p for p in pkgs if p.repo.testing == self.repo.testing] + if len(pkgs) > 0: + pkg = pkgs[0] + trimmed.append(pkg) + return trimmed @cache_function(300) def get_depends(self): """ - Returns a list of dicts. Each dict contains ('pkg' and 'dep'). - If it represents a found package both vars will be available; - else pkg will be None if it is a 'virtual' dependency. + Returns a list of dicts. Each dict contains ('pkg' and 'dep'). If it + represents a found package both vars will be available; else pkg will + be None if it is a 'virtual' dependency. Packages will match the + testing status of this package if possible. """ deps = [] # TODO: we can use list comprehension and an 'in' query to make this more effective -- cgit v1.2.3-55-g3dc8