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