summaryrefslogtreecommitdiff
path: root/public/utils.py
diff options
context:
space:
mode:
authorParabola <dev@list.parabolagnulinux.org>2011-05-21 05:41:26 +0000
committerParabola <dev@list.parabolagnulinux.org>2011-05-21 05:41:26 +0000
commit62059d83aea71ac7bde8902b20221e52c86a810b (patch)
tree758bd028d43a30893ba6261f39b1ebdbebbbf6e3 /public/utils.py
parent081223981aa520f792757a1776588756a4107fd4 (diff)
parentfd9ecb6eb1c8ee56adfbb58640d7a98baa6cd62c (diff)
Merge branch 'master' of /srv/git/projects/parabolaweb
Diffstat (limited to 'public/utils.py')
-rw-r--r--public/utils.py80
1 files changed, 64 insertions, 16 deletions
diff --git a/public/utils.py b/public/utils.py
index 81f589f7..fd29a845 100644
--- a/public/utils.py
+++ b/public/utils.py
@@ -3,30 +3,78 @@ from operator import attrgetter
from main.models import Arch, Package
from main.utils import cache_function
+class RecentUpdate(object):
+ def __init__(self, packages):
+ if len(packages) == 0:
+ raise Exception
+ first = packages[0]
+ self.pkgbase = first.pkgbase
+ self.repo = first.repo
+ self.version = ''
+
+ packages = sorted(packages, key=attrgetter('arch', 'pkgname'))
+ # split the packages into two lists. we need to prefer packages
+ # matching pkgbase as our primary, and group everything else in other.
+ self.packages = [pkg for pkg in packages if pkg.pkgname == pkg.pkgbase]
+ self.others = [pkg for pkg in packages if pkg.pkgname != pkg.pkgbase]
+
+ if self.packages:
+ version = self.packages[0].full_version
+ if all(version == pkg.full_version for pkg in self.packages):
+ self.version = version
+ elif self.others:
+ version = self.others[0].full_version
+ if all(version == pkg.full_version for pkg in self.others):
+ self.version = version
+
+ def package_links(self):
+ '''Returns either actual packages or package-standins for virtual
+ pkgbase packages.'''
+ if self.packages:
+ # we have real packages- just yield each in sequence
+ for package in self.packages:
+ yield package
+ else:
+ # time to fake out the template, this is a tad dirty
+ arches = set(pkg.arch for pkg in self.others)
+ for arch in arches:
+ url = '/packages/%s/%s/%s/' % (
+ self.repo.name.lower(), arch.name, self.pkgbase)
+ package_stub = {
+ 'pkgname': self.pkgbase,
+ 'arch': arch,
+ 'repo': self.repo,
+ 'get_absolute_url': url
+ }
+ yield package_stub
+
@cache_function(300)
-def get_recent_updates():
+def get_recent_updates(number=15):
# This is a bit of magic. We are going to show 15 on the front page, but we
# want to try and eliminate cross-architecture wasted space. Pull enough
# packages that we can later do some screening and trim out the fat.
pkgs = []
+ # grab a few extra so we can hopefully catch everything we need
+ fetch = number * 6
for arch in Arch.objects.all():
- # grab a few extra so we can hopefully catch everything we need
pkgs += list(Package.objects.select_related(
- 'arch', 'repo').filter(arch=arch).order_by('-last_update')[:50])
+ 'arch', 'repo').filter(arch=arch).order_by('-last_update')[:fetch])
pkgs.sort(key=attrgetter('last_update'))
+
updates = []
- ctr = 0
- while ctr < 15 and len(pkgs) > 0:
- # not particularly happy with this logic, but it works.
- p = pkgs.pop()
- is_same = lambda q: p.is_same_version(q) and p.repo == q.repo
- samepkgs = filter(is_same, pkgs)
- samepkgs.append(p)
- samepkgs.sort(key=attrgetter('arch'))
- updates.append(samepkgs)
- for q in samepkgs:
- if p != q: pkgs.remove(q)
- ctr += 1
- return updates
+ while len(pkgs) > 0:
+ pkg = pkgs.pop()
+
+ in_group = lambda x: pkg.repo == x.repo and pkg.pkgbase == x.pkgbase
+ samepkgs = [other for other in pkgs if in_group(other)]
+ samepkgs.append(pkg)
+
+ # now remove all the packages we just pulled out
+ pkgs = [other for other in pkgs if other not in samepkgs]
+
+ update = RecentUpdate(samepkgs)
+ updates.append(update)
+
+ return updates[:number]
# vim: set ts=4 sw=4 et: