source: main/waeup.ikoba/trunk/src/waeup/ikoba/browser/breadcrumbs.py @ 12774

Last change on this file since 12774 was 12761, checked in by Henrik Bettermann, 10 years ago

Add browser module in payments. We need this module for payments management.

Move breadcrumbs PaymentBreadcrumb? into this module.

  • Property svn:keywords set to Id
File size: 6.2 KB
Line 
1## $Id: breadcrumbs.py 12761 2015-03-14 06:17:25Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18""" Components to get breadcrumbs for any object.
19"""
20import grok
21from grokcore.view.interfaces import IGrokView
22from zope.component import getAdapter
23
24from waeup.ikoba.interfaces import IConfigurationContainer
25from waeup.ikoba.interfaces import MessageFactory as _
26from waeup.ikoba.browser.interfaces import (
27    IBreadcrumb, IBreadcrumbIgnorable, IBreadcrumbContainer, IIkobaObject,
28    ICompany, IUsersContainer, IDataCenter,
29    )
30from waeup.ikoba.reports import IReportsContainer
31
32
33class Breadcrumb(grok.Adapter):
34    """A most general breadcrumb generator.
35    """
36    grok.provides(IBreadcrumb)
37    grok.context(IIkobaObject)
38    grok.name('index')
39
40    _title = None
41    _parent = 0
42    _request = None
43    parent_viewname = 'index'
44    viewname = 'index'
45
46    def __init__(self, context):
47        """Turn a context into a breadcrumb.
48        """
49        self.context = context
50
51    @property
52    def title(self):
53        """Get a title for a context.
54        """
55        if self._title is not None:
56            return self._title
57        if hasattr(self.context, 'title'):
58            return self.context.title
59        if hasattr(self.context, 'name'):
60            return self.context.name
61        return None
62
63    @property
64    def parent(self):
65        """Get the contexts parent object and viewname or None.
66        """
67        if self._parent is None:
68            return None
69        if self._parent is not 0:
70            return (self._parent, self.parent_viewname)
71
72        if self.viewname != 'index':
73            self._parent = self.context
74        else:
75            site = grok.getSite()
76            if self.context is not site:
77                self._parent = getattr(self.context, '__parent__', None)
78
79        if self._parent is not None:
80            return (self._parent, self.parent_viewname)
81        return None
82
83    @property
84    def target(self):
85        return self.viewname
86
87
88class CompanyBreadcrumb(Breadcrumb):
89    """A breadcrumb for company index pages.
90    """
91    grok.context(ICompany)
92    title = _(u'Home')
93    parent = None
94
95
96class PortalSettingsBreadcrumb(Breadcrumb):
97    """A breadcrumb for the manage view of universities.
98
99    Here we need a special `parent()` implementation, because the
100    parent object is not a real parent (the Company object has no
101    valid parent in terms of breadcrumbs). Instead it is the
102    ``administration`` view of the same context the ``manage`` page
103    itself is bound to.
104    """
105    grok.context(ICompany)
106    grok.name('manage')
107    title = _(u'Portal Settings')
108
109    @property
110    def parent(self):
111        """Return the 'administration' view of our context as parent.
112        """
113        return (self.context, 'administration')
114
115
116class AdministrationBreadcrumb(Breadcrumb):
117    """A breadcrumb for administration areas of Company instances.
118    """
119    grok.context(ICompany)
120    grok.name('administration')
121    title = _(u'Administration')
122    viewname = 'administration'
123
124
125class ConfigurationContainerBreadcrumb(Breadcrumb):
126    """A breadcrumb for the configuration container.
127    """
128    grok.context(IConfigurationContainer)
129    title = _(u'Portal Configuration')
130    parent_viewname = 'administration'
131
132
133class UsersContainerBreadcrumb(Breadcrumb):
134    """A breadcrumb for user containers.
135    """
136    grok.context(IUsersContainer)
137    title = _(u'Officers')
138    parent_viewname = 'administration'
139
140
141class DataCenterBreadcrumb(Breadcrumb):
142    """A breadcrumb for data centers.
143    """
144    grok.context(IDataCenter)
145    title = _(u'Data Center')
146    parent_viewname = 'administration'
147
148
149class ReportsBreadcrumb(Breadcrumb):
150    """A breadcrumb for reports.
151    """
152    grok.context(IReportsContainer)
153    title = _(u'Reports')
154    parent_viewname = 'administration'
155    target = None
156
157
158def getBreadcrumb(obj, viewname=None):
159    """ Get a breadcrumb for an object and a viewname.
160
161    If there is no breadcrumb defined for such a combination, a
162    breadcrumb for the ``index`` view will be looked up.
163    """
164    try:
165        return getAdapter(obj, IBreadcrumb, name=viewname)
166    except:
167        pass
168    return getAdapter(obj, IBreadcrumb, name='index')
169
170
171def getBreadcrumbList(obj, viewname):
172    """Get an ordered list of breadcrumbs for an object and a viewname.
173
174    Ignorables are excluded from the result.
175    """
176    current = getBreadcrumb(obj, viewname)
177    result = [current]
178    while current.parent is not None:
179        context, viewname = current.parent
180        current = getBreadcrumb(context, viewname)
181        if IBreadcrumbIgnorable.providedBy(current):
182            # Ignore empty breadcrumbs...
183            continue
184        result.append(current)
185    result.reverse()
186    return result
187
188
189def getBreadcrumbListForView(view):
190    """Get an ordered list of breadcrumbs a certain view.
191
192    Ignorables are excluded from the result.
193    """
194    context = getattr(view, 'context')
195    viewname = getattr(view, '__name__')
196    return getBreadcrumbList(context, viewname)
197
198
199class BreadcrumbContainer(grok.Adapter):
200    """An adapter to adapt grok views to list of breadcrumbs.
201    """
202    grok.context(IGrokView)
203    grok.provides(IBreadcrumbContainer)
204
205    _breadcrumbs = None
206
207    def __init__(self, context):
208        self.context = context
209        self._breadcrumbs = getBreadcrumbListForView(self.context)
210
211    def __iter__(self):
212        """Allow iteration.
213        """
214        return self._breadcrumbs.__iter__()
215
216    def getList(self):
217        """Get the (ordered) list of breadcrumbs liked to the context view.
218        """
219        return self._breadcrumbs
Note: See TracBrowser for help on using the repository browser.