summaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-06-29 12:05:07 -0500
committerDan McGee <dan@archlinux.org>2011-07-05 09:48:10 -0500
commitc5308b75837dfcbb851bcf9e93553c72e4b8996e (patch)
tree6becd17d67abe0f5a9d8f05e365c53b9986f9250 /main
parentf3262790b37da4883d74095ce34a84951432399b (diff)
downloadarchweb-c5308b75837dfcbb851bcf9e93553c72e4b8996e.tar.gz
archweb-c5308b75837dfcbb851bcf9e93553c72e4b8996e.zip
Recent updates refactor
Pull out a few helpful objects and functions for use later elsewhere. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main')
-rw-r--r--main/utils.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/main/utils.py b/main/utils.py
index 2ca69bcb..81f689e7 100644
--- a/main/utils.py
+++ b/main/utils.py
@@ -91,4 +91,39 @@ def set_created_field(sender, **kwargs):
if hasattr(obj, 'created') and not obj.created:
obj.created = datetime.utcnow()
+def groupby_preserve_order(iterable, keyfunc):
+ '''Take an iterable and regroup using keyfunc to determine whether items
+ belong to the same group. The order of the iterable is preserved and
+ similar keys do not have to be consecutive. This means the earliest
+ occurrence of a given key will determine the order of the lists in the
+ returned list.'''
+ seen_keys = {}
+ result = []
+ for item in iterable:
+ key = keyfunc(item)
+
+ group = seen_keys.get(key, None)
+ if group is None:
+ group = []
+ seen_keys[key] = group
+ result.append(group)
+
+ group.append(item)
+
+ return result
+
+class PackageStandin(object):
+ '''Resembles a Package object, and has a few of the same fields, but is
+ really a link to a pkgbase that has no package with matching pkgname.'''
+ def __init__(self, package):
+ self.package = package
+ self.pkgname = package.pkgbase
+
+ def __getattr__(self, name):
+ return getattr(self.package, name)
+
+ def get_absolute_url(self):
+ return '/packages/%s/%s/%s/' % (
+ self.repo.name.lower(), self.arch.name, self.pkgbase)
+
# vim: set ts=4 sw=4 et: