1 | ## $Id: breadcrumbs.py 9166 2012-09-06 16:50:17Z uli $ |
---|
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 | """ |
---|
20 | import grok |
---|
21 | from grokcore.view.interfaces import IGrokView |
---|
22 | from zope.component import getAdapter |
---|
23 | from zope.publisher.browser import TestRequest |
---|
24 | |
---|
25 | from waeup.kofa.interfaces import ( |
---|
26 | IConfigurationContainer, ISessionConfiguration) |
---|
27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
28 | from waeup.kofa.browser.interfaces import ( |
---|
29 | IBreadcrumb, IBreadcrumbIgnorable, IBreadcrumbContainer, IKofaObject, |
---|
30 | IUniversity, IFacultiesContainer, IUsersContainer, IDataCenter, IFaculty, |
---|
31 | IDepartment, ICourse, ICertificate, ICoursesContainer, ICertificateCourse, |
---|
32 | ICertificatesContainer, |
---|
33 | ) |
---|
34 | |
---|
35 | class Breadcrumb(grok.Adapter): |
---|
36 | """A most general breadcrumb generator. |
---|
37 | """ |
---|
38 | grok.provides(IBreadcrumb) |
---|
39 | grok.context(IKofaObject) |
---|
40 | grok.name('index') |
---|
41 | |
---|
42 | _title = None |
---|
43 | _parent = 0 |
---|
44 | _request = None |
---|
45 | parent_viewname = 'index' |
---|
46 | viewname = 'index' |
---|
47 | |
---|
48 | def __init__(self, context): |
---|
49 | """Turn a context into a breadcrumb. |
---|
50 | """ |
---|
51 | self.context = context |
---|
52 | |
---|
53 | @property |
---|
54 | def title(self): |
---|
55 | """Get a title for a context. |
---|
56 | """ |
---|
57 | if self._title is not None: |
---|
58 | return self._title |
---|
59 | if hasattr(self.context, 'title'): |
---|
60 | return self.context.title |
---|
61 | if hasattr(self.context, 'name'): |
---|
62 | return self.context.name |
---|
63 | return None |
---|
64 | |
---|
65 | @property |
---|
66 | def parent(self): |
---|
67 | """Get the contexts parent object and viewname or None. |
---|
68 | """ |
---|
69 | if self._parent is None: |
---|
70 | return None |
---|
71 | if self._parent is not 0: |
---|
72 | return (self._parent, self.parent_viewname) |
---|
73 | |
---|
74 | if self.viewname != 'index': |
---|
75 | self._parent = self.context |
---|
76 | else: |
---|
77 | site = grok.getSite() |
---|
78 | if self.context is not site: |
---|
79 | self._parent = getattr(self.context, '__parent__', None) |
---|
80 | |
---|
81 | if self._parent is not None: |
---|
82 | return (self._parent, self.parent_viewname) |
---|
83 | return None |
---|
84 | |
---|
85 | @property |
---|
86 | def target(self): |
---|
87 | return self.viewname |
---|
88 | |
---|
89 | class UniversityBreadcrumb(Breadcrumb): |
---|
90 | """A breadcrumb for university index pages. |
---|
91 | """ |
---|
92 | grok.context(IUniversity) |
---|
93 | title = _(u'Home') |
---|
94 | parent = None |
---|
95 | |
---|
96 | class 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 University 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(IUniversity) |
---|
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 | class FacultiesContainerBreadcrumb(Breadcrumb): |
---|
116 | """A breadcrumb for faculty containers. |
---|
117 | """ |
---|
118 | grok.context(IFacultiesContainer) |
---|
119 | title = _(u'Academics') |
---|
120 | |
---|
121 | class AdministrationBreadcrumb(Breadcrumb): |
---|
122 | """A breadcrumb for administration areas of University instances. |
---|
123 | """ |
---|
124 | grok.context(IUniversity) |
---|
125 | grok.name('administration') |
---|
126 | title = _(u'Administration') |
---|
127 | viewname = 'administration' |
---|
128 | |
---|
129 | class ConfigurationContainerBreadcrumb(Breadcrumb): |
---|
130 | """A breadcrumb for the configuration container. |
---|
131 | """ |
---|
132 | grok.context(IConfigurationContainer) |
---|
133 | title = _(u'Portal Configuration') |
---|
134 | parent_viewname = 'administration' |
---|
135 | |
---|
136 | class SessionConfigurationBreadcrumb(Breadcrumb): |
---|
137 | """A breadcrumb for the configuration container. |
---|
138 | """ |
---|
139 | grok.context(ISessionConfiguration) |
---|
140 | title = u'Portal Session Configuration' |
---|
141 | |
---|
142 | @property |
---|
143 | def title(self): |
---|
144 | session_string = self.context.getSessionString() |
---|
145 | return 'Session %s' % session_string |
---|
146 | |
---|
147 | class UsersContainerBreadcrumb(Breadcrumb): |
---|
148 | """A breadcrumb for user containers. |
---|
149 | """ |
---|
150 | grok.context(IUsersContainer) |
---|
151 | title = _(u'Portal Users') |
---|
152 | parent_viewname = 'administration' |
---|
153 | |
---|
154 | class DataCenterBreadcrumb(Breadcrumb): |
---|
155 | """A breadcrumb for data centers. |
---|
156 | """ |
---|
157 | grok.context(IDataCenter) |
---|
158 | title = _(u'Data Center') |
---|
159 | parent_viewname = 'administration' |
---|
160 | |
---|
161 | class FacultyBreadcrumb(Breadcrumb): |
---|
162 | """A breadcrumb for faculties. |
---|
163 | """ |
---|
164 | grok.context(IFaculty) |
---|
165 | |
---|
166 | @property |
---|
167 | def title(self): |
---|
168 | return self.context.longtitle() |
---|
169 | |
---|
170 | class DepartmentBreadcrumb(FacultyBreadcrumb): |
---|
171 | """A breadcrumb for departments. |
---|
172 | """ |
---|
173 | grok.context(IDepartment) |
---|
174 | |
---|
175 | class CourseBreadcrumb(FacultyBreadcrumb): |
---|
176 | """A breadcrumb for courses. |
---|
177 | """ |
---|
178 | grok.context(ICourse) |
---|
179 | |
---|
180 | class CertificateBreadcrumb(FacultyBreadcrumb): |
---|
181 | """A breadcrumb for certificates. |
---|
182 | """ |
---|
183 | grok.context(ICertificate) |
---|
184 | |
---|
185 | class CoursesContainerBreadcrumb(Breadcrumb): |
---|
186 | """ We don't want course container breadcrumbs. |
---|
187 | """ |
---|
188 | grok.context(ICoursesContainer) |
---|
189 | grok.implements(IBreadcrumbIgnorable) |
---|
190 | |
---|
191 | class CertificatesContainerBreadcrumb(Breadcrumb): |
---|
192 | """ We don't want course container breadcrumbs. |
---|
193 | """ |
---|
194 | grok.context(ICertificatesContainer) |
---|
195 | grok.implements(IBreadcrumbIgnorable) |
---|
196 | |
---|
197 | class CertificateCourseBreadcrumb(Breadcrumb): |
---|
198 | """ We don't want course container breadcrumbs. |
---|
199 | """ |
---|
200 | grok.context(ICertificateCourse) |
---|
201 | @property |
---|
202 | def title(self): |
---|
203 | return self.context.longtitle() |
---|
204 | |
---|
205 | def getBreadcrumb(obj, viewname=None): |
---|
206 | """ Get a breadcrumb for an object and a viewname. |
---|
207 | |
---|
208 | If there is no breadcrumb defined for such a combination, a |
---|
209 | breadcrumb for the ``index`` view will be looked up. |
---|
210 | """ |
---|
211 | try: |
---|
212 | return getAdapter(obj, IBreadcrumb, name=viewname) |
---|
213 | except: |
---|
214 | pass |
---|
215 | return getAdapter(obj, IBreadcrumb, name='index') |
---|
216 | |
---|
217 | def getBreadcrumbList(obj, viewname): |
---|
218 | """Get an ordered list of breadcrumbs for an object and a viewname. |
---|
219 | |
---|
220 | Ignorables are excluded from the result. |
---|
221 | """ |
---|
222 | current = getBreadcrumb(obj, viewname) |
---|
223 | result = [current] |
---|
224 | while current.parent is not None: |
---|
225 | context, viewname = current.parent |
---|
226 | current = getBreadcrumb(context, viewname) |
---|
227 | if IBreadcrumbIgnorable.providedBy(current): |
---|
228 | # Ignore empty breadcrumbs... |
---|
229 | continue |
---|
230 | result.append(current) |
---|
231 | result.reverse() |
---|
232 | return result |
---|
233 | |
---|
234 | def getBreadcrumbListForView(view): |
---|
235 | """Get an ordered list of breadcrumbs a certain view. |
---|
236 | |
---|
237 | Ignorables are excluded from the result. |
---|
238 | """ |
---|
239 | context = getattr(view, 'context') |
---|
240 | viewname = getattr(view, '__name__') |
---|
241 | return getBreadcrumbList(context, viewname) |
---|
242 | |
---|
243 | class BreadcrumbContainer(grok.Adapter): |
---|
244 | """An adapter to adapt grok views to list of breadcrumbs. |
---|
245 | """ |
---|
246 | grok.context(IGrokView) |
---|
247 | grok.provides(IBreadcrumbContainer) |
---|
248 | |
---|
249 | _breadcrumbs = None |
---|
250 | |
---|
251 | def __init__(self, context): |
---|
252 | self.context = context |
---|
253 | self._breadcrumbs = getBreadcrumbListForView(self.context) |
---|
254 | |
---|
255 | def __iter__(self): |
---|
256 | """Allow iteration. |
---|
257 | """ |
---|
258 | return self._breadcrumbs.__iter__() |
---|
259 | |
---|
260 | def getList(self): |
---|
261 | """Get the (ordered) list of breadcrumbs liked to the context view. |
---|
262 | """ |
---|
263 | return self._breadcrumbs |
---|