source: main/waeup.kofa/trunk/src/waeup/kofa/browser/breadcrumbs.py @ 12248

Last change on this file since 12248 was 10655, checked in by Henrik Bettermann, 11 years ago

Implement an VirtualApplicantsExportJobContainer? which allows to export applicants locally. On each container page there is now an'Export applicants' button which directs to the exports overview page. Unlike student exporters, the applicants exporter can't be configured. It just exports all applicants in the container.

  • Property svn:keywords set to Id
File size: 8.2 KB
Line 
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"""
20import grok
21from grokcore.view.interfaces import IGrokView
22from zope.component import getAdapter
23from zope.publisher.browser import TestRequest
24
25from waeup.kofa.interfaces import (
26    IConfigurationContainer, ISessionConfiguration, IExportJobContainer)
27from waeup.kofa.interfaces import MessageFactory as _
28from 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    )
34from waeup.kofa.reports import IReportsContainer
35
36class 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
90class UniversityBreadcrumb(Breadcrumb):
91    """A breadcrumb for university index pages.
92    """
93    grok.context(IUniversity)
94    title = _(u'Home')
95    parent = None
96
97class 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
116class FacultiesContainerBreadcrumb(Breadcrumb):
117    """A breadcrumb for faculty containers.
118    """
119    grok.context(IFacultiesContainer)
120    title = _(u'Academics')
121
122class 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
130class ConfigurationContainerBreadcrumb(Breadcrumb):
131    """A breadcrumb for the configuration container.
132    """
133    grok.context(IConfigurationContainer)
134    title = _(u'Portal Configuration')
135    parent_viewname = 'administration'
136
137class 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
148class UsersContainerBreadcrumb(Breadcrumb):
149    """A breadcrumb for user containers.
150    """
151    grok.context(IUsersContainer)
152    title = _(u'Portal Users')
153    parent_viewname = 'administration'
154
155class DataCenterBreadcrumb(Breadcrumb):
156    """A breadcrumb for data centers.
157    """
158    grok.context(IDataCenter)
159    title = _(u'Data Center')
160    parent_viewname = 'administration'
161
162class ReportsBreadcrumb(Breadcrumb):
163    """A breadcrumb for reports.
164    """
165    grok.context(IReportsContainer)
166    title = _(u'Reports')
167    parent_viewname = 'administration'
168    target = None
169
170class ExportsBreadcrumb(Breadcrumb):
171    """A breadcrumb for exports.
172    """
173    grok.context(IExportJobContainer)
174    title = _(u'Student Data Exports')
175    target = None
176
177class 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
186class DepartmentBreadcrumb(FacultyBreadcrumb):
187    """A breadcrumb for departments.
188    """
189    grok.context(IDepartment)
190
191class CourseBreadcrumb(FacultyBreadcrumb):
192    """A breadcrumb for courses.
193    """
194    grok.context(ICourse)
195
196class CertificateBreadcrumb(FacultyBreadcrumb):
197    """A breadcrumb for certificates.
198    """
199    grok.context(ICertificate)
200
201class CoursesContainerBreadcrumb(Breadcrumb):
202    """ We don't want course container breadcrumbs.
203    """
204    grok.context(ICoursesContainer)
205    grok.implements(IBreadcrumbIgnorable)
206
207class CertificatesContainerBreadcrumb(Breadcrumb):
208    """ We don't want course container breadcrumbs.
209    """
210    grok.context(ICertificatesContainer)
211    grok.implements(IBreadcrumbIgnorable)
212
213class 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
221def 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
233def 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
250def 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
259class 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
Note: See TracBrowser for help on using the repository browser.