1 | ## $Id: vocabularies.py 8439 2012-05-13 19:01:12Z 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 | """Vocabularies and sources for the academics section. |
---|
19 | """ |
---|
20 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
21 | from zc.sourcefactory.contextual import BasicContextualSourceFactory |
---|
22 | from zope.catalog.interfaces import ICatalog |
---|
23 | from zope.component import getUtility, queryUtility |
---|
24 | from waeup.kofa.interfaces import SimpleKofaVocabulary, IKofaUtils |
---|
25 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
26 | from waeup.kofa.utils.utils import KofaUtils |
---|
27 | |
---|
28 | course_levels = SimpleKofaVocabulary( |
---|
29 | (_('Pre-Studies'),10), |
---|
30 | (_('100 (Year 1)'),100), |
---|
31 | (_('200 (Year 2)'),200), |
---|
32 | (_('300 (Year 3)'),300), |
---|
33 | (_('400 (Year 4)'),400), |
---|
34 | (_('500 (Year 5)'),500), |
---|
35 | (_('600 (Year 6)'),600), |
---|
36 | (_('700 (Year 7)'),700), |
---|
37 | (_('800 (Year 8)'),800), |
---|
38 | (_('900 (Year 9)'),900), |
---|
39 | ) |
---|
40 | |
---|
41 | #: An instance of :class:`waeup.kofa.utils.utils.KofaUtils` for fallback. |
---|
42 | KOFA_UTILS = KofaUtils() |
---|
43 | |
---|
44 | class ContextualDictSourceFactoryBase(BasicContextualSourceFactory): |
---|
45 | """A base for contextual sources based on KofaUtils dicts. |
---|
46 | |
---|
47 | To create a real source, you have to set the `DICT_NAME` attribute |
---|
48 | which should be the name of a dictionary in KofaUtils. |
---|
49 | """ |
---|
50 | def getValues(self, context): |
---|
51 | utils = queryUtility(IKofaUtils, default=KOFA_UTILS) |
---|
52 | return sorted(getattr(utils, self.DICT_NAME).keys()) |
---|
53 | |
---|
54 | def getToken(self, context, value): |
---|
55 | return str(value) |
---|
56 | |
---|
57 | def getTitle(self, context, value): |
---|
58 | utils = queryUtility(IKofaUtils, default=KOFA_UTILS) |
---|
59 | return getattr(utils, self.DICT_NAME)[value] |
---|
60 | |
---|
61 | class SemesterSource(ContextualDictSourceFactoryBase): |
---|
62 | """An institution type source delivers semester or term descriptors. |
---|
63 | """ |
---|
64 | #: name of dict to deliver from kofa utils. |
---|
65 | DICT_NAME = 'SEMESTER_DICT' |
---|
66 | |
---|
67 | class InstTypeSource(ContextualDictSourceFactoryBase): |
---|
68 | """An institution type source delivers types of institutions |
---|
69 | in the portal. |
---|
70 | """ |
---|
71 | #: name of dict to deliver from kofa utils. |
---|
72 | DICT_NAME = 'INST_TYPES_DICT' |
---|
73 | |
---|
74 | class AppCatSource(ContextualDictSourceFactoryBase): |
---|
75 | """A application category source delivers all application categories |
---|
76 | provided in the portal. |
---|
77 | """ |
---|
78 | #: name of dict to deliver from kofa utils. |
---|
79 | DICT_NAME = 'APP_CATS_DICT' |
---|
80 | |
---|
81 | class StudyModeSource(ContextualDictSourceFactoryBase): |
---|
82 | """A study modes source delivers all study modes provided |
---|
83 | in the portal. |
---|
84 | """ |
---|
85 | #: name of dict to deliver from kofa utils. |
---|
86 | DICT_NAME = 'STUDY_MODES_DICT' |
---|
87 | |
---|
88 | class CourseSource(BasicSourceFactory): |
---|
89 | """A course source delivers all courses inside the portal by looking |
---|
90 | up a catalog. |
---|
91 | """ |
---|
92 | def getValues(self): |
---|
93 | catalog = getUtility(ICatalog, name='courses_catalog') |
---|
94 | return sorted(list( |
---|
95 | catalog.searchResults( |
---|
96 | code=(None, None))),key=lambda value: value.code) |
---|
97 | |
---|
98 | def getToken(self, value): |
---|
99 | return value.code |
---|
100 | |
---|
101 | def getTitle(self, value): |
---|
102 | return "%s - %s" % (value.code, value.title[:64]) |
---|
103 | |
---|
104 | class CertificateSource(BasicContextualSourceFactory): |
---|
105 | """A certificate source delivers all certificates provided |
---|
106 | in the portal. |
---|
107 | """ |
---|
108 | def getValues(self, context): |
---|
109 | catalog = getUtility(ICatalog, name='certificates_catalog') |
---|
110 | return sorted(list( |
---|
111 | catalog.searchResults( |
---|
112 | code=(None, None))), |
---|
113 | key=lambda value: value.code) |
---|
114 | |
---|
115 | def getToken(self, context, value): |
---|
116 | return value.code |
---|
117 | |
---|
118 | def getTitle(self, context, value): |
---|
119 | return "%s - %s" % (value.code, value.title) |
---|