From patchwork Mon Oct 3 12:26:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygmunt Krynicki X-Patchwork-Id: 4496 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id DCBDF23DEC for ; Mon, 3 Oct 2011 12:26:17 +0000 (UTC) Received: from mail-yw0-f52.google.com (mail-yw0-f52.google.com [209.85.213.52]) by fiordland.canonical.com (Postfix) with ESMTP id 98DD8A18097 for ; Mon, 3 Oct 2011 12:26:17 +0000 (UTC) Received: by ywp31 with SMTP id 31so5260843ywp.11 for ; Mon, 03 Oct 2011 05:26:17 -0700 (PDT) Received: by 10.223.94.134 with SMTP id z6mr5335887fam.8.1317644776655; Mon, 03 Oct 2011 05:26:16 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.152.23.170 with SMTP id n10cs12937laf; Mon, 3 Oct 2011 05:26:16 -0700 (PDT) Received: by 10.213.26.208 with SMTP id f16mr25966ebc.147.1317644774781; Mon, 03 Oct 2011 05:26:14 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com. [91.189.90.7]) by mx.google.com with ESMTPS id w20si10358355wec.122.2011.10.03.05.26.14 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 03 Oct 2011 05:26:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1RAhb4-00088f-6B for ; Mon, 03 Oct 2011 12:26:14 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id 20389E208B for ; Mon, 3 Oct 2011 12:26:14 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: lava-server X-Launchpad-Branch: ~linaro-validation/lava-server/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 245 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~linaro-validation/lava-server/trunk] Rev 245: Add bread crumb helpers from lava-dashboard Message-Id: <20111003122614.15457.12282.launchpad@ackee.canonical.com> Date: Mon, 03 Oct 2011 12:26:14 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="14071"; Instance="launchpad-lazr.conf" X-Launchpad-Hash: be31d05b213270282abb99e950c4b08cf815c323 ------------------------------------------------------------ revno: 245 committer: Zygmunt Krynicki branch nick: trunk timestamp: Fri 2011-09-30 15:34:25 +0200 message: Add bread crumb helpers from lava-dashboard added: lava_server/bread_crumbs.py --- lp:lava-server https://code.launchpad.net/~linaro-validation/lava-server/trunk You are subscribed to branch lp:lava-server. To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-server/trunk/+edit-subscription === added file 'lava_server/bread_crumbs.py' --- lava_server/bread_crumbs.py 1970-01-01 00:00:00 +0000 +++ lava_server/bread_crumbs.py 2011-09-30 13:34:25 +0000 @@ -0,0 +1,90 @@ +# Copyright (C) 2010, 2011 Linaro Limited +# +# Author: Zygmunt Krynicki +# +# This file is part of LAVA Server. +# +# LAVA Server is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License version 3 +# as published by the Free Software Foundation +# +# LAVA Server is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with LAVA Server. If not, see . + + +""" +Bread crubm management for LAVA server +""" + +from django.core.urlresolvers import reverse +import logging + + +class BreadCrumb(object): + + def __init__(self, name, parent=None, needs=None): + self.name = name + self.view = None + self.parent = parent + self.needs = needs or [] + + def __repr__(self): + return "" % ( + self.name, self.view, self.parent) + + def __call__(self, view): + self.view = view + view._bread_crumb = self + return view + + def get_name(self, kwargs): + try: + return self.name.format(**kwargs) + except: + logging.exception("Unable to construct breadcrumb name for view %r", self.view) + raise + + def get_absolute_url(self, kwargs): + try: + return reverse(self.view, args=[kwargs[name] for name in self.needs]) + except: + logging.exception("Unable to construct breadcrumb URL for view %r", self.view) + raise + + +class LiveBreadCrumb(object): + + def __init__(self, bread_crumb, kwargs): + self.bread_crumb = bread_crumb + self.kwargs = kwargs + + def get_name(self): + return self.bread_crumb.get_name(self.kwargs) + + def get_absolute_url(self): + return self.bread_crumb.get_absolute_url(self.kwargs) + + +class BreadCrumbTrail(object): + + def __init__(self, bread_crumb_list, kwargs): + self.bread_crumb_list = bread_crumb_list + self.kwargs = kwargs + + def __iter__(self): + for bread_crumb in self.bread_crumb_list: + yield LiveBreadCrumb(bread_crumb, self.kwargs) + + @classmethod + def leading_to(cls, view, **kwargs): + lst = [] + while view is not None: + lst.append(view._bread_crumb) + view = view._bread_crumb.parent + lst.reverse() + return cls(lst, kwargs or {})